MySQL数据库使用XtraBackup备份实现示例
关于MySQL数据库的备份的工具和方式也比较多,本文只简单介绍一些我司一个平台的备份方案。Xtrabackup是由percona开源的免费数据库热备份软件,但是只能对InnoDB数据库和XtraDB存储引擎的数据库进行非阻塞的备份,但是对于MyISAM(比如MySQL)的备份同样在备份过程中会锁表。更多的关于Xtrabackup介绍请度娘之。
1,安装Xtrabackup
到官网https://www.percona.com下载tar进行安装,由于我司服务器部分可连公网,直接在服务商下载并传到目标主机即可
[root@host1 tmp]#wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.9/binary/RedHat/6/x86_64/Percona-XtraBackup-2.4.9-ra467167cdd4-el6-x86_64-bundle.tar
[root@host1 tmp]#tar -xvf Percona-XtraBackup-2.4.9-ra467167cdd4-el6-x86_64-bundle.tar
[root@host1 tmp]#rpm -ivh percona-xtrabackup-24-debuginfo-2.4.9-1.el6.x86_64.rpm
安装后会生成几个工具,其中innobackupex就可以用来对MySQL进行备份的。
2,我司平台数据库备份策略
平台MySQL数据当前部署在A和B两台服务器上,两台互为主备实时同步数据,目前主用库是A。备份脚本和定时任务在两台服务上都有部署,定时任务在两台服务商每天都会执行,但是脚本策略只会对主用库进行备份,判断的主要依据是MySQL数据的浮动IP在那个主机上。
全量备份
#全量备份脚本
#!/bin/bash
HOMEDIR=/opt/DB_backup
#判断全量备份的保存目录,日志目录是否存在,不存在的话新建
[ -d /opt/DB_backup/full_bakcup ] || mkdir -p /opt/DB_backup/full_bakcup
[ -d /opt/DB_backup/logs ] || mkdir -p /opt/DB_backup/logs
#设置备份日志
logfile=`date "+%Y%m%d"`_fullbackup.log
touch $HOMEDIR/logs/$logfile
#判断浮动IP是否启动在此主机上
/sbin/ip addr | grep 192.168.1.10
if [ $? -ne 0 ];then
echo "This is not the master DB, no need to backup." >> $HOMEDIR/logs/$logfile
exit 3
fi
#检查mysql是否运行正常
/usr/sbin/lsof -i:3306 | grep LISTEN
if [ $? -ne 0 ];then
echo "MySQL may not working correctly, can not find LISTEN for mysql." >> $HOMEDIR/logs/$logfile
exit 5
fi
ps -ef | grep /home/mysql/bin/mysqld
if [ $? -ne 0 ];then
echo "MySQL may not working correctly, can not find pid for mysql." >> $HOMEDIR/logs/$logfile
exit 7
fi
#执行全量备份
/usr/bin/innobackupex --defaults-file=/etc/my.cnf --user=root --password="abc@123" /opt/DB_backup/full_bakcup/ &>> $HOMEDIR/logs/$logfile
[ $? -eq 0 ] && echo "Full backup successful." >> $HOMEDIR/logs/$logfile
#将前一周的增量备份转移到/home的临时保存目录
tmp_back_name="`date -d "-7 days" +"%Y%m%d"`-`date -d "-1 days" +"%Y%m%d"`"
tar -zcvf /home/old_DB_backup/$tmp_back_name.tar.gz /opt/DB_backup/incremental_backup/201* &>> $HOMEDIR/logs/$logfile && rm -rf /opt/DB_backup/incremental_backup/* &>> $HOMEDIR/logs/$logfile && echo "$tmp_back_name 的增量备份转移到/home的临时保存目录成功." >> $HOMEDIR/logs/$logfile
增量备份
增量备份同样通过MySQL浮动IP判断该主机是不是主用库,然后再判定MySQL的运行状态是否正常。上述判定通过会再对依赖的全量备份目录进行判定,如果该主机没有进行过全量备份,或者最近的一次全量备份是7天前的,则不会进行增量备份,转而进行全量备份。每天增量备份均使用周一的全量备份为base, 这样虽然会多占用一些磁盘空间,但是恢复时则只需要执行两次恢复动作即可。
#增量备份脚本
#!/bin/bash
#本脚本目的用于每天一次MySQL增量备份
HOMEDIR=/opt/DB_backup
#判断增量备份的保存目录,日志目录是否存在,不存在的话新建
[ -d /opt/DB_backup/incremental_backup ] || mkdir -p /opt/DB_backup/incremental_backup
[ -d /opt/DB_backup/logs ] || mkdir -p /opt/DB_backup/logs
#设置备份日志
logfile=`date "+%Y%m%d"`_incremental_backup.log
touch $HOMEDIR/logs/$logfile
#判断主机是不是mysql的主用库,如果不是主库则退出脚本的运行.
/sbin/ip addr | grep 192.168.1.10
if [ $? -ne 0 ];then
echo "This is not the master DB, no need to backup." >> $HOMEDIR/logs/$logfile
exit 3
fi
#检查mysql是否运行正常
/usr/sbin/lsof -i:3306 | grep LISTEN
if [ $? -ne 0 ];then
echo "MySQL may not working correctly, can not find LISTEN for mysql." >> $HOMEDIR/logs/$logfile
exit 5
fi
ps -ef | grep /home/mysql/bin/mysqld
if [ $? -ne 0 ];then
echo "MySQL may not working correctly, can not find pid for mysql." >>$HOMEDIR/logs/$logfile
exit 7
fi
#确认增量备份所依赖的全量备份最新的目录
basedir=`ls -lrt /opt/DB_backup/full_bakcup | tail -1 | awk '{print $9}'`
#判定如果该主机未执行过全灵备份,则执行全量备份,取消增量备份
if [ "$basedir" == "" ];then
echo "This DB dosen't has a full backup before, cannot execute incremental backup." >>$HOMEDIR/logs/$logfile
echo "Begin to execute full backup." >>$HOMEDIR/logs/$logfile
sh /opt/DB_backup/shell/full_backup.sh
if [ $? -eq 0 ];then
echo "The first full backup on this server is successful, the incremental backup will begin at tomorrow.">>$HOMEDIR/logs/$logfile
exit
fi
exit 77
fi
#确认该主机已存在的全量备份是否早于7天,如果不是7天内的全量备份,则执行全量备份.
T1=`date +"%Y-%m-%d"`
T2=`echo "$basedir" |awk -F_ '{print $1}'`
Day1=`date +%s -d "$T1"`
Day2=`date +%s -d "$T2"`
Day3=$((($Day1-$Day2)/86400))
if [ $Day3 -gt 7 ];then
echo "The lastest full backup is 7 days ago, begin to execute full backup." >>$HOMEDIR/logs/$logfile
sh /opt/DB_backup/shell/full_backup.sh
if [ $? -eq 0 ];then
echo "exec full backup successful, pls check full backup logs.">>$HOMEDIR/logs/$logfile
exit
fi
exit 99
fi
#将全量备份的目录打印到日志
echo "The basedir is $basedir" >> $HOMEDIR/logs/$logfile
#执行增量备份
/usr/bin/innobackupex --defaults-file=/etc/my.cnf --user=root --password="abc@123" --incremental --incremental-basedir=/opt/DB_backup/full_bakcup/$basedir /opt/DB_backup/incremental_backup >> $HOMEDIR/logs/$logfile
[ $? -eq 0 ] && echo "Incremental backup successful." >> $HOMEDIR/logs/$logfile
3,恢复方案
数据的恢复操作是比较简单的
全量恢复
#先prepare,利用--apply-log的作用是通过回滚未提交的事务及同步已经提交的事务至数据文件使数据文件处于一致性状态
innobackupex --apply-log /opt/DB_backup/full_bakcup/2019-05-09_10-45-32
#还原操作需要清空源库的所有数据,并删除原始数据目录下的数据文件
mysql> drop database xxx;
rm -rf ib_logfile0 ib_logfile1 ibdata1
#恢复命令
innobackupex --defaults-file=/etc/mysql/my.cnf --copy-back /opt/DB_backup/full_bakcup/2019-05-09_10-45-32
增量恢复
#如果是数据只是部分数据有损,在确定有损数据范围后,可直接还原增量即可
innobackupex --apply-log /opt/DB_backup/full_bakcup/2019-05-09_10-45-32 --incremental-dir=/home/DB_backup/incremental_backup/2019-05-11_03-00-00
#如果是数据全部损毁,执行如下操作
#1,先还原全量
innobackupex --defaults-file=/etc/mysql/my.cnf --copy-back /opt/DB_backup/full_bakcup/2019-05-09_10-45-32
#2,在还原增量
innobackupex --apply-log /opt/DB_backup/full_bakcup/2019-05-09_10-45-32 --incremental-dir=/home/DB_backup/incremental_backup/2019-05-11_03-00-00