欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

基于Innobackupex的增备及恢复

程序员文章站 2022-05-13 20:17:50
...

MySQL的热备(物理备份)可以采取全备加增量备份的方式来减轻数据库I/O压力及系统资源的占用。增量备份主要是以全备或增量备份为基

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 modied, this number gets incremented. An incremental backup copies all pages since a specic 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、创建演示环境
robin@localhost[(none)]> create database tempdb;

robin@localhost[(none)]> use tempdb;

robin@localhost[tempdb]> create table tb(id smallint,val varchar(20));

robin@localhost[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

--再新增一条记录,以便区分全备与增备
robin@localhost[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