Xtrabackup是由percona提供的mysql数据库备份工具,支持在线热备份(备份时不影响数据读写).
Xtrabackup有两个主要的工具:xtrabackup、innobackupex
1、xtrabackup只能备份InnoDB和XtraDB两种数据表,而不能备份MyISAM数据表
2、innobackupex是参考了InnoDB Hotbackup的innoback脚本修改而来的.innobackupex是一个perl脚本封装,封装了xtrabackup。主要是为了方便的 同时备份InnoDB和MyISAM引擎的表,但在处理myisam时需要加一个读锁。并且加入了一些使用的选项。如slave-info可以记录备份恢 复后,作为slave需要的一些信息,根据这些信息,可以很方便的利用备份来重做slave。
安装:
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-release-0.1-4.noarch.rpm yum install percona-xtrabackup-24 # 可使用innobackupex -verion检查版本 若出现版本号则安装成功 # innobackupex --help 可查看参数帮助
备份前准备:
修改InnoDB为独立表空间模式,即在my.cnf中[mysqld]下设置innodb_file_per_table=1
建议创建一个单独的备份目录 例:/xbackup
全备:
innobackupex --defaults-file=/etc/my.cnf --user=root --password='yourpassword' /xbackup # 出现completed OK!则代表备份成功,文件会保存至一个以时间戳命名的目录内。
恢复:
若全部恢复,则需要先停止mysql服务,还需确保mysqldata目录下无文件
service mysqld stop innobackupex --apply-log /xbackup/2017-09-07_09-50-11/ # apply-log称作准备阶段,是为了保持数据一致性,回滚备份过程中未提交的事务,提交已提交的事务 innobackupex --defaults-file=/etc/my.cnf --copy-back /xbackup/2017-09-07_09-50-11/ chown -R mysql:mysql /mysqldata service mysqld start
单表恢复
innobackupex --apply-log --export /xbackup/2017-09-07_15-53-53/ # 若t1表数据误删 确保表结构存在 ALTER TABLE t1 DISCARD TABLESPACE; cp /xbackup/2017-09-07_15-53-53/test/t1.{ibd,exp,cfg} /mysqldata/test/ chown -R mysql:mysql /mysqldata ALTER TABLE t1 IMPORT TABLESPACE;
增量备份与恢复:
innobackupex --defaults-file=/etc/my.cnf --user=root --password='xxxxxx' --no-timestamp --incremental /xbackup/inc1 --incremental-basedir=/xbackup/2017-09-07_09-50-11 # 恢复 service mysqld stop innobackupex --apply-log /xbackup/2017-09-07_09-50-11/ --incremental-dir=/xbackup/inc1/ innobackupex --copy-back /xbackup/2017-09-07_09-50-11/ chown -R mysql:mysql /mysqldata service mysqld start
Xtrabackup备份及恢复脚本
此备份脚本的策略是每周日和周三进去全备 其余每天增量备份。
# 备份脚本XtraBackup.sh: #!/bin/bash # filename : XtraBackup.sh # Author : wang day=`date +%w` dt=`date +%Y%m%d` lastday=`date -d '1 days ago' +%Y%m%d` user=root pwd='xxxxx' log=backuplog.`date +%Y%m%d` case $day in 0) # Sunday Full backup find /backup/ -name "xtra_*" -mtime +6 -exec rm -rf {} \; innobackupex --defaults-file=/etc/my.cnf --user=$user --password=$pwd --no-timestamp /backup/xtra_base_$dt > /tmp/$log 2>&1 ;; 1) # Monday Relatively Sunday's incremental backup innobackupex --defaults-file=/etc/my.cnf --user=$user --password=$pwd --no-timestamp --incremental /backup/xtra_inc_$dt --incremental-basedir=/backup/xtra_base_$lastday > /tmp/$log 2>&1 ;; 2) # Tuesday Compared with Monday's incremental backup innobackupex --defaults-file=/etc/my.cnf --user=$user --password=$pwd --no-timestamp --incremental /backup/xtra_inc_$dt --incremental-basedir=/backup/xtra_inc_$lastday > /tmp/$log 2>&1 ;; 3) # Wednesday Full backup find /backup/ -name "xtra_*" -mtime +6 -exec rm -rf {} \; innobackupex --defaults-file=/etc/my.cnf --user=$user --password=$pwd --no-timestamp /backup/xtra_base_$dt > /tmp/$log 2>&1 ;; 4) # Thursday Relatively Wednesday's incremental backup innobackupex --defaults-file=/etc/my.cnf --user=$user --password=$pwd --no-timestamp --incremental /backup/xtra_inc_$dt --incremental-basedir=/backup/xtra_base_$lastday > /tmp/$log 2>&1 ;; 5) # Friday Compared with Thursday's incremental backup innobackupex --defaults-file=/etc/my.cnf --user=$user --password=$pwd --no-timestamp --incremental /backup/xtra_inc_$dt --incremental-basedir=/backup/xtra_inc_$lastday > /tmp/$log 2>&1 ;; 6) # Saturday Compared with Friday's incremental backup innobackupex --defaults-file=/etc/my.cnf --user=$user --password=$pwd --no-timestamp --incremental /backup/xtra_inc_$dt --incremental-basedir=/backup/xtra_inc_$lastday > /tmp/$log 2>&1 ;; esac find /tmp -mtime +6 -type f -name 'backuplog.*' -exec rm -rf {} \;
# 全库恢复脚本 xtrabackup_recover.sh: #!/bin/bash # filename : xtrabackup_recover.sh # Author : wang day=`date +%w` dt=`date +%Y%m%d` lastday=`date -d '1 days ago' +%Y%m%d` lasttwoday=`date -d '2 days ago' +%Y%m%d` lastthreeday=`date -d '3 days ago' +%Y%m%d` user=root pwd='xxxxxxx' log=recoverlog.`date +%Y%m%d` datefile=/mysqldata case $day in 0) # Sunday Recover Database innobackupex --apply-log /backup/xtra_base_$dt > /tmp/$log 2>&1 service mysqld stop rm -rf $datefile/* innobackupex --defaults-file=/etc/my.cnf --copy-back /backup/xtra_base_$dt >> /tmp/$log 2>&1 chown -R mysql:mysql $datefile service mysqld start binlog=`cat /backup/xtra_base_$dt/xtrabackup_binlog_info|awk '{print $1}'` pos=`cat /backup/xtra_base_$dt/xtrabackup_binlog_info|awk '{print $2}'` mysqlbinlog --no-defaults --start-position=$pos /mysqllog/$binlog | mysql -u$user -p$pwd ;; 1) # Monday Recover Database innobackupex --apply-log --redo-only /backup/xtra_base_$lastday > /tmp/$log 2>&1 innobackupex --apply-log /backup/xtra_base_$lastday/ --incremental-dir=/backup/xtra_inc_$dt/ >> /tmp/$log 2>&1 innobackupex --apply-log /backup/xtra_base_$lastday >> /tmp/$log 2>&1 service mysqld stop rm -rf $datefile/* innobackupex --defaults-file=/etc/my.cnf --copy-back /backup/xtra_base_$lastday >> /tmp/$log 2>&1 chown -R mysql:mysql $datefile service mysqld start binlog=`cat /backup/xtra_base_$lastday/xtrabackup_binlog_info|awk '{print $1}'` pos=`cat /backup/xtra_base_$lastday/xtrabackup_binlog_info|awk '{print $2}'` mysqlbinlog --no-defaults --start-position=$pos /mysqllog/$binlog | mysql -u$user -p$pwd ;; 2) # Tuesday Recover Database innobackupex --apply-log --redo-only /backup/xtra_base_$lasttwoday > /tmp/$log 2>&1 innobackupex --apply-log --redo-only /backup/xtra_base_$lasttwoday/ --incremental-dir=/backup/xtra_inc_$lastday/ >> /tmp/$log 2>&innobackupex --apply-log /backup/xtra_base_$lasttwoday/ --incremental-dir=/backup/xtra_inc_$dt/ >> /tmp/$log 2>&1 innobackupex --apply-log /backup/xtra_base_$lasttwoday >> /tmp/$log 2>&1 service mysqld stop rm -rf $datefile/* innobackupex --defaults-file=/etc/my.cnf --copy-back /backup/xtra_base_$lasttwoday >> /tmp/$log 2>&1 chown -R mysql:mysql $datefile service mysqld start binlog=`cat /backup/xtra_base_$lasttwoday/xtrabackup_binlog_info|awk '{print $1}'` pos=`cat /backup/xtra_base_$lasttwoday/xtrabackup_binlog_info|awk '{print $2}'` mysqlbinlog --no-defaults --start-position=$pos /mysqllog/$binlog | mysql -u$user -p$pwd ;; 3) # Wednesday Recover Database innobackupex --apply-log /backup/xtra_base_$dt > /tmp/$log 2>&1 service mysqld stop rm -rf $datefile/* innobackupex --defaults-file=/etc/my.cnf --copy-back /backup/xtra_base_$dt >> /tmp/$log 2>&1 chown -R mysql:mysql $datefile service mysqld start binlog=`cat /backup/xtra_base_$dt/xtrabackup_binlog_info|awk '{print $1}'` pos=`cat /backup/xtra_base_$dt/xtrabackup_binlog_info|awk '{print $2}'` mysqlbinlog --no-defaults --start-position=$pos /mysqllog/$binlog | mysql -u$user -p$pwd ;; 4) # Thursday Recover Database innobackupex --apply-log --redo-only /backup/xtra_base_$lastday > /tmp/$log 2>&1 innobackupex --apply-log /backup/xtra_base_$lastday/ --incremental-dir=/backup/xtra_inc_$dt/ >> /tmp/$log 2>&1 innobackupex --apply-log /backup/xtra_base_$lastday >> /tmp/$log 2>&1 service mysqld stop rm -rf $datefile/* innobackupex --defaults-file=/etc/my.cnf --copy-back /backup/xtra_base_$lastday >> /tmp/$log 2>&1 chown -R mysql:mysql $datefile service mysqld start binlog=`cat /backup/xtra_base_$lastday/xtrabackup_binlog_info|awk '{print $1}'` pos=`cat /backup/xtra_base_$lastday/xtrabackup_binlog_info|awk '{print $2}'` mysqlbinlog --no-defaults --start-position=$pos /mysqllog/$binlog | mysql -u$user -p$pwd ;; 5) # Friday Recover Database innobackupex --apply-log --redo-only /backup/xtra_base_$lasttwoday > /tmp/$log 2>&1 innobackupex --apply-log --redo-only /backup/xtra_base_$lasttwoday/ --incremental-dir=/backup/xtra_inc_$lastday/ >> /tmp/$log 2>&innobackupex --apply-log /backup/xtra_base_$lasttwoday/ --incremental-dir=/backup/xtra_inc_$dt/ >> /tmp/$log 2>&1 innobackupex --apply-log /backup/xtra_base_$lasttwoday >> /tmp/$log 2>&1 service mysqld stop rm -rf $datefile/* innobackupex --defaults-file=/etc/my.cnf --copy-back /backup/xtra_base_$lasttwoday >> /tmp/$log 2>&1 chown -R mysql:mysql $datefile service mysqld start binlog=`cat /backup/xtra_base_$lasttwoday/xtrabackup_binlog_info|awk '{print $1}'` pos=`cat /backup/xtra_base_$lasttwoday/xtrabackup_binlog_info|awk '{print $2}'` mysqlbinlog --no-defaults --start-position=$pos /mysqllog/$binlog | mysql -u$user -p$pwd ;; 6) # Saturday Recover Database innobackupex --apply-log --redo-only /backup/xtra_base_$lastthreeday > /tmp/$log 2>&1 innobackupex --apply-log --redo-only /backup/xtra_base_$lastthreeday/ --incremental-dir=/backup/xtra_inc_$lasttwoday/ >> /tmp/$log 2>&1 innobackupex --apply-log --redo-only /backup/xtra_base_$lastthreeday/ --incremental-dir=/backup/xtra_inc_$lastday/ >> /tmp/$log 2>&1 innobackupex --apply-log /backup/xtra_base_$lastthreeday/ --incremental-dir=/backup/xtra_inc_$dt/ >> /tmp/$log 2>&1 innobackupex --apply-log /backup/xtra_base_$lastthreeday >> /tmp/$log 2>&1 service mysqld stop rm -rf $datefile/* innobackupex --defaults-file=/etc/my.cnf --copy-back /backup/xtra_base_$lastthreeday >> /tmp/$log 2>&1 chown -R mysql:mysql $datefile service mysqld start binlog=`cat /backup/xtra_base_$lastthreeday/xtrabackup_binlog_info|awk '{print $1}'` pos=`cat /backup/xtra_base_$lastthreeday/xtrabackup_binlog_info|awk '{print $2}'` mysqlbinlog --no-defaults --start-position=$pos /mysqllog/$binlog | mysql -u$user -p$pwd ;; esac find /tmp -mtime +6 -type f -name 'recoverlog.*' -exec rm -rf {} \;