XtraBackup不停机不锁表热备mysql数据库

环境

  • mysql版本 Server version: 5.6.42-log MySQL Community Server
  • 数据库data目录 /data/mysql/
  • binlog日志目录 /data/bin_logs/
  • 备份目录 /data/backup/

完全备份脚本 (每日一次,00:10:00执行)

#!/bin/bash
base_dir=”/data/backup/`date +%F`”
[ -d ${base_dir} ] || mkdir ${base_dir}
[ -d ${base_dir}/full_`date +%F` ] && exit 0
innobackupex –defaults-file=/etc/my.cnf –user=root –password=’123456′ –no-timestamp ${base_dir}/full_`date +%F`
[ $? -eq 0 ] || echo “$(date +”%F_ %T”) 完全备份失败,请重新备份” >> /data/backup/backup.log

增量备份脚本 (每小时一次,xx:30:00执行)

#!/bin/bash
base_dir=”/data/backup/`date +%F`”
full_dir=”/data/backup/`date +%F`/full_`date +%F`”
inc_dir=”${base_dir}/inc_`date +%H`”
[ -d ${inc_dir} ] && exit 0
innobackupex –defaults-file=/etc/my.cnf –user=root –password=’123456′ –no-timestamp –incremental-basedir=${full_dir} –incremental ${inc_dir} &> /dev/null
[ $? -eq 0 ] || echo “$(date +”%F_ %T”) ${inc_dir}增量备份失败,请重新备份” >> /data/backup/backup.log

一键还原脚本

#!/bin/bash
base_dir=”/data/backup/`date +%F`”
full_dir=”${base_dir}/full_`date +%F`”
inc_dir=”${base_dir}/`ls ${base_dir} | tail -1`”
cp -a ${base_dir} /data/backup/`date +”%F-%T”`_bak
innobackupex –defaults-file=/etc/my.cnf –user=root –apply-log –redo-only ${full_dir}
innobackupex –defaults-file=/etc/my.cnf –user=root –apply-log –redo-only ${full_dir} –incremental-dir=${inc_dir}

service mysqld stop
mv /data/mysql /data/mysql_bak`date +”%F_%T”`
mkdir /data/mysql
innobackupex –defaults-file=/etc/my.cnf –user=root –copy-back ${full_dir}
chown -R mysql.mysql /data/mysql
service mysqld start

###binlog日志还原
###这里选择的是最后一个binlog日志,如果增量备份的一个小时内,有多个mysql-bin.0000xx生成,需要手动选择bin-log日志;
###还原执行13:30点后的所有sql命令
Hour=`basename ${inc_dir}|grep -o ‘[0-9]*’`
binlog=`ls /data/bin_logs/|grep [0-9]|tail -1`
mysqlbinlog –start-datetime=”`date +%F` ${Hour}:30:00″ /data/mysql/${binlog} | mysql -uroot -p ‘123456’

###还原执行13:30-14:10之间的所有sql命令
##mysqlbinlog –start-datetime=”2018-11-21 13:30:00″ –stop-datetime=”2012-03-15 14:10:00″ /data1/log/mysql/mysql-bin.000001 > /tmp/mysql_restore_030915.sql

计划任务

10 00 * * * /bin/bash /root/.scripts/full_back.sh
30 * * * * /bin/bash /root/.scripts/inc_back.sh

原创文章,作者:kinpo, k,如若转载,请注明出处:https://www.yidc.net/archives/5351