用cron每天18:00自动备份MySQL数据库,文件名以当天的年月日命名,前缀为数据库名,备份后自动删除前六天的备份。
1、建备份目录,以用户shaobing为例
$mkdir /home/shaobing/dbbak
$chmod 755 /home/shaobing/dbbak //确保该文件可写
$mkdir /home/shaobing/dbbak/rd_db
$chmod 755 /home/shaobing/dbbak/rd_db //确保该文件可写
2、写/home/shaobing/dbbak/dbbak.sh 脚本。DBName=数据库名 BackupPath=备份目录 DBbinPath=mysqldump等实用工具所在目录
#!/bin/sh
DBName=npc_people
BackupPath=/home/shaobing/dbbak/rd_dbbak/
DBbinPath=/usr/local/bin/mysqldump
if ${DBbinPath} --opt --extended-insert=false -uroot ${DBName} > ${BackupPath}${DBName}"/"${DBName}`date "+%Y-%m-%d"`".sql" ;
then
find ${DBbinPath}${DBName}"/" -mtime +6 -exec rm {} \;
else
exit
fi
3、将dbbak.sh加入cron
#vi /etc/crontab 加入下面一行:
#每天下午6点(18点)备份数据并删除前6六天的备份文件
0 18 * * * root /home/shaobing/dbbak/dbbak.sh
28 11 * * * root /home/shaobing/dbbak/dbbak.sh
------------------------------------------------------------------------------------------
直接copy数据库文件
在shell里 写cp -r /var/db/mysql/npc_people/ .../.......
#!/bin/sh
mkdir /home/shaobing/dbbak/rd_dbbak/`date "+%Y-%m-%d"`/
cp -r /var/db/mysql/npc_people/ /home/shaobing/dbbak/rd_dbbak/`date "+%Y-%m-%d"`/