Mysql5.7 innobackupex 增量备份恢复数据

1、模拟数据变化(全备份之前)
3306 [(none)]> create database test charset utf8;
3306 [(none)]> use test;
3306 [(none)]> create table t1 (id int);
3306 [(none)]> insert into t1 values(1),(2),(3);
3306 [(none)]> commit;

2、模拟周日全备
[root@53 ~]# innobackupex --user=root --password=123456 --no-timestamp /data/backup/full

3、模拟周一数据变化
[root@53 ~]# mysql -uroot -p123456
3306 [(none)]> use test;
3306 [(none)]> create table t2 (id int);
3306 [(none)]> insert into t2 values(1),(2),(3);
3306 [(none)]> commit;

3.1. 模拟周一增量备份
[root@53 ~]# innobackupex --user=root --password=123456 --no-timestamp --incremental /data/backup/inc1 --incremental-basedir=/data/backup/full/

4、模拟周二数据变化
[root@53 ~]# mysql -uroot -p123456
3306 [(none)]> use test;
3306 [(none)]> create table t3 (id int);
3306 [(none)]> insert into t3 values(1),(2),(3);
3306 [(none)]> commit;

4.1 模拟周二增量备份
[root@53 ~]# innobackupex --user=root --password=123456 --no-timestamp --incremental /data/backup/inc2 --incremental-basedir=/data/backup/inc1

5、模拟周三数据变化
[root@53 ~]# mysql -uroot -p123456
3306 [(none)]> use test;
3306 [(none)]> create table t4 (id int);
3306 [(none)]> insert into t4 values(1),(2),(3);
3306 [(none)]> commit;
3306 [(none)]> drop database test;

++++++++++++++++++++++++++++++++++++++++++++++++++++
恢复方案
注意:
1、增量不能单独恢复
2、增量必须顺序合并到全备中(LSN号码为基准)
3、所有备份都需要–apply-log
4、部分备份只需要redo不需要undo(–redo-only)

–apply-log Prepare a backup in BACKUP-DIR by applying the
transaction log file named “xtrabackup_logfile” located
in the same directory. Also, create new transaction logs.
The InnoDB configuration is read from the file
“backup-my.cnf”.
–redo-only This option should be used when preparing the base full
backup and when merging all incrementals except the last
one. This forces xtrabackup to skip the “rollback” phase
and do a “redo” only. This is necessary if the backup
will have incremental changes applied to it later.

开始恢复:
1、没合并之前整理全备(–redo-only)
[root@53 ~]# innobackupex --apply-log --redo-only /data/backup/full/

2、整理合并inc1到full
[root@53 ~]# innobackupex --apply-log --redo-only --incremental-dir=/data/backup/inc1 /data/backup/full/

3、整理合并inc2到full(最后一个增量备份没有–redo-only)
[root@53 ~]# innobackupex --apply-log --incremental-dir=/data/backup/inc2 /data/backup/full/

4、full的终极整理
[root@53 ~]# innobackupex --apply-log /data/backup/full/

5、 二进制日志截取

5.1 判断起点
[root@53 ~]# cat /data/backup/inc2/xtrabackup_binlog_info
mysql-bin.000001 1430 ed745770-805a-11ea-85cf-b8ca3af3796f:1-6

5.2 判断终点(删库之前为终点)
| mysql-bin.000001 | 1857 | Gtid | 2 | 1922 | SET @@SESSION.GTID_NEXT= ‘ed745770-805a-11ea-85cf-b8ca3af3796f:9’ |
| mysql-bin.000001 | 1922 | Query | 2 | 2014 | drop database test |

5.3 截取二进制日志

[root@53 ~]# mysqlbinlog --skip-gtids --include-gtids=“ed745770-805a-11ea-85cf-b8ca3af3796f:7-8” /opt/mysql5.7/logs/mysql-bin.000001 > /data/backup/bin.sql

6、恢复数据

6.1 准备恢复环境
[root@53 3307]# cd /data/3307/data
[root@53 3307]# rm -rf *

6.2 xbk恢复数据
[root@53 3307]# cp -a /data/backup/full/* /data/3307/data/

6.3 授权并启动数据库
[root@53 3307]# chown -R mysql.mysql /data/3307/data/
[root@53 3307]# systemctl restart mysqld3307

6.4 进入数据库恢复数据
[root@53 3307]# mysql -uroot -p -S /tmp/3307.sock
[(none)]> set sql_log_bin=0
[(none)]>source /data/backup/bin.sql

6.5 导出数据恢复至原库
[root@53 ~]# mysqldump -uroot -p -S /tmp/3307.sock -R -E --triggers --single-transaction -B test > /data/backup/test.sql

[root@53 ~]# mysql -uroot -ps
[(none)]> set sql_log_bin=0
[(none)]> source /data/backup/test.sql

6.6 检查核对数据
3306 [test]>show databases;
3306 [test]>use test;
3306 [test]>select * from t4;

你可能感兴趣的:(mysql)