阅读目录
前一篇文章我们讲到了PXB的原理以及安装方法,接下来将详细介绍 XtraBackup 备份和恢复的具体过程。
xtrabackup 选项
1) innobackupex 参数选项
--defaults-file=[MY.CNF] //指定配置文件:只能从给定的文件中读取默认选项。 且必须作为命令行上的第一个选项;必须是一个真实的文件,它不能是一个符号链接。
--databases=# //指定备份的数据库和表,格式为:--database="db1[.tb1] db2[.tb2]" 多个库之间以空格隔开,如果此选项不被指定,将会备份所有的数据库。
--include=REGEXP //用正则表达式的方式指定要备份的数据库和表,格式为 --include=‘^mydb[.]mytb’ ,对每个库中的每个表逐一匹配,因此会创建所有的库,不过是空的目录。--include 传递给 xtrabackup --tables。
--tables-file=FILE //此选项的参数需要是一个文件名,此文件中每行包含一个要备份的表的完整名称,格式为databasename.tablename。该选项传递给 xtrabackup --tables-file,与--tables选项不同,只有要备份的表的库才会被创建。
注意:部分备份(--include、--tables-file、--database)需要开启 innodb_file_per_table 。
--compact //创建紧凑型备份,忽略所有辅助索引页,只备份data page;通过--apply-log中重建索引--rebuild-indexs。
--compress //此选项指示xtrabackup压缩备份的InnoDB数据文件,会生成 *.qp 文件。
--decompress //解压缩qp文件,为了解压缩,必须安装 qpress 工具。 Percona XtraBackup不会自动删除压缩文件,为了清理备份目录,用户应手动删除 * .qp文件:find /data/backup -name "*.qp" | xargs rm。
--no-timestamp //指定了这个选项备份将会直接存储在 BACKUP-DIR 目录,不再创建时间戳文件夹。
--apply-log //应用 BACKUP-DIR 中的 xtrabackup_logfile 事务日志文件。一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处于不一致状态。“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件使得数据文件处于一致性状态。
--use-memory=# //此选项接受一个字符参数(1M/1MB,1G/1GB,默认100M),仅与--apply-log一起使用,该选项指定prepare时用于崩溃恢复(crash-recovery)的内存。
--copy-back //拷贝先前备份所有文件到它们的原始路径。但原路径下不能有任何文件或目录,除非指定 --force-non-empty-directories 选项。
--force-non-empty-directories //恢复时指定此选项,可使 --copy-back 和 --move-back 复制文件到非空目录,即原data目录下可以有其他文件,但是不能有与恢复文件中同名的文件,否则恢复失败。
--rsync //此选项可优化本地文件(非InnoDB)的传输。rsync工具一次性拷贝所有非InnoDB文件,而不是为每个文件单独创建cp,在备份恢复很多数据库和表时非常高效。此选项不能和 --stream 一起使用。
--incremental //这个选项告诉 xtrabackup 创建一个增量备份,而不是完全备份。它传递到 xtrabackup 子进程。当指定这个选项,可以设置 --incremental-lsn 或 --incremental-basedir。如果这2个选项都没有被指定,--incremental-basedir 传递给 xtrabackup 默认值,默认值为:基础备份目录的第一个时间戳备份目录。
--incremental-basedir=DIRECTORY //该选项接受一个字符串参数,该参数指定作为增量备份的基本数据集的完整备份目录。它与 --incremental 一起使用。
--incremental-dir=DIRECTORY //该选项接受一个字符串参数,该参数指定了增量备份将与完整备份相结合的目录,以便进行新的完整备份。它与 --incremental 选项一起使用。
--redo-only //在“准备基本完整备份” 和 “合并所有的增量备份(除了最后一个增备)”时使用此选项。它直接传递给xtrabackup的 xtrabackup --apply-log-only 选项,使xtrabackup跳过"undo"阶段,只做"redo"操作。如果后面还有增量备份应用到这个全备,这是必要的。有关详细信息,请参阅xtrabackup文档。
--parallel=NUMBER-OF-THREADS //此选项接受一个整数参数,指定xtrabackup子进程应用于同时备份文件的线程数。请注意,此选项仅适用于文件级别,也就是说,如果您有多个.ibd文件,则它们将被并行复制; 如果您的表一起存储在一个表空间文件中,它将不起作用。
2) xtrabackup 参数选项
--apply-log-only //这个选项使在准备备份(prepare)时,只执行重做(redo)阶段,这对于增量备份非常重要。
xtrabackup 全量备份恢复
1. 完全备份
创建用于备份恢复的用户 pxb 并赋予权限
mysql
>
create
user pxb@
'localhost' identified
by
'123456';
mysql > grant reload,process,lock tables, replication client on *. * to pxb @localhost;
mysql > grant reload,process,lock tables, replication client on *. * to pxb @localhost;
创建存放目录
[root@centos6 mysql]#
mkdir -pv /data/pxb
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/pxb"
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/pxb"
进行数据库全备
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
[root@centos6 pxb]# innobackupex --defaults-
file=/etc/my.cnf --user=pxb --password=
123456 --socket=/tmp/mysql.sock /data/pxb
170424 02: 46: 11 innobackupex: Starting the backup operation
IMPORTANT: Please check that the backup run completes successfully.
At the end of a successful backup run innobackupex
prints "completed OK!".
…………
170424 02: 46: 12 >> log scanned up to ( 2627779)
170424 02: 46: 13 Executing FLUSH NO_WRITE_TO_BINLOG TABLES...
170424 02: 46: 13 Executing FLUSH TABLES WITH READ LOCK...
170424 02: 46: 13 Starting to backup non-InnoDB tables and files
…………
170424 02: 46: 15 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point ( for incremental): '2627770'
xtrabackup: Stopping log copying thread.
. 170424 02: 46: 15 >> log scanned up to ( 2627779)
170424 02: 46: 15 Executing UNLOCK TABLES
170424 02: 46: 15 All tables unlocked
170424 02: 46: 15 [ 00] Copying ib_buffer_pool to /data/pxb/ 2017- 04-24_02- 46- 11/ib_buffer_pool
170424 02: 46: 15 [ 00] ... done
170424 02: 46: 15 Backup created in directory '/data/pxb/2017-04-24_02-46-11/'
MySQL binlog position: filename 'mysql-bin.000002', position '2628'
170424 02: 46: 15 [ 00] Writing backup-my.cnf
170424 02: 46: 15 [ 00] ... done
170424 02: 46: 15 [ 00] Writing xtrabackup_info
170424 02: 46: 15 [ 00] ... done
xtrabackup: Transaction log of lsn ( 2627770) to ( 2627779) was copied.
170424 02: 46: 15 completed OK!
170424 02: 46: 11 innobackupex: Starting the backup operation
IMPORTANT: Please check that the backup run completes successfully.
At the end of a successful backup run innobackupex
prints "completed OK!".
…………
170424 02: 46: 12 >> log scanned up to ( 2627779)
170424 02: 46: 13 Executing FLUSH NO_WRITE_TO_BINLOG TABLES...
170424 02: 46: 13 Executing FLUSH TABLES WITH READ LOCK...
170424 02: 46: 13 Starting to backup non-InnoDB tables and files
…………
170424 02: 46: 15 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point ( for incremental): '2627770'
xtrabackup: Stopping log copying thread.
. 170424 02: 46: 15 >> log scanned up to ( 2627779)
170424 02: 46: 15 Executing UNLOCK TABLES
170424 02: 46: 15 All tables unlocked
170424 02: 46: 15 [ 00] Copying ib_buffer_pool to /data/pxb/ 2017- 04-24_02- 46- 11/ib_buffer_pool
170424 02: 46: 15 [ 00] ... done
170424 02: 46: 15 Backup created in directory '/data/pxb/2017-04-24_02-46-11/'
MySQL binlog position: filename 'mysql-bin.000002', position '2628'
170424 02: 46: 15 [ 00] Writing backup-my.cnf
170424 02: 46: 15 [ 00] ... done
170424 02: 46: 15 [ 00] Writing xtrabackup_info
170424 02: 46: 15 [ 00] ... done
xtrabackup: Transaction log of lsn ( 2627770) to ( 2627779) was copied.
170424 02: 46: 15 completed OK!
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
可以看到整个备份过程:连接数据库,开始拷贝redo log,拷贝innodb表文件,锁表、拷贝非innodb表文件,停止拷贝redo log,解锁。
查看生成的文件:
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
[root@centos6 pxb]# ll
2017-
04-24_02-
46-
11/
总用量 12340
-rw-r----- 1 root root 427 4月 24 02: 46 backup-my.cnf
-rw-r----- 1 root root 358 4月 24 02: 46 ib_buffer_pool
-rw-r----- 1 root root 12582912 4月 24 02: 46 ibdata1
drwxr-x--- 2 root root 4096 4月 24 02: 46 mysql
drwxr-x--- 2 root root 4096 4月 24 02: 46 performance_schema
drwxr-x--- 2 root root 12288 4月 24 02: 46 sys
drwxr-x--- 2 root root 4096 4月 24 02: 46 test
drwxr-x--- 2 root root 4096 4月 24 02: 46 test1
-rw-r----- 1 root root 22 4月 24 02: 46 xtrabackup_binlog_info
-rw-r----- 1 root root 113 4月 24 02: 46 xtrabackup_checkpoints
-rw-r----- 1 root root 518 4月 24 02: 46 xtrabackup_info
-rw-r----- 1 root root 2560 4月 24 02: 46 xtrabackup_logfile
总用量 12340
-rw-r----- 1 root root 427 4月 24 02: 46 backup-my.cnf
-rw-r----- 1 root root 358 4月 24 02: 46 ib_buffer_pool
-rw-r----- 1 root root 12582912 4月 24 02: 46 ibdata1
drwxr-x--- 2 root root 4096 4月 24 02: 46 mysql
drwxr-x--- 2 root root 4096 4月 24 02: 46 performance_schema
drwxr-x--- 2 root root 12288 4月 24 02: 46 sys
drwxr-x--- 2 root root 4096 4月 24 02: 46 test
drwxr-x--- 2 root root 4096 4月 24 02: 46 test1
-rw-r----- 1 root root 22 4月 24 02: 46 xtrabackup_binlog_info
-rw-r----- 1 root root 113 4月 24 02: 46 xtrabackup_checkpoints
-rw-r----- 1 root root 518 4月 24 02: 46 xtrabackup_info
-rw-r----- 1 root root 2560 4月 24 02: 46 xtrabackup_logfile
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
其中,mysql/, performance_schema/, sys/ ,test/ ,test1/ 下存放的是数据库文件。
backup-my.cnf,
备份命令用到的配置选项信息;
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
[root@centos6
2017-
04-24_02-
46-
11]#
cat backup-my.cnf
# This MySQL options file was generated by innobackupex.# The MySQL server[mysqld]innodb_checksum_algorithm=innodbinnodb_log_checksum_algorithm=strict_crc32innodb_data_file_path=ibdata1:12M:autoextendinnodb_log_files_in_group= 2
innodb_log_file_size= 50331648
innodb_fast_checksum= false
innodb_page_size= 16384
innodb_log_block_size= 512
innodb_undo_directory=./
innodb_undo_tablespaces= 0
server_id= 153
redo_log_version= 1
# This MySQL options file was generated by innobackupex.# The MySQL server[mysqld]innodb_checksum_algorithm=innodbinnodb_log_checksum_algorithm=strict_crc32innodb_data_file_path=ibdata1:12M:autoextendinnodb_log_files_in_group= 2
innodb_log_file_size= 50331648
innodb_fast_checksum= false
innodb_page_size= 16384
innodb_log_block_size= 512
innodb_undo_directory=./
innodb_undo_tablespaces= 0
server_id= 153
redo_log_version= 1
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
ib_buffer_pool, buffer pool 中的热数据,当设置 innodb_buffer_pool_dump_at_shutdown=1 ,在关闭 MySQL 时,会把内存中的热数据保存在磁盘里 ib_buffer_pool 文件中,位于数据目录下。
ibdata1,备份的共享表空间文件;
xtrabackup_binlog_info,mysql服务器当前正在使用的二进制日志文件及至备份这一刻为止二进制日志事件的位置;
[root@centos6
2017-
04-24_02-
46-
11]#
cat xtrabackup_binlog_info
mysql-bin. 000002 2628
mysql-bin. 000002 2628
xtrabackup_checkpoints,备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息;
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
[root@centos6
2017-
04-24_02-
46-
11]#
cat xtrabackup_checkpoints
backup_type = full-backuped
from_lsn = 0
to_lsn = 2627770
last_lsn = 2627779
compact = 0
recover_binlog_info = 0
backup_type = full-backuped
from_lsn = 0
to_lsn = 2627770
last_lsn = 2627779
compact = 0
recover_binlog_info = 0
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
xtrabackup_info,记录备份的基本信息,uuid、备份命令、备份时间、binlog、LSN、以及其他加密压缩等信息。
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
[root@centos6
2017-
04-24_02-
46-
11]#
cat xtrabackup_info
uuid = 216881a3- 2855-11e7-bc84-080027b387ba
name =
tool_name = innobackupex
tool_command = --defaults- file=/etc/my.cnf --user=pxb --password=... --socket=/tmp/mysql.sock /data/pxb
tool_version = 2.4. 6
ibbackup_version = 2.4. 6
server_version = 5.7. 10-log
start_time = 2017- 04- 24 02: 46: 11
end_time = 2017- 04- 24 02: 46: 15
lock_time = 0
binlog_pos = filename 'mysql-bin.000002', position '2628'
innodb_from_lsn = 0
innodb_to_lsn = 2627770
partial = N
incremental = N
format = file
compact = N
compressed = N
encrypted = N
uuid = 216881a3- 2855-11e7-bc84-080027b387ba
name =
tool_name = innobackupex
tool_command = --defaults- file=/etc/my.cnf --user=pxb --password=... --socket=/tmp/mysql.sock /data/pxb
tool_version = 2.4. 6
ibbackup_version = 2.4. 6
server_version = 5.7. 10-log
start_time = 2017- 04- 24 02: 46: 11
end_time = 2017- 04- 24 02: 46: 15
lock_time = 0
binlog_pos = filename 'mysql-bin.000002', position '2628'
innodb_from_lsn = 0
innodb_to_lsn = 2627770
partial = N
incremental = N
format = file
compact = N
compressed = N
encrypted = N
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
xtrabackup_logfile,备份的重做日志文件。
2. 全备恢复
关闭数据库并删除数据文件
[root@centos6 data]# /etc/init.d/mysqld stop
Shutting down MySQL.. [确定]
[root@centos6 mysql]# cd /home/mysql
[root@centos6 mysql]# mv data/ data_bak/
[root@centos6 mysql]# mkdir data
Shutting down MySQL.. [确定]
[root@centos6 mysql]# cd /home/mysql
[root@centos6 mysql]# mv data/ data_bak/
[root@centos6 mysql]# mkdir data
准备(prepare)一个完全备份: --apply-log ( /data/pxb/2017-04-24_02-46-11/ 为备份目录,执行之后 xtrabackup_checkpoints 文件中的 backup_type = full-prepared )
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
[root@centos6 mysql]# innobackupex --apply-log /data/pxb/
2017-
04-24_02-
46-
11/
170424 15: 50: 20 innobackupex: Starting the apply-log operation
IMPORTANT: Please check that the apply-log run completes successfully.
At the end of a successful apply-log run innobackupex
prints "completed OK!".
…………
InnoDB: 5.7. 13 started; log sequence number 2628117
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2628136
170424 15: 50: 24 completed OK!
170424 15: 50: 20 innobackupex: Starting the apply-log operation
IMPORTANT: Please check that the apply-log run completes successfully.
At the end of a successful apply-log run innobackupex
prints "completed OK!".
…………
InnoDB: 5.7. 13 started; log sequence number 2628117
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2628136
170424 15: 50: 24 completed OK!
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
执行恢复操作:
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
[root@centos6
2017-
04-24_02-
46-
11]# innobackupex --defaults-
file=/etc/my.cnf --copy-back --rsync /data/pxb/
2017-
04-24_02-
46-
11/
170424 16: 02: 26 innobackupex: Starting the copy-back operation
IMPORTANT: Please check that the copy-back run completes successfully.
At the end of a successful copy-back run innobackupex
prints "completed OK!".
innobackupex version 2.4. 6 based on MySQL server 5.7. 13 Linux (x86_64) (revision id: 8ec05b7)
170424 16: 02: 26 [ 01] Copying ib_logfile0 to /home/mysql/data/ib_logfile0
170424 16: 02: 26 [ 01] ... done
170424 16: 02: 26 [ 01] Copying ib_logfile1 to /home/mysql/data/ib_logfile1
170424 16: 02: 26 [ 01] ... done
170424 16: 02: 26 [ 01] Copying ibdata1 to /home/mysql/data/ibdata1
170424 16: 02: 26 [ 01] ... done
……
170424 16: 02: 31 [ 01] Copying ./xtrabackup_info to /home/mysql/data/xtrabackup_info
170424 16: 02: 31 [ 01] ... done
170424 16: 02: 31 [ 01] Copying ./xtrabackup_binlog_pos_innodb to /home/mysql/data/xtrabackup_binlog_pos_innodb
170424 16: 02: 31 [ 01] ... done
170424 16: 02: 31 completed OK!
170424 16: 02: 26 innobackupex: Starting the copy-back operation
IMPORTANT: Please check that the copy-back run completes successfully.
At the end of a successful copy-back run innobackupex
prints "completed OK!".
innobackupex version 2.4. 6 based on MySQL server 5.7. 13 Linux (x86_64) (revision id: 8ec05b7)
170424 16: 02: 26 [ 01] Copying ib_logfile0 to /home/mysql/data/ib_logfile0
170424 16: 02: 26 [ 01] ... done
170424 16: 02: 26 [ 01] Copying ib_logfile1 to /home/mysql/data/ib_logfile1
170424 16: 02: 26 [ 01] ... done
170424 16: 02: 26 [ 01] Copying ibdata1 to /home/mysql/data/ibdata1
170424 16: 02: 26 [ 01] ... done
……
170424 16: 02: 31 [ 01] Copying ./xtrabackup_info to /home/mysql/data/xtrabackup_info
170424 16: 02: 31 [ 01] ... done
170424 16: 02: 31 [ 01] Copying ./xtrabackup_binlog_pos_innodb to /home/mysql/data/xtrabackup_binlog_pos_innodb
170424 16: 02: 31 [ 01] ... done
170424 16: 02: 31 completed OK!
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
更改 data/ 目录权限并启动mysql:
[root@centos6 mysql]#
chown -R mysql.mysql data/
[root@centos6 data]# /etc/init.d/mysqld start
Starting MySQL. [确定]
[root@centos6 data]# /etc/init.d/mysqld start
Starting MySQL. [确定]
可以看到数据库和表均已恢复:
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
mysql
> show databases;
+ --------------------+
| Database |
+ --------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| test1 |
+ --------------------+
6 rows in set ( 0.00 sec)
+ --------------------+
| Database |
+ --------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| test1 |
+ --------------------+
6 rows in set ( 0.00 sec)
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
xtrabackup 增量备份恢复
我们以之前做的全备为基准,在其基础上做增量备份:
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
##新建一张表,并插入数据作为增量##
mysql > create table tb2 (id int,name varchar( 40));
Query OK, 0 rows affected ( 0.06 sec)
mysql > insert into tb2 values ( 1, 'aaa'),( 2, 'bbb'),( 3, 'ccc'),( 26, 'zzz');
Query OK, 4 rows affected ( 0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql > select * from tb2;
+ ------+------+
| id | name |
+ ------+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 26 | zzz |
+ ------+------+
4 rows in set ( 0.00 sec)
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
增量备份1:( 以全备为基准:/data/pxb/2017-04-24_02-46-11/ )
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
[root@centos6 pxb]# innobackupex --defaults-
file=/etc/my.cnf --user=pxb --password=
123456 --socket=/tmp/mysql.sock --incremental /data/pxb/inc --incremental-basedir=/data/pxb/
2017-
04-24_02-
46-
11/ --parallel=
2
[root@centos6 2017- 04-28_01- 09- 40]# cat xtrabackup_checkpoints
backup_type = incremental ##说明是增量备份
from_lsn = 2627770
to_lsn = 2636739
last_lsn = 2636748
compact = 0
recover_binlog_info = 0
[root@centos6 2017- 04-28_01- 09- 40]# cat xtrabackup_checkpoints
backup_type = incremental ##说明是增量备份
from_lsn = 2627770
to_lsn = 2636739
last_lsn = 2636748
compact = 0
recover_binlog_info = 0
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
再往 tb2 里插入数据:
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
mysql
>
insert
into tb2
values (
201,
'aaa'),(
202,
'bbb'),(
203,
'ccc'),(
326,
'zzz');
Query OK, 4 rows affected ( 0.12 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql >
mysql > select * from tb2;
+ ------+------+
| id | name |
+ ------+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 26 | zzz |
| 201 | aaa |
| 202 | bbb |
| 203 | ccc |
| 326 | zzz |
+ ------+------+
8 rows in set ( 0.00 sec)
Query OK, 4 rows affected ( 0.12 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql >
mysql > select * from tb2;
+ ------+------+
| id | name |
+ ------+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 26 | zzz |
| 201 | aaa |
| 202 | bbb |
| 203 | ccc |
| 326 | zzz |
+ ------+------+
8 rows in set ( 0.00 sec)
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
增量备份2:( 以增量1为基准:/data/pxb/inc/2017-04-28_01-09-40/ )
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
innobackupex --defaults-
file=/etc/my.cnf --user=pxb --password=
123456 --socket=/tmp/mysql.sock --incremental /data/pxb/inc --incremental-basedir=/data/pxb/inc/
2017-
04-28_01-
09-
40/ --parallel=
2
[root@centos6 2017- 04-28_01- 27- 46]# cat xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2636739
to_lsn = 2643563
last_lsn = 2643572
compact = 0
recover_binlog_info = 0
[root@centos6 2017- 04-28_01- 27- 46]# cat xtrabackup_checkpoints
backup_type = incremental
from_lsn = 2636739
to_lsn = 2643563
last_lsn = 2643572
compact = 0
recover_binlog_info = 0
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
增量备份的恢复
增量备份的恢复需要有3个步骤
- 恢复完全备份
- 恢复增量备份到完全备份(开始恢复的增量备份要添加--redo-only参数,到最后一次增量备份要去掉--redo-only)
- 对整体的完全备份进行恢复,回滚未提交的数据
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
##准备一个全备##
[root@centos6 pxb]# innobackupex --apply-log --redo-only /data/pxb/
2017-
04-24_02-
46-
11/
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2628145
InnoDB: Number of pools: 1
170428 01: 33: 52 completed OK!
##将增量1应用到完全备份##
[root@centos6 pxb]# innobackupex --apply-log --redo-only /data/pxb/ 2017- 04-24_02- 46- 11/ --incremental- dir=/data/pxb/inc/ 2017- 04-28_01- 09- 40/
##将增量2应用到完全备份,注意不加 --redo-only 参数了##
[root@centos6 pxb]# innobackupex --apply-log /data/pxb/ 2017- 04-24_02- 46- 11/ --incremental- dir=/data/pxb/inc/ 2017- 04-28_01- 27- 46/
##把所有合在一起的完全备份整体进行一次apply操作,回滚未提交的数据##
[root@centos6 pxb]# innobackupex --apply-log /data/pxb/ 2017- 04-24_02- 46- 11/
xtrabackup: Log applied to lsn 2628145
xtrabackup: The intended lsn is 2643563
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2628145
InnoDB: Number of pools: 1
170428 01: 33: 52 completed OK!
##将增量1应用到完全备份##
[root@centos6 pxb]# innobackupex --apply-log --redo-only /data/pxb/ 2017- 04-24_02- 46- 11/ --incremental- dir=/data/pxb/inc/ 2017- 04-28_01- 09- 40/
##将增量2应用到完全备份,注意不加 --redo-only 参数了##
[root@centos6 pxb]# innobackupex --apply-log /data/pxb/ 2017- 04-24_02- 46- 11/ --incremental- dir=/data/pxb/inc/ 2017- 04-28_01- 27- 46/
##把所有合在一起的完全备份整体进行一次apply操作,回滚未提交的数据##
[root@centos6 pxb]# innobackupex --apply-log /data/pxb/ 2017- 04-24_02- 46- 11/
xtrabackup: Log applied to lsn 2628145
xtrabackup: The intended lsn is 2643563
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
模拟测试:
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)
mysql> drop table tb2;
Query OK, 0 rows affected ( 0.05 sec)
[root@centos6 mysql]# /etc/init.d/mysqld stop
[root@centos6 mysql]# mv data data_bak2
[root@centos6 mysql]# mkdir data
innobackupex --defaults- file=/etc/my.cnf --copy-back --rsync /data/pxb/ 2017- 04-24_02- 46- 11/
[root@centos6 mysql]# chown -R mysql:mysql data/
[root@centos6 mysql]# /etc/init.d/mysqld start
##数据已经恢复##
mysql> select * from tb2;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 26 | zzz |
| 201 | aaa |
| 202 | bbb |
| 203 | ccc |
| 326 | zzz |
+------+------+
8 rows in set ( 0.01 sec)
Query OK, 0 rows affected ( 0.05 sec)
[root@centos6 mysql]# /etc/init.d/mysqld stop
[root@centos6 mysql]# mv data data_bak2
[root@centos6 mysql]# mkdir data
innobackupex --defaults- file=/etc/my.cnf --copy-back --rsync /data/pxb/ 2017- 04-24_02- 46- 11/
[root@centos6 mysql]# chown -R mysql:mysql data/
[root@centos6 mysql]# /etc/init.d/mysqld start
##数据已经恢复##
mysql> select * from tb2;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 26 | zzz |
| 201 | aaa |
| 202 | bbb |
| 203 | ccc |
| 326 | zzz |
+------+------+
8 rows in set ( 0.01 sec)
![复制代码](https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif)