使用innobackupex进行增量备份每个InnoDB的页面都会包含一个LSN信息,每当相关的数据发生改变,相关的页面的LSN就会自动增长。这正是InnoDB表可以进行增量备份的
使用innobackupex进行增量备份
每个InnoDB的页面都会包含一个LSN信息,每当相关的数据发生改变,相关的页面的LSN就会自动增长。这正是InnoDB表可以进行增量备份的基础,即innobackupex通过备份上次完全备份之后发生改变的页面来实现。
要实现第一次增量备份,可以使用下面的命令进行:
innobackupex --incremental /backup --incremental-basedir=BASEDIR
其中,BASEDIR指的是完全备份所在的目录,此命令执行结束后,innobackupex命令会在/backup目录中创建一个新的以时间命名的目录以存放所有的增量备份数据。另外,在执行过增量备份之后再一次进行增量备份时,其–incremental-basedir应该指向上一次的增量备份所在的目录。
需要注意的是,增量备份仅能应用于InnoDB或XtraDB表,对于MyISAM表而言,执行增量备份时其实进行的是完全备份。
“准备”(prepare)增量备份与整理完全备份有着一些不同,尤其要注意的是:
(1)需要在每个备份(包括完全和各个增量备份)上,将已经提交的事务进行“重放”。“重放”之后,所有的备份数据将合并到完全备份上。
(2)基于所有的备份将未提交的事务进行“回滚”。
于是,操作就变成了:
innobackupex --apply-log --redo-only BASE-DIR
接着执行:
innobackupex --apply-log --redo-only BASE-DIR --incremental-dir=INCREMENTAL-DIR-1
而后是第二个增量:
innobackupex --apply-log --redo-only BASE-DIR --incremental-dir=INCREMENTAL-DIR-2
其中BASE-DIR指的是完全备份所在的目录,而INCREMENTAL-DIR-1指的是第一次增量备份的目录,INCREMENTAL-DIR-2指的是第二次增量备份的目录,其它依次类推,即如果有多次增量备份,每一次都要执行如上操作;
备份过程:
完全备份
mysql> create database week1;
Query OK, 1 row affected (0.00 sec)
mysql> use week1;
Database changed
mysql> create table test(name char(10) not null);
Query OK, 0 rows affected (0.06 sec)
mysql> insert into test (name) values ("kk01");
Query OK, 1 row affected (0.01 sec)
[root@bogon ~]# innobackupex --user=shiina --password=shiina /backup/
...
...
xtrabackup: The latest check point (for incremental): '2496292'
xtrabackup: Stopping log copying thread.
.160530 19:06:15 >> log scanned up to (2496301)
160530 19:06:15 Executing UNLOCK BINLOG
160530 19:06:15 Executing UNLOCK TABLES
160530 19:06:15 All tables unlocked
160530 19:06:15 [00] Copying ib_buffer_pool to /backup/2016-05-30_19-06-10/ib_buffer_pool
160530 19:06:15 [00] ...done
160530 19:06:15 Backup created in directory '/backup/2016-05-30_19-06-10'
160530 19:06:15 [00] Writing backup-my.cnf
160530 19:06:15 [00] ...done
160530 19:06:15 [00] Writing xtrabackup_info
160530 19:06:15 [00] ...done
xtrabackup: Transaction log of lsn (2496292) to (2496301) was copied.
160530 19:06:15 completed OK!
第一次增量备份
mysql> insert into test (name) values ("kk02");
Query OK, 1 row affected (0.02 sec)
[root@bogon ~]# innobackupex --user=shiina --password=shiina --incremental /backup/ --incremental-basedir=/backup/2016-05-30_07-04-31/
...
...
xtrabackup: Transaction log of lsn (2499531) to (2499540) was copied.
160530 19:08:27 completed OK!
第二次增量备份
mysql> insert into test (name) values ("kk03");
Query OK, 1 row affected (0.02 sec)
[root@bogon ~]# innobackupex --user=shiina --password=shiina --incremental /backup/ --incremental-basedir=/backup/2016-05-30_07-05-32/
...
...
xtrabackup: Transaction log of lsn (2499709) to (2499718) was copied.
160530 19:09:40 completed OK!
查看增量备份记录文件:
[root@bogon backup]# cat 2016-05-30_19-06-10/xtrabackup_checkpoints
backup_type = full-prepared
from_lsn = 0 #完全备份的起始为0
to_lsn = 2496292
last_lsn = 2496301
compact = 0
recover_binlog_info = 0
[root@bogon backup]# cat 2016-05-30_19-08-21/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2496292
to_lsn = 2499531
last_lsn = 2499540
compact = 0
recover_binlog_info = 0
[root@bogon backup]# cat 2016-05-30_19-09-35/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2499531
to_lsn = 2499709
last_lsn = 2499718
compact = 0
recover_binlog_info = 0
准备合并:
[root@bogon ~]# innobackupex --apply-log --redo-only /backup/2016-05-30_19-06-10/
...
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2496310
InnoDB: Number of pools: 1
160530 19:10:49 completed OK!
[root@bogon ~]# innobackupex --apply-log --redo-only --incremental /backup/2016-05-30_19-06-10/ --incremental-dir=/backup/2016-05-30_19-08-21/
...
160530 19:35:54 completed OK!
[root@bogon ~]# innobackupex --apply-log --redo-only --incremental /backup/2016-05-30_19-06-10/ --incremental-dir=/backup/2016-05-30_19-09-35/
...
160530 19:36:38 completed OK!
Percona XtraBackup 执行类似以下命令准备(prepare)备份时, 报以下错误
[root@bogon ~]# innobackupex --apply-log --redo-only --incremental /backup/2016-05-30_19-06-10/ --incremental-dir=/backup/2016-05-30_19-08-21/
...
...
InnoDB: File (unknown): 'read' returned OS error 114. Cannot continue operation
InnoDB: Cannot continue operation.
停止mysql服务或重启服务器, 即可解决
再次查看备份记录文件:
[root@bogon backup]# cat 2016-05-30_19-06-10/xtrabackup_checkpoints
backup_type = log-applied
from_lsn = 0
to_lsn = 2499709
last_lsn = 2499718 #注意此时完全备份的 last_lsn 与第二次增量备份的 last_lsn 值相等, 说明增量备份的数据已经合并到了完全备份中
compact = 0
recover_binlog_info = 0
[root@bogon backup]# cat 2016-05-30_19-08-21/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2496292
to_lsn = 2499531
last_lsn = 2499540
compact = 0
recover_binlog_info = 0
[root@bogon backup]# cat 2016-05-30_19-09-35/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2499531
to_lsn = 2499709
last_lsn = 2499718
compact = 0
recover_binlog_info = 0
恢复数据:
[root@bogon backup]# service mysqld stop
Shutting down MySQL (Percona Server).. SUCCESS!
[root@bogon backup]# rm -rf /data/mysql/*
[root@bogon backup]# innobackupex --copy-back /backup/2016-05-30_19-06-10/
...
160530 19:42:31 completed OK
[root@bogon ~]# chown -R mysql:mysql /data/mysql/*
[root@bogon ~]# service mysqld start
Starting MySQL (Percona Server). SUCCESS!
测试数据完整:
[root@bogon ~]# mysql -h127.0.0.1 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.11-4 Percona Server (GPL), Release 4, Revision 5c940e1
Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use week1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from test;
+------+
| name |
+------+
| kk01 |
| kk02 |
| kk03 |
+------+
3 rows in set (0.00 sec)
补充: innobackup 常用参数说明(转载)
–defaults-file
同xtrabackup的–defaults-file参数
–apply-log
对xtrabackup的–prepare参数的封装
–copy-back
做数据恢复时将备份数据文件拷贝到MySQL服务器的datadir ;
–remote-host=HOSTNAME
通过ssh将备份数据存储到进程服务器上;
–stream=[tar]
备 份文件输出格式, tar时使用tar4ibd , 该文件可在XtarBackup binary文件中获得.如果备份时有指定–stream=tar, 则tar4ibd文件所处目录一定要在$PATH中(因为使用的是tar4ibd去压缩, 在XtraBackup的binary包中可获得该文件)。
在 使用参数stream=tar备份的时候,你的xtrabackup_logfile可能会临时放在/tmp目录下,如果你备份的时候并发写入较大的话 xtrabackup_logfile可能会很大(5G+),很可能会撑满你的/tmp目录,可以通过参数–tmpdir指定目录来解决这个问题。
–tmpdir=DIRECTORY
当有指定–remote-host or –stream时, 事务日志临时存储的目录, 默认采用MySQL配置文件中所指定的临时目录tmpdir
–redo-only –apply-log组,
强制备份日志时只redo ,跳过rollback。这在做增量备份时非常必要。
–use-memory=#
该参数在prepare的时候使用,控制prepare时innodb实例使用的内存量
–throttle=IOS
同xtrabackup的–throttle参数
–sleep=是给ibbackup使用的,指定每备份1M数据,过程停止拷贝多少毫秒,也是为了在备份时尽量减小对正常业务的影响,具体可以查看ibbackup的手册 ;
–compress[=LEVEL]
对备份数据迚行压缩,仅支持ibbackup,xtrabackup还没有实现;
–include=REGEXP
对 xtrabackup参数–tables的封装,也支持ibbackup。备份包含的库表,例如:–include=”test.“,意思是要备份 test库中所有的表。如果需要全备份,则省略这个参数;如果需要备份test库下的2个表:test1和test2,则写 成:–include=”test.test1|test.test2″。也可以使用通配符,如:–include=”test.test“。
–databases=LIST
列出需要备份的databases,如果没有指定该参数,所有包含MyISAM和InnoDB表的database都会被备份;
–uncompress
解压备份的数据文件,支持ibbackup,xtrabackup还没有实现该功能;
–slave-info,
备 份从库, 加上–slave-info备份目录下会多生成一个xtrabackup_slave_info 文件, 这里会保存主日志文件以及偏移, 文件内容类似于:CHANGE MASTER TO MASTER_LOG_FILE=”, MASTER_LOG_POS=0
–socket=SOCKET
指定mysql.sock所在位置,以便备份进程登录mysql.