xtrabackup备份小脚本

#!/bin/bash
#数据库备份脚本
#方式: 物理备份
#工具:xtrabackup
#需求1:在脚本中,不允许有任何的安装过程
#需求2:在脚本中,不允许有任何的交互式代码
#需求3:在脚本中,不允许有任何输出内容,使用日志记录
#需求4:每周为一个循环周期,1全备、2增量、3增量、4差异、5增量、6增量、7差异,自动检测
#需求5:执行报错,邮件告警
#配置服务器邮箱
cat > /etc/mail.rc <<EOF
set from=768252235@qq.com 
set smtp=smtp.qq.com 
set smtp-auth-user=768252235@qq.com
set smtp-auth-password=igpagdrxvrckbgaf
set smtp-auth=login
EOF
#创建数据和日志存放目录
mkdir /xtrabackup/{data,log} -p
#一周中的一天(0..6),0代表星期天
week=$(date +%w)
#mysql数据库账户密码
mysql_user=root
mysql_passwd=Qianfeng@123
#%V   ISO 标准计数周,该方法将周一作为一周的起始 (01..53)
data_name=$(date +%Y)backup_$(date +%V)week
#定义邮件发送变量
mailx=$(echo "$data_name-$week备份数据失败" | mailx -s "$备份告警" 768252235@qq.com)
if [ $week -eq 2 ];then
  mkdir /xtrabackup/data/$data_name/$week/ -p
  mkdir /xtrabackup/log/$data_name/$week/ -p
  innobackupex --user=$mysql_user --password=$mysql_passwd /xtrabackup/data/$data_name/$week/ &>/xtrabackup/log/$data_name/$week/backup.log
 elif [ $week-eq 1 ];then
   mkdir /xtrabackup/data/$data_name/$week/ -p
   mkdir /xtrabackup/log/$data_name/$week/ -p
   innobackupex --user=$mysql_user --password=$mysql_passwd --incremental /xtrabackup/data/$data_name/$week/ --incremental-basedir=$(ls /xtrabackup/data/$data_name/1/*) &>/xtrabackup/log/$data_name/$week/backup.log
 elif [ $week -eq 3 ];then
   mkdir /xtrabackup/data/$data_name/$week/ -p
   mkdir /xtrabackup/log/$data_name/$week/ -p
   innobackupex --user=$mysql_user --password=$mysql_passwd --incremental /xtrabackup/data/$data_name/$week/ --incremental-basedir=$(ls /xtrabackup/data/$data_name/2/*) &>/xtrabackup/log/$data_name/$week/backup.log
 elif [ $week -eq 4 ];then
   mkdir /xtrabackup/data/$data_name/$week/ -p
   mkdir /xtrabackup/log/$data_name/$week/ -p
   innobackupex --user=$mysql_user --password=$mysql_passwd --incremental /xtrabackup/data/$data_name/$week/ --incremental-basedir=$(ls /xtrabackup/data/$data_name/1/*) &>/xtrabackup/log/$data_name/$week/backup.log
 elif [ $week -eq 5 ];then
   mkdir /xtrabackup/data/$data_name/$week/ -p
   mkdir /xtrabackup/log/$data_name/$week/ -p
   innobackupex --user=$mysql_user --password=$mysql_passwd --incremental /xtrabackup/data/$data_name/$week/ --incremental-basedir=$(ls /xtrabackup/data/$data_name/4/*) &>/xtrabackup/log/$data_name/$week/backup.log
 elif [ $week -eq 6 ];then
   mkdir /xtrabackup/data/$data_name/$week/ -p
   mkdir /xtrabackup/log/$data_name/$week/ -p
   innobackupex --user=$mysql_user --password=$mysql_passwd --incremental /xtrabackup/data/$data_name/$week/ --incremental-basedir=$(ls /xtrabackup/data/$data_name/5/*) &>/xtrabackup/log/$data_name/$week/backup.log
 elif [ $week -eq 0 ];then
   mkdir /xtrabackup/data/$data_name/$week/ -p
   mkdir /xtrabackup/log/$data_name/$week/ -p
   innobackupex --user=$mysql_user --password=$mysql_passwd --incremental /xtrabackup/data/$data_name/$week/ --incremental-basedir=$(ls /xtrabackup/data/$data_name/1/*) &>/xtrabackup/log/$data_name/$week/backup.log
else
  $mailx
fi