1.linux平台
30 3 * * * sh /data/tools/mysqlbackup.sh 每天3点半备份数据库
mysqlbackup.sh(备份最近5天的数据):
1 #设置数据库名,数据库登录名,密码,备份路径,日志路径,数据文件位置, 2 #以及备份方式 3 #默认情况下备份方式是tar,还可以是mysqldump,mysqldotcopy 4 #默认情况下,用root(空)登录mysql数据库,备份至/root/dbxxxxx.tgz 5 6 DBName="test" 7 DBUser="root" 8 DBPasswd="123456" 9 BackupPath="/data/mysql_backup/" 10 LogFile="/data/mysql_backup/db.log" 11 #DBPath="/opt/lamp/mysql/" 12 BackupMethod="mysqldump" 13 BackupMethodPath="/opt/lamp/bin/mysqldump" 14 15 NewFile="$BackupPath"db$(date +%y%m%d).tgz 16 DumpFile="$BackupPath"db$(date +%y%m%d) 17 OldFile="$BackupPath"db$(date +%y%m%d --date='5 days ago').tgz 18 19 echo "-------------------------------------------" >> $LogFile 20 echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile 21 echo "--------------------------" >> $LogFile 22 23 #Delete Old File 24 if [ -f $OldFile ] 25 then 26 rm -f $OldFile >> $LogFile 2>&1 27 echo "[$OldFile]Delete Old File Success!" >> $LogFile 28 else 29 echo "[$OldFile]No Old Backup File!" >> $LogFile 30 fi 31 32 if [ -f $NewFile ] 33 then 34 echo "[$NewFile]The Backup File is exists, Can't Backup!" >> $LogFile 35 else 36 case $BackupMethod in 37 mysqldump) 38 if [ -z $DBPasswd ] 39 then 40 echo "$BackupMethodPath -u $DBUser --opt $DBName > $DumpFile" 41 $BackupMethodPath -u $DBUser --opt $DBName > $DumpFile 42 else 43 echo "$BackupMethodPath -u $DBUser -p$DBPasswd --opt $DBName" 44 $BackupMethodPath -u $DBUser -p$DBPasswd --opt $DBName > $DumpFile 45 fi 46 tar -czvf $NewFile $DumpFile >> $LogFile 2>&1 47 echo "[$NewFile]Backup Success!" >> $LogFile 48 rm -rf $DumpFile 49 ;; 50 mysqlhotcopy) 51 rm -rf $DumpFile 52 mkdir $DumpFile 53 if [ -z $DBPasswd ] 54 then 55 mysqlhotcopy -u $DBUser $DBName $DumpFile >> $LogFile 2>&1 56 else 57 mysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1 58 fi 59 tar czvf $NewFile $DumpFile >> $LogFile 2>&1 60 echo "[$NewFile]Backup Success!" >> $LogFile 61 rm -rf $DumpFile 62 ;; 63 esac 64 fi 65 66 echo "-------------------------------------------" >> $LogFile
2.windows平台
...