一个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值