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;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章