一个mysql备份脚本

一个mysql备份脚本

#!/bin/bash
# 用于使用xtrabackup进行物理备份
# 默认备份策略为周六全备,其他时间基于周六一级增量备份
# 备份文件保留两周
# created by wjf 20190724

backup_dir_base="/Data/mysql/backup/"
cmd_inno_xtraback="/bin/innobackupex"

backup_start_time=$(date +%Y%m%d%H%M%S)
current_date_in_week=$(date +%w)
current_day=$(date +%Y%m%d)
backup_log_today="/Data/mysql/backup/log/${current_day}_backup.log"
backup_log_summary="/Data/mysql/backup/backup_summary.log"

# 设置全量备份的星期几,0代表星期日,1代表星期1
full_backup_day=6
# 通过计算,获取上次全备的目录名
if [ ${full_backup_day} -gt ${current_date_in_week} ];then
    interval_days_to_last_full="-$((7+current_date_in_week-full_backup_day)) days"
else
    interval_days_to_last_full="-$((current_date_in_week-full_backup_day)) days"
fi
full_backup_dir="${backup_dir_base}/`date -d \"${interval_days_to_last_full}\" +%Y%m%d`_full"

# db连接信息
mydb_cnf_file="/Data/mysql/my3306/my.cnf"
mydb_username="mysqladmin"
mydb_password="mysqladmin"
mydb_socket="/Data/mysql/my3306/my3306.sock"



if [ ${current_date_in_week} == ${full_backup_day} ];then
  back_dir_today="${backup_dir_base}/${current_day}_full"
  if [ ! -d ${back_dir_today} ];then
     mkdir -p ${back_dir_today}
     echo "${backup_start_time}----none" > ${backup_log_summary}
     ${cmd_inno_xtraback} --defaults-file=${mydb_cnf_file} --no-timestamp --slave-info --user=${mydb_username} --password=${mydb_password} --socket=${mydb_socket} ${back_dir_today} > ${backup_log_today} 2>&1
  else
     echo "fail--${back_dir_today} dir exists before backup!" >> ${backup_log_summary}
     exit 1
  fi
else
  back_dir_today="${backup_dir_base}/${current_day}_incr"
  if [ ! -d ${back_dir_today} -a -d ${full_backup_dir} ];then
     mkdir -p ${back_dir_today}
     echo "${backup_start_time}----none" > ${backup_log_summary}
     ${cmd_inno_xtraback} --defaults-file=${mydb_cnf_file} --no-timestamp --slave-info --user=${mydb_username} --password=${mydb_password} --socket=${mydb_socket} --incremental --incremental-basedir=${full_backup_dir} ${back_dir_today} > ${backup_log_today} 2>&1
  else
     echo "fail--${back_dir_today} dir exists before backup or full backup dir ${full_backup_dir} not exists" >> ${backup_log_summary}
     exit 1
  fi
fi

if [ $? == 0 ];then
    echo "${backup_start_time}----$(date +%Y%m%d%H%M%S)----success" > ${backup_log_summary}
    # 删除动作,就不用变量了,测试阶段,直接写死目录和时间
    find /Data/mysql/backup/* -type d -ctime +14 -exec rm -rf {} \;    
fi

你可能感兴趣的:(mysql)