这次使用的mysql版本是5.6.20、xtrabackup 2.X
如有问题、可联系公众号:阿牛哥在厦门
通过查看官方文档,xtrabackup 已经分成了2.X版本和8.X版本,其中8.X版本专门针对于mysql8系列、从mysql8.0.33为分水岭、压缩解压参数有修改。
备份脚本:
代码语言:javascript复制#!/bin/sh
inc_back_num=7 # 备份次数周期
BEGINTIME=`date "%Y-%m-%d %H:%M:%S"`
format_time=`date "%Y-%m-%d_%H:%M:%S"`
week=`date %w`
#MySQL数据库端口号
port=3306
#ip地址
ip=localhost
#备份文件存放的路径
basedir=/home/backup
backdir=${basedir}/xbstream_tables
#历史备份路径
backdir_bak=${basedir}/xbstream_tables_bak
#mysql配置文件
file_cnf=/etc/my.cnf
#mysql用户名
user_name=root
#mysql密码
password=Xmlgrg163
#xtrabackup备份日志文件名
out_log=${backdir}/xbstream_log_${format_time}
#备份过程文本名
time_cost=${backdir}/xbstream_time.txt
#备份指定数据库,多个空格隔开,如-–databases="dbname1 dbname2" ,不加备份所有库
#列表中的每个值是以空格区分的,可以用双引号包含空格或单引号的值
DBname_all="tdatabak tdata"
#判断$backdir是否存在 最后一天 备份的文件夹 rec[${inc_back_num} - 2]
if [ -d "${backdir}/rec$[${inc_back_num}-2]" ];then
rm -rf ${backdir_bak}
mv ${backdir} ${backdir_bak}
mkdir ${backdir}
fi
#prapare xtrabackup-xbstream
if [ ! -d "${basedir}" ]; then
mkdir ${basedir}
fi
if [ ! -d "${basedir}/inc-log" ]; then
mkdir ${basedir}/inc-log
fi
if [ ! -d "${basedir}/inc-log/${format_time}" ]; then
mkdir ${basedir}/inc-log/${format_time}
fi
if [ ! -d "${basedir}/lsndir" ]; then
mkdir ${basedir}/lsndir
fi
if [ ! -d "$backdir" ]; then
mkdir $backdir
fi
#start xtrabackup
#full
if [ ! -d "${backdir}/full" ];then
mkdir ${backdir}/full
echo "#####start full backup at ${BEGINTIME} to directory full" >>${time_cost}
innobackupex --no-timestamp --user=${user_name} --password=${password} --databases="${DBname_all}" --host=${ip} --port=${port} --stream=xbstream --compress --extra-lsndir=${backdir}/full ${backdir}/full 1> ${out_log} 2>&1 >${backdir}/full/full.xbstream
find_content=$(find ${out_log}|xargs grep -ri ' completed OK!' -l)
if [ "${find_content}" = "" ];then
echo "full backup is fail" >>${time_cost}
rm -rf ${backdir}/*
# 可配合一个 报警的 命令
echo "remove fail backup files">>${time_cost}
else
echo "full backup is success,start remove history backup." >>${time_cost}
# 如果全备成功了,就清除上一次 备份的目录,节省服务器空间
rm -rf ${backdir_bak}
echo "completed remove history backup." >>${time_cost}
fi
elif [ ! -d "${backdir}/rec0" ];then
mkdir ${backdir}/rec0
echo "#####start 0 incremental backup at ${BEGINTIME} to directory rec0" >>${time_cost}
innobackupex --no-timestamp --user=${user_name} --password=${password} --databases="${DBname_all}" --host=${ip} --port=${port} --stream=xbstream --compress --extra-lsndir=${backdir}/rec0 --incremental --incremental-basedir=${backdir}/full ${backdir}/rec0 1> ${out_log} 2>&1 >${backdir}/rec0/rec0.xbstream
find_content=$(find ${out_log}|xargs grep -ri ' completed OK!' -l)
if [ "${find_content}" = "" ];then
echo "rec0 backup is fail !!!!" >>${time_cost}
rm -rf ${backdir}/rec0
echo "remove fail backup files">>${time_cost}
fi
else
for ((i=1;i<=$[${inc_back_num}-2];i ))
do
if [ ! -d "${backdir}/rec${i}" ]; then
mkdir ${backdir}/rec${i}
echo "#####start ${i} incremental backup at ${BEGINTIME} to directory rec${i}" >>${time_cost}
innobackupex --no-timestamp --user=${user_name} --password=${password} --databases="${DBname_all}" --host=${ip} --port=${port} --stream=xbstream --compress --extra-lsndir=${backdir}/rec${i} --incremental --incremental-basedir=${backdir}/rec$[${i}-1] ${backdir}/rec${i} 1> ${out_log} 2>&1 >${backdir}/rec${i}/rec${i}.xbstream
find_content=$(find ${out_log}|xargs grep -ri ' completed OK!' -l)
if [ "${find_content}" = "" ];then
echo "rec${i} backup is fail !!!!" >>${time_cost}
rm -rf ${backdir}/rec${i}
echo "remove fail backup files">>${time_cost}
fi
break;
fi
done
fi
ENDTIME=`date "%Y-%m-%d %H:%M:%S"`
begin_data=`date -d "${BEGINTIME}" %s`
end_data=`date -d "${ENDTIME}" %s`
spendtime=`expr ${end_data} - ${begin_data}`
echo "it takes ${spendtime} sec for packing the data directory" >>${time_cost}
# 调试可以打开,后边可以注释
tail -f ${time_cost}
上面脚本不能用于mysql8系列的备份,因为xtrabackup备份参数有变。