每天早晨7点执行数据库的完全备份,同时目录下的备份文件保留10天。
#!/bin/bash
#Setting
DBName=ad_analyse
DBUser=root
DBPasswd=Zhong24Nan!@Hai
BackupPath=/home/backup/
LogFile=/home/backup/db.log
DBPath=/var/lib/mysql/
BackupMethod=mysqldump
#BackupMethod=mysqlhotcopy
#BackupMethod=tar
#Setting End
NewFile="$BackupPath"ad_analyse$(date +%y%m%d).tgz
DumpFile="$BackupPath"ad_analyse$(date +%y%m%d).sql
OldFile="$BackupPath"ad_analyse$(date +%y%m%d --date='10 days ago').tgz
echo "-------------------------------------------" >> $LogFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile
echo "--------------------------" >> $LogFile
#Delete Old File
if [ -f $OldFile ]
then
rm -f $OldFile >>$LogFile 2>&1
echo "[$OldFile]Delete Old File Success!" >> $LogFile
else
echo "[$OldFile]No Old Backup File!" >> $LogFile
fi
if [ -f $NewFile ]
then
echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile
else
case $BackupMethod in
mysqldump)
if [ -z $DBPasswd ]
then
mysqldump -u $DBUser --opt $DBName > $DumpFile
else
mysqldump -u $DBUser -p$DBPasswd -R -B --default-character-set=utf8 --opt $DBName > $DumpFile
fi
cd /home/backup/
tar czvf $NewFile ad_analyse$(date +%y%m%d).sql >> $LogFile 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
rm -rf $DumpFile
;;
esac
fi
echo "-------------------------------------------" >> $LogFile