从库做增量备份


Innobackup 的介绍详见 -- Mysql增备工具--Xtrabackup


1.全备
Slave$ innobackupex --no-timestamp --slave-info --safe-slave-backup /data/backup/full/

Note:
--save-info : It prints the binary log position and name of the master server. It also writes this information to the xtrabackup_slave_info file as a CHANGE MASTER statement
--safe-slave-backup :this option stops the slave SQL thread and wait to start backing up until Slave_open_temp_tables in SHOW STATUS is zero

2.增备
第一次增备
Master$ mysql -uroot -pqwer1234 -e 'create database kk;'
Master$ mysql -uroot -pqwer1234 -e 'use kk;create table k_1 as select * from information_schema.tables;'
Slave$ mysql -uroot -pqwer1234 -e 'use kk;show tables;'
Slave$ innobackupex -no-timestamp --slave-info --safe-slave-backup --incremental --incremental-basedir=/data/backup/full/ /data/backup/inc1

第二次增备
Master$ mysql -uroot -pqwer1234 -e 'use kk;create table k_2 as select * from information_schema.tables;'
Slave$ mysql -uroot -pqwer1234 -e 'use kk;show tables;'
Slave$ innobackupex -no-timestamp --slave-info --safe-slave-backup --incremental --incremental-basedir=/data/backup/inc1/ /data/backup/inc2

第三次增量
Master$ mysql -uroot -pqwer1234 -e 'use kk;create table k_3 as select * from information_schema.tables;'
Master$ mysql -uroot -pqwer1234 -e 'use kk;create table k_4 engine='Myisam' as select * from information_schema.tables;'
Slave$ mysql -uroot -pqwer1234 -e 'use kk;show tables;'
Slave$ innobackupex -no-timestamp --slave-info --safe-slave-backup --incremental --incremental-basedir=/data/backup/inc2/ /data/backup/inc3

3.恢复 从库上做

第一次增备恢复
/etc/init.d/mysql stop
rm -rf /var/lib/mysql/*
rm -rf /data/backup
cp -rf /data/backup--bak /data/backup

“准备”(prepare)增量备份与整理完全备份有着一些不同,尤其要注意的是:
(1)需要在每个备份(包括完全和各个增量备份)上,将已经提交的事务进行“重放”。“重放”之后,所有的备份数据将合并到完全备份上。
(2)基于所有的备份将未提交的事务进行“回滚”。
于是,命令中需要加上--redo-only,如下:

innobackupex --apply-log --redo-only /data/backup/full/

innobackupex --apply-log /data/backup/full --incremental --incremental-dir=/data/backup/inc1

innobackupex --apply-log /data/backup/full ## 恢复前的准备操作,主要做如下两个操作

replay committed transactions and rolled back uncommitted transaction

innobackupex --copy-back /data/backup/full ## 拷贝数据到数据库目录
chown -R mysql.mysql /var/lib/mysql
/etc/init.d/mysql start
mysql -uroot -pqwer1234 -e 'use kk;show tables;'
+--------------+
| Tables_in_kk |
+--------------+
| k_1 |
+--------------+

第二次增备恢复
/etc/init.d/mysql stop
rm -rf /var/lib/mysql/*
rm -rf /data/backup
cp -rf /data/backup--bak /data/backup
innobackupex --apply-log --redo-only /data/backup/full/
innobackupex --apply-log --redo-only /data/backup/full --incremental --incremental-dir=/data/backup/inc1
innobackupex --apply-log /data/backup/full --incremental --incremental-dir=/data/backup/inc2

# --redo-only should be used when merging all incrementals except the last one.
#Even if the --redo-only was used on the last step, backup would still be consistent but in that case server would perform the rollback phase

#If you merge the incrementals in the wrong order, the backup will be useless.

innobackupex --apply-log /data/backup/full
innobackupex --copy-back /data/backup/full
chown -R mysql.mysql /var/lib/mysql
/etc/init.d/mysql start
mysql -uroot -pqwer1234 -e 'use kk;show tables;'
+--------------+
| Tables_in_kk |
+--------------+
| k_1 |
| k_2 |
+--------------+

第三次增备恢复 (一般就只需要做这一步)
/etc/init.d/mysql stop
rm -rf /var/lib/mysql/*
rm -rf /data/backup
cp -rf /data/backup--bak /data/backup
innobackupex --apply-log --redo-only /data/backup/full/
innobackupex --apply-log --redo-only /data/backup/full --incremental --incremental-dir=/data/backup/inc1
innobackupex --apply-log --redo-only /data/backup/full --incremental --incremental-dir=/data/backup/inc2
innobackupex --apply-log /data/backup/full --incremental --incremental-dir=/data/backup/inc3
innobackupex --apply-log /data/backup/full
innobackupex --copy-back /data/backup/full
chown -R mysql.mysql /var/lib/mysql
/etc/init.d/mysql start
mysql -uroot -pqwer1234 -e 'use kk;show tables;'
+--------------+
| Tables_in_kk |
+--------------+
| k_1 |
| k_2 |
| k_3 |
| k_4 |
+--------------+


备注:

使用XtraBackup容易引起的一个经典错误,引起这个错误的原因也很简单,就是XtraBackup在备完innodb表的时候,要获得对所有表的锁定来备份MyISAM表和其他文件,来保证数据的一致性,如果在此过程中数据库不断的有DML操作,XtraBackup就可能迟迟无法获得对所有表的锁定,最后超时。
要避免这个问题,一个是加大超时的时间限制,但这显然不是一个好的解决办法,另外就是在备份的时候加上no-lock参数,就是在复制MyISAM表和.frm等文件的时候不锁表,但要保证这时没有对MyISAM表的DML操作和Innodb表的DDL操作,这一般通过应用端权衡时间是容易办到的。
但是要注意的是,如果备份出来的文件要用于主从复制,那么不锁表会导致没有输出 slave_info文件和binlog_info文件,这对于一些人的应用场景可能会有影响。这时可以加一个safe-slave-backup参数,使得在从库上备份的时候停止SQL THRED,这样即使从库能从主库接收binlog文件,但是不会应用,relay log position就不会移动了。但是单纯的停掉slave SQL Thread是不会影响binlog position的。

因为临时表的原因,safe-slave-backup需要在SHOW STATUS的输出中slave_open_temp_tables为0的时候才停止slave SQL Thread,否则会导致复制出问题。因为临时表是基于用户session的,因此如果正在操作临时表的数据的时候停止slave SQL Thread,会导致可能后续的数据不一致,在其他停用slave SQL Thread的场景也要注意这个问题。