mysql周三周六全量备份,其他时间增量备份。
脚本内容如下
#!/bin/bash
Full=/usr/local/databak/fullbak
Incr=/usr/local/databak/incrbak
DB_HOST='localhost'
DB_PORT=13360
DB_USER='backup'
DB_PASS='Yh03D66h#*.'
DB_SOCK=/usr/local/mysql/mysql.sock
DB_CONF=/etc/my.cnf
baklog=./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|6)
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 --compress --compress-threads=4 --slave-info --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)
case ${Week} in
0|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 --compress --compress-threads=4 --slave-info --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
;;
1|2|5)
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 --compress --compress-threads=4 --slave-info --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
以上内容根据自己实际情况进行修改。
其修改地方只有 MySQL相关配置信息 与 路径
第三篇有每周全量备份脚本,感谢关注。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c35a7189d9c9335bfc369e56c49cfdc9.png)