MySQL的热备(物理备份)可以采取全备加增量备份的方式来减轻数据库I/O压力及系统资源的占用。增量备份主要是以全备或增量备份为基础,备份那些变更过的页面。其备份的原理是基于一个不断增长的LSN序列,这个LSN与Oracle的SCN类似。在恢复期间,我们需要将已提交的事务前滚,未提交的事务回滚。本文主要描述了增量备份及增量恢复。
1、增备的相关知识点
As not all information changes between each backup, the incremental backup strategy uses this to reduce the storage needs and the duration of making a backup. This can be done because each InnoDB page has a log sequence number, LSN, which acts as a version number of the entire database. Every time the database is modi?ed, this number gets incremented. An incremental backup copies all pages since a speci?c LSN. Once the pages have been put together in their respective order, applying the logs will recreate the process that affected the database, yielding the data at the moment of the most recently created backup.
增备是备份上次以来发生变化的页面,通过增备可以减轻存储以及系统资源开销。增量备份主要针对于InnoDB,因为InnoDB采用了日志序列号(LSN)的方式。InnoDB的LSN是一个增长的序列,类似于Oracle的SCN,记录了InnoDB的变化情况。增量备份则是备份特定的LSN之后变化的情况。通过按序重组这些LSN即可将数据库恢复到故障点或任意时刻。
innobackupex --incremental /data/backups --incremental-lsn=1291135
innobackupex --incremental /data/backups --incremental-lsn=1358967
如上,我们可以使用--incremental-lsn选项来实施增量备份
Warning: This procedure only affects XtraDB or InnoDB-based tables. Other tables with a different storage engine, e.g. MyISAM, will be copied entirely each time an incremental backup is performed.
对于非XtraDB或者InnoDB存储引擎,热备方式依旧会全部备份所有的数据文件,索引文件,格式文件等。
Preparing an Incremental Backup with innobackupex Preparing incremental backups is a bit different than full ones. This is, perhaps, the stage where more attention is needed:
? First, only the committed transactions must be replayed on each backup. This will merge the base full backup with the incremental ones.
? Then, the uncommitted transaction must be rolled back in order to have a ready-to-use backup.
对于增量备份的Prepare阶段,有2个需要注意的地方,一个是提交的事务需要replayed,一个未提交的事务需要rollback。
If you replay the committed transactions and rollback the uncommitted ones on the base backup, you will not be able to add the incremental ones. If you do this on an incremental one, you won’t be able to add data from that moment and the remaining increments. Having this in mind, the procedure is very straight-forward using the --redo-only option, starting with the base backup:
如果在Prepare阶段replay了已提交的事务以及回滚了未提交的事务,则后续的增量备份无法添加到当前全备。因此在Prepare阶段全备应使用--redo-only选项。
--redo-only should be used when merging all incrementals except the last one. That’s why the previous line doesn’t contain the --redo-only option. 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.
对于存在多次增量的情形,仅仅只有最后一个增量不需要使用--redo-only 选项。如果使用了的话,rollback将由服务器启动的时候来完成。
2、演示增量备份
a、创建演示环境 [email protected][(none)]> create database tempdb; [email protected][(none)]> use tempdb; [email protected][tempdb]> create table tb(id smallint,val varchar(20)); [email protected][tempdb]> insert into tb values(1,‘fullbak‘); b、启动一个全备 SHELL> innobackupex --user=robin -password=xxx --port=3606 --socket=/tmp/mysql3606.sock --defaults-file=/etc/my3606.cnf > /hotbak/full --no-timestamp --再新增一条记录,以便区分全备与增备 [email protected][tempdb]> insert into tb values(2,‘Incbak‘); c、启动一个增量备份 SHELL> innobackupex --user=robin -password=xxx --port=3606 --socket=/tmp/mysql3606.sock --defaults-file=/etc/my3606.cnf > --incremental /hotbak/inc --incremental-basedir=/hotbak/full --no-timestamp --........非重要信息忽略........ innobackupex: Using mysql server version 5.6.12-log --当前mysql的版本 innobackupex: Created backup directory /hotbak/inc -- 创建备份目录 innobackupex: Suspend file ‘/hotbak/inc/xtrabackup_suspended_2‘ -- 同时会创建相应的suspended目录供临时使用 --........非重要信息忽略........ xtrabackup: using the full scan for incremental backup [01] Copying ./ibdata1 to /hotbak/inc/ibdata1.delta --可以看到生产了相应的delta文件,即增量部分 [01] ...done >> log scanned up to (391476794) xtrabackup: Creating suspend file ‘/hotbak/inc/xtrabackup_suspended_2‘ with pid ‘25001‘ --........非重要信息忽略........ 141222 14:55:08 innobackupex: Executing FLUSH TABLES WITH READ LOCK... --这个主要是针对非innodb 141222 14:55:08 innobackupex: All tables locked and flushed to disk 141222 14:55:08 innobackupex: Starting to backup non-InnoDB tables and files --开始备份非innodb innobackupex: in subdirectories of ‘/data/inst3606/data3606‘ --所有的非innodb会被重新备份一次 innobackupex: Backing up files ‘/data/inst3606/data3606/mysql/*.{frm,isl,MYD,...,CSV,opt,par}‘ (77 files) 141222 14:55:09 innobackupex: Executing FLUSH ENGINE LOGS... --日志切换 141222 14:55:09 innobackupex: Waiting for log copying to finish xtrabackup: The latest check point (for incremental): ‘391476794‘ --检查点位置 xtrabackup: Stopping log copying thread. .>> log scanned up to (391476794) --........非重要信息忽略........ xtrabackup: Creating suspend file ‘/hotbak/inc/xtrabackup_log_copied‘ with pid ‘25001‘ xtrabackup: Transaction log of lsn (391476794) to (391476794) was copied. --复制事务日志 141222 14:55:10 innobackupex: All tables unlocked --表解锁 --........非重要信息忽略........ 141222 14:55:10 innobackupex: completed OK! d、查看增备后的相关文件 SHELL> ls -hltr /hotbak/inc/*delta* -rw-r----- 1 root root 96K 2014/12/22 14:55 /hotbak/inc/ibdata1.delta SHELL> more /hotbak/inc/xtrabackup_info|grep ^incremental incremental = Y --文件xtrabackup_checkpoints包含了备份的相关检查点信息 SHELL> more /hotbak/inc/xtrabackup_checkpoints backup_type = incremental from_lsn = 391476482 to_lsn = 391476794 last_lsn = 391476794 compact = 0 --文件xtrabackup_binlog_info包含了binlog的位置 SHELL> more xtrabackup_binlog_info inst3606bin.000010 874 --再次新增一条记录,看看增备能否恢复 [email protected][tempdb]> insert into tb values(3,‘Inbinlog‘); Query OK, 1 row affected (0.00 sec)
3、恢复增量备份
a、先做基于全备的apply,注意,此时使用了--redo-only SHELL> innobackupex --apply-log --redo-only --user=robin -password=xxx --port=3606 > --defaults-file=/etc/my3606.cnf /hotbak/full b、基于增备的apply, --此时没有--redo-only,如果有多个增备,仅仅最后一个增备无需指定--redo-only SHELL> innobackupex --apply-log --user=robin -password=xxx --port=3606 --defaults-file=/etc/my3606.cnf > /hotbak/full --incremental-dir=/hotbak/inc --........非重要信息忽略........ xtrabackup: page size for /hotbak/inc/tempdb/tb.ibd.delta is 16384 bytes Applying /hotbak/inc/tempdb/tb.ibd.delta to ./tempdb/tb.ibd... --可以看到数据库tempdb下表tb被apply到全备目录 xtrabackup: page size for /hotbak/inc/sakila/t.ibd.delta is 16384 bytes Applying /hotbak/inc/sakila/t.ibd.delta to ./sakila/t.ibd... -- ........非重要信息忽略........ xtrabackup: Starting InnoDB instance for recovery. --启动InnoDB实例恢复 xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter) --........非重要信息忽略........ InnoDB: Highest supported file format is Barracuda. InnoDB: The log sequence numbers 391476482 and 391476482 in ibdata files do not match the log sequence number 391476794 in the ib_logfiles! --提示log sequence不一致 InnoDB: Database was not shutdown normally! InnoDB: Starting crash recovery. --启动crash recovery InnoDB: Reading tablespace information from the .ibd files... InnoDB: Restoring possible half-written data pages InnoDB: from the doublewrite buffer... InnoDB: Last MySQL binlog file position 0 874, file name inst3606bin.000010 InnoDB: 128 rollback segment(s) are active. --提示有一些rollback段是活动的 InnoDB: Waiting for purge to start InnoDB: 5.6.21 started; log sequence number 391476794 --........非重要信息忽略........ Author: Leshami Blog: http://blog.csdn.net/leshami innobackupex: Starting to copy non-InnoDB files in ‘/hotbak/inc‘ innobackupex: to the full backup directory ‘/hotbak/full‘ --将非InnoDB表复制到全备目录,覆盖方式 innobackupex: Copying ‘/hotbak/inc/xtrabackup_binlog_info‘ to ‘/hotbak/full/xtrabackup_binlog_info‘ ........非重要信息忽略........ 141222 16:19:52 innobackupex: completed OK! --完成基于增量的合并 c、进行copy back SHELL> mv /data/inst3606/data3606 /data/inst3606/data3606bk SHELL> mkdir -p /data/inst3606/data3606 SHELL> mysqldown -P3606 --copy back前关闭实例 SHELL> netstat -nltp|grep mysql|grep 3606 SHELL> innobackupex --user=robin -password=xxx --port=3606 --copy-back /hotbak/full --defaults-file=/etc/my3606.cnf SHELL> chown -R mysql:mysql /data/inst3606/data3606 --启动恢复后的实例 SHELL> mysqld_safe --user=mysql --defaults-file=/etc/my3606.cnf & --验证结果 [[email protected] inst3606]$ sql -P3606 --如下查询,由于我们未使用binlog做完全恢复,因此无法查询到id为3的记录 [email protected][(none)]> select * from tempdb.tb; +------+---------+ | id | val | +------+---------+ | 1 | fullbak | | 2 | Incbak | +------+---------+
4、小结
a、增量备份是基于增量或全备的基础之上完成的。
b、增量备份的基础是InnoDB引擎使用了LSN机制,非InnoDB引擎不存在增量备份的说法,每次都是全备。
c、对于增量备份的恢复期间需要对已提交的事务前滚,未提交的事务回滚。
d、增量备份的恢复应按照备份的顺利逐个逐个replay,需要使用--apply-log --redo-only选项。
e、仅仅最后一个增量备份不需要使用--redo-only选项。
f、如果要做完全恢复或时点恢复,需要结合binlog来实现。