首页 > 代码库 > Innobackupex(全备+增量)备份恢复

Innobackupex(全备+增量)备份恢复

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将由服务器启动的时候来完成。


二、 演示
1. 准备实验环境
mysql> select version();
+------------+
| version()  |
+------------+
| 5.6.25-log |
+------------+
1 row in set (0.00 sec)
mysql> create database inc_rec;
Query OK, 1 row affected (0.00 sec)
mysql> use inc_rec;
Database changed
mysql> create table andy (id int);
Query OK, 0 rows affected (0.08 sec)
mysql> insert into andy values(1),(2);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
2. 全备
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=oracle --port=3606 /xtrabackup/full/
xtrabackup: Transaction log of lsn (1648193) to (1648193) was copied.
170609 03:53:56 completed OK!
3. 查看全备生成文件
[root@mysql02 full]# ll /xtrabackup/full/
total 4
drwxr-x---. 6 root root 4096 Jun  9 03:53 2017-06-09_03-53-51
4. 模拟业务新数据
mysql> insert into andy values(3),(4);
Query OK, 2 rows affected (0.14 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
5. 增量备份
-- 创建存放增量备份目录并赋权
[root@mysql02 full]# mkdir -p /xtrabackup/increament/
[root@mysql02 full]# chown -R mysql:mysql /xtrabackup/increament/
[root@mysql02 full]# ll /xtrabackup/
total 8
drwxr-xr-x. 3 mysql mysql 4096 Jun  9 03:53 full
drwxr-xr-x. 2 mysql mysql 4096 Jun  9 04:00 increament
-- 正式开始增量备份
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=oracle  --incremental  \
--incremental-basedir=/xtrabackup/full/2017-06-09_03-53-51/ /xtrabackup/increament/
########################################下面是增量备份输出
170609 04:02:21 Backup created in directory ‘/xtrabackup/increament/2017-06-09_04-02-16/‘
MySQL binlog position: filename ‘binlog.000003‘, position ‘894‘
。。。省略
xtrabackup: Transaction log of lsn (1652210) to (1652210) was copied.
170609 04:02:22 completed OK!


补充:
[root@mysql02 2017-06-09_04-02-16]# pwd
/xtrabackup/increament/2017-06-09_04-02-16
-- 查看增备产生的相关文件  
[root@mysql02 2017-06-09_04-02-16]# ll
total 8580
-rw-r-----. 1 root root     418 Jun  9 04:02 backup-my.cnf
-rw-r-----. 1 root root  360448 Jun  9 04:02 ibdata1.delta
-rw-r-----. 1 root root      44 Jun  9 04:02 ibdata1.meta
drwxr-x---. 2 root root    4096 Jun  9 04:02 inc_rec
drwxr-x---. 2 root root    4096 Jun  9 04:02 mysql
drwxr-x---. 2 root root    4096 Jun  9 04:02 performance_schema
drwxr-x---. 2 root root    4096 Jun  9 04:02 test
-rw-r-----. 1 root root      18 Jun  9 04:02 xtrabackup_binlog_info
-rw-r-----. 1 root root     117 Jun  9 04:02 xtrabackup_checkpoints
-rw-r-----. 1 root root     584 Jun  9 04:02 xtrabackup_info
-rw-r-----. 1 root root 8388608 Jun  9 04:24 xtrabackup_logfile
-- 文件 xtrabackup_info 含有备份类型
[root@mysql02 2017-06-09_04-02-16]# more xtrabackup_info|grep ^incremental 
incremental = Y
-- 文件xtrabackup_checkpoints包含了备份的相关检查点信息  
[root@mysql02 2017-06-09_04-02-16]# more xtrabackup_checkpoints  
backup_type = incremental
from_lsn = 1648193
to_lsn = 1652210
last_lsn = 1652210
compact = 0
recover_binlog_info = 0
-- 文件xtrabackup_binlog_info包含了binlog的位置  
[root@mysql02 2017-06-09_04-02-16]# more xtrabackup_binlog_info  
binlog.000003 894
6. 误操作,truncate表
mysql> truncate table andy;
Query OK, 0 rows affected (0.22 sec)
mysql> select * from andy;
Empty set (0.01 sec)
7. 停止mysql数据库
[root@mysql02 ~]# service mysql stop
[root@mysql02 ~]# ps -ef|grep mysql
8.恢复数据库  (先恢复全备,再按增量备份时间先后顺序,依次恢复增量备份)
8.1 先恢复完整的备份集:
[root@mysql02 full]#  innobackupex --defaults-file=/etc/my.cnf --user=root --apply-log --redo-only /xtrabackup/full/2017-06-09_03-53-51/
170609 04:19:30 completed OK!
8.2 在恢复增量备份集:   (如果有多份增量备份,最好最后一份增量不用 --redo-only , 其他的都用 --redo-only)
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --apply-log  /xtrabackup/full/2017-06-09_03-53-51  --incremental-dir=/xtrabackup/increament/2017-06-09_04-02-16/
170609 04:24:45 completed OK!  #结果出现completed OK表示完全成功
说明: /xtrabackup/full/2017-06-09_03-53-51 为全备基目录 , incremental-dir 为增量备份目录
9.将原有文件夹重命名到新位置,并创建原文件夹 
[root@mysql02 full]# mv /data/mysql /data/mysqlbak
[root@mysql02 full]# mkdir -p /data/mysql
10.执行拷贝恢复的文件到原来的数据位置
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --copy-back  /xtrabackup/full/2017-06-09_03-53-51/
170609 04:33:06 completed OK! #结果出现completed OK表示完全成功
说明: /xtrabackup/full/2017-06-09_03-53-51/ 为全备基目录
11. 权限修改
[root@mysql02 ~]# mkdir -p /data/mysql/binarylog (说明:这里我binlog在datadir在路径下,所以要单独为binlog创建目录)
chown -R mysql:mysql /data/mysql 
12. 启动被恢复的实例
[root@mysql02 mysql]# mysqld_safe --defaults-file=/etc/my.cnf & 
[root@mysql02 ~]# mysql -uroot -poracle
mysql> use inc_rec;
mysql> select * from andy;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |     > 恢复成功!
|    4 |
+------+
总结:
a、增量备份是基于增量或全备的基础之上完成的。
b、增量备份的基础是InnoDB引擎使用了LSN机制,非InnoDB引擎不存在增量备份的说法,每次都是全备。
c、对于增量备份的恢复期间需要对已提交的事务前滚,未提交的事务回滚。
d、增量备份的恢复应按照备份的顺利逐个逐个replay,需要使用--apply-log --redo-only选项。
e、仅仅最后一个增量备份不需要使用--redo-only选项。
f、如果要做完全恢复或时点恢复,需要结合binlog来实现。
参考:http://blog.csdn.net/leshami/article/details/42141627    感谢大师

Innobackupex(全备+增量)备份恢复