数据库备份脚本

#!/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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值