Mysql8的备份脚本 ,基于xtrabackup8.0
注意:xtrabackup8.0与mysql8直接的版本依赖强,安装xtra后可查看支持的Mysql8版本。
xtrabackup8 下载页面:https://centos.pkgs.org/7/percona-x86_64/percona-xtrabackup-80-8.0.13-1.el7.x86_64.rpm.html
脚本建议在测试库测试好之后再上生产
#!/bin/bash
# 备份策略(可以根据需求调整备份策略)
# 周三全量备份
# 周一、周二、周四、周五、周六、周日增量备份
# 全量备份目录
Full=/data/xtr_mysql8/fullbak
# 增量备份目录
Incr=/data/xtr_mysql8/incrbak
# MySQL相关配置信息
DB_HOST='localhost'
DB_PORT=3309
DB_USER='backup'
DB_PASS='backup@2022'
DB_SOCK=/tmp/mysql.sock
DB_CONF=/etc/my.cnf
# 备份的日志文件
baklog=/data/xtr_mysql8/back.log
# 备份开始
echo -e "\n\n" | tee -a ${baklog} 2>&1
echo -e "备份开始...\n" | tee -a ${baklog} 2>&1
# 判断当前日期是周几
Week=`date +%w`
case ${Week} in
3)
# 判断今日为周三则进行全量备份
# 判断主目录是否存在,不存在则创建
if [ ! -d "${Full}" ];then
mkdir ${Full}
fi
# 第一步 创建本次的备份目录
FullBakTime=`date +%Y%m%d-%H%M%S`
mkdir -p ${Full}/${FullBakTime}_${Week}
FullBakDir=${Full}/${FullBakTime}_${Week}
# 第二步 开始全量备份
echo -e "日期: ${FullBakTime}\n" | tee -a ${baklog} 2>&1
echo -e "星期: ${Week}\n" | tee -a ${baklog} 2>&1
echo -e "本次全量备份目录为 ${FullBakDir}\n" | tee -a ${baklog} 2>&1
xtrabackup --defaults-file=${DB_CONF} --host=${DB_HOST} --user=${DB_USER} --password=${DB_PASS} --socket=${DB_SOCK} --backup --target-dir=${FullBakDir} --no-timestamp
dirStorage=`du -sh ${FullBakDir}`
echo -e "本次备份数据 ${dirStorage}\n" | tee -a ${baklog} 2>&1
echo -e "备份完成...\n\n\n" | tee -a ${baklog} 2>&1
exit 0
;;
0|1|2|4|5|6)
# 判断今日为周日、周一、周二、周四、周五、周六时,进行增量备份。由于周四时基于前一天的全量备份进行增量备份的,而周一、周二、周五、周六、周日是基于前一天的增量备份进行增量备份的,所以再进行一次判断。
case ${Week} in
4)
# 基于上一次全量备份的增量备份
# 判断主目录是否存在,不存在则创建
if [ ! -d "${Incr}" ];then
mkdir ${Incr}
fi
# 第一步 获取上一次全量备份
LatestFullDir=`ls -lth ${Full}|grep -v total|head -1|awk -F '[ ]+' '{print $NF}'`
CompliteLatestFullDir=${Full}/${LatestFullDir}
# 第二步 创建备份目录
IncrBakTime=`date +%Y%m%d-%H%M%S`
mkdir -p ${Incr}/${IncrBakTime}_${Week}
IncrBakDir=${Incr}/${IncrBakTime}_${Week}
# 第三步 开始增量备份
echo -e "日期: ${IncrBakTime}\n" | tee -a ${baklog} 2>&1
echo -e "星期: ${Week}\n" | tee -a ${baklog} 2>&1
echo -e "本次备份为基于上一次全量备份${CompliteLatestFullDir}的增量备份\n" | tee -a ${baklog} 2>&1
echo -e "本次增量备份目录为: ${IncrBakDir}\n" | tee -a ${baklog} 2>&1
xtrabackup --defaults-file=${DB_CONF} --host=${DB_HOST} --user=${DB_USER} --password=${DB_PASS} --socket=${DB_SOCK} --backup --parallel=4 --target-dir=${IncrBakDir} --incremental-basedir=${CompliteLatestFullDir} --no-timestamp
dirStorage=`du -sh ${IncrBakDir}`
echo -e "本次备份数据 ${dirStorage}\n" | tee -a ${baklog} 2>&1
echo -e "备份完成...\n\n\n" | tee -a ${baklog} 2>&1
exit 0
;;
0|1|2|5|6)
# 基于上一次增量备份的增量备份
# 判断主目录是否存在,不存在则创建
if [ ! -d "${Incr}" ];then
mkdir ${Incr}
fi
# 第一步 获取上一次增量备份
LatestIncrDir=`ls -lth ${Incr}|grep -v total|head -1|awk -F '[ ]+' '{print $NF}'`
CompliteLatestIncrDir=${Incr}/${LatestIncrDir}
# 第二步 创建备份目录
IncrBakTime=`date +%Y%m%d-%H%M%S`
mkdir -p ${Incr}/${IncrBakTime}_${Week}
IncrBakDir=${Incr}/${IncrBakTime}_${Week}
# 第三步 开始增量备份
echo -e "日期: ${IncrBakTime}\n" | tee -a ${baklog} 2>&1
echo -e "星期: ${Week}\n" | tee -a ${baklog} 2>&1
echo -e "本次备份为基于上一次增量备份${CompliteLatestIncrDir}的增量备份\n" | tee -a ${baklog} 2>&1
echo -e "本次增量备份目录为: ${IncrBakDir}\n" | tee -a ${baklog} 2>&1
xtrabackup --defaults-file=${DB_CONF} --host=${DB_HOST} --user=${DB_USER} --password=${DB_PASS} --socket=${DB_SOCK} --backup --parallel=4 --target-dir=${IncrBakDir} --incremental-basedir=${CompliteLatestIncrDir} --no-timestamp
dirStorage=`du -sh ${IncrBakDir}`
echo -e "本次备份数据 ${dirStorage}\n" | tee -a ${baklog} 2>&1
echo -e "备份完成...\n\n\n" | tee -a ${baklog} 2>&1
exit 0
;;
*)
echo -e "Error\n"
exit 1
;;
esac
;;
*)
echo -e "Error\n"
exit 1
;;
esac