本文章给大家分享一个mysql5.5的二进制日志增量备份脚步代码,这个我只是写了代码,如果要定时备份计划任务来操作。
代码如下 |
|
#!/bin/bash
#定义用户名及密码
host=
user=
userPWD=
#定义数据库
database=
mysqladmin -h$host -u$user flush-logs
daily_databakDir=/backdir
#定义MYSQL数据目录
mysqlDataDir=/mysql5.5/data
eMailFile=$daily_databakDir/email.txt
#eMail=
DATE=`date +%Y%m%d%H%M%S`
logFile=$daily_databakDir/mysql$DATE.log
#echo " " > $eMailFile
#echo "-----------------------" >> $eMailFile
#echo $(date +"%y-%m-%d %H:%M:%S") >> $eMailFile
#echo "-------
#
#------------------" >> $eMailFile
TIME=$(date "-d 40 day ago" +%Y%m%d%H%M%S)
StartTime=$(date -d "-60 minute" +"%Y-%m-%d %H:%M:%S")
echo “Delete 40 days before the log” >>$eMailFile
mysql -h$host -u$user -e "purge master logs before ${TIME}" && echo "delete 40 days before log" |tee -a $eMailFile #删除10天前的2进制文件
filename=`cat $mysqlDataDir/mysql-bin.index |awk -F "/" '{print $2}'` # 2进制文件
for i in $filename
do
#echo "$StartTime start backup binlog" >> $eMailFile
mysqlbinlog -h$host -u$user -d $database --start-datetime="$StartTime" $mysqlDataDir/$i >> $daily_databakDir/daily$DATE.sql |tee -a $eMailFile
done
if [ $? = 0 ]
then
# 删除mtime>32的增量日志备份文件
find $daily_databakDir -name "*.log" -type f -mtime +32 -exec rm {} ; > /dev/null 2>&1
cd $daily_databakDir
tar -cvzf $daily_databakDir/$database$DATE.tar.gz daily$DATE.sql
# >>/dev/null 2&1
#scp 至另一台服务器
echo "Daily backup succeed" >>$eMailFile
else
echo "Daily backup fail" >>$eMailFile
#mail -s "MySQL Backup" $eMail < $eMailFile #备份失败之后发送邮件通知
fi
cat $eMailFile > $logFile
# 删除mtime>32的增量日志备份文件
find $daily_databakDir -name "*.log" -type f -mtime +32 -exec rm {} ; > /dev/null 2>&1
|