#!/bin/bash
#数据库备份
#工具:xtrabackup
if [ ! -d /xtrabackup/ ];then
mkdir /xtrabackup/{full,inter,diff} -p
fi
to_mail=15191876750@163.com
db_user=root
db_passwd=Aren@123
basedir=/xtrabackup/full/
baseinter=/xtrabackup/inter/
basediff=/xtrabackup/diff/
full=`ls -l -t /xtrabackup/full/ | awk 'NR==2{print $NF}'`
inter=`ls -l -t /xtrabackup/inter/ | awk 'NR==2{print $NF}'`
diff=`ls -l -t /xtrabackup/diff/ | awk 'NR==2{print $NF}'`
#全量备份
full(){
innobackupex --user=$db_user --password=$db_passwd ${basedir}
}
#增量备份
inter(){
if [ $day -eq 2 ];then
innobackupex --user=$db_user --password=$db_passwd --incremental ${baseinter} --incremental-basedir=${basedir}${full}
elif [ $day -eq 5 ];then
innobackupex --user=$db_user --password=$db_passwd --incremental ${baseinter} --incremental-basedir=${basediff}${diff}
else
innobackupex --user=$db_user --password=$db_passwd --incremental ${baseinter} --incremental-basedir=${baseinter}${inter}
fi
}
#差异备份
diff(){
innobackupex --user=$db_user --password=$db_passwd --incremental ${basediff} --incremental-basedir=${basedir}${basedir}
}
ntpdate time.windows.com >/dev/null
day=`date +%u`
if [[ $day =~ 1 ]];then
full &>>/var/log/xtrabackup.log
elif [[ $day =~ 2|3|5|6 ]];then
inter &>>/var/log/xtrabackup.log
elif [[ $day =~ 4|7 ]];then
diff &>>/var/log/xtrabackup.log
fi
if [ $? -eq 0 ];then
echo "completed OK!" &>>/var/log/xtrabackup.log
echo "周${day}备份成功!" | mail -s "注意!" $to_mail
else
echo "completed Falied!" &>>/var/log/xtrabackup.log
echo "周${day}备份失败! 请查看!!!" | mail -s "警告!" $to_mail
fi
配置邮件
[root@server02 ~]# vim /etc/mail.rc
set bsdcompat
set from=XXXXXXXXXXX0@163.com
set smtp=smtp.163.com
set smtp-auth-user=XXXXXXXXXXXXX@163.com
set smtp-auth-password=XXXXXXXXXXXX
set smtp-auth=login
set ssl-verify=ignore