基于GTID恢复误篡改数据

问题描述:创建测试库和测试表,先update数据,在delete数据,在update数据,通过gtid查找两次update的值。

参考文档:https://baijiahao.baidu.com/s?id=1661214737415657389&wfr=spider&for=pc

1.创建测试数据

create database back_gtid charset utf8mb4;
use back_gtid; create table tmp(id int, name varchar(20));
insert into tmp values(1,'zs'),(2,'ls'),(3,'ww'),(4,'zl'),(5,'qb');
commit;

2.全库导出

mysqldump -uroot -p -A -R -E --triggers --master-data=2 --single-transaction -S /data/3308/mysql.sock | gzip > /tmp/full_$(date +%F).sql.gz

3.修改数据

use back_gtid; 
update tmp set name = 'ZS' where id=1; 
commit; 
update tmp set name = 'LS' where id =2; 
commit; 
insert into tmp values(6,'aa'),(7,'bb'),(8,'cc'); 
commit; 
delete from tmp where id = 5; 
commit;

4.删除所有数据

use back_gtid; 
delete from tmp; 
commit;

5.再插入数据新的数据

use back_gtid; 
insert into tmp values(9,"dd"),(10,"ee"); 
commit;

6.准备多实例测试库3309,做中转库做数据测试

systemctl start mysqld3309.service

7.全库恢复

cd /tmp 
gunzip full_2021-02-01.sql.gz

8.从备份中找到创建库时的GTID,跳过误删除部分,加上新插入的数据行,查找删除库前的position号,从6开始

[root@mysql-test /tmp 20:00:05]# grep -A 10 "GTID_PURGED" full_2021-02-01.sql 
SET @@GLOBAL.GTID_PURGED='b7ccf235-5f7b-11eb-a983-000c29a61c0a:1-5';

--
-- Position to start replication or point-in-time recovery from
--

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000021', MASTER_LOG_POS=1180;

--
-- Current Database: `back_gtid`
--

9.查看当前的gtid值,现在的位置号是11,说明从备份数据到现在为止的操作是6-11,现在要在6-11的区间内排除误删数据的操作

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000021 |     2890 |              |                  | b7ccf235-5f7b-11eb-a983-000c29a61c0a:1-11 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)

10.每一次事务的提交,都对应一个GTID号,从模拟执行过程看,需要10就是删除表数据的元凶,恢复时要排除它

从下面的events中可以看到,delete操作的位置号是10,

mysql> show binlog events in 'mysql-bin.000021';
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| Log_name         | Pos  | Event_type     | Server_id | End_log_pos | Info                                                               |
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| mysql-bin.000021 |    4 | Format_desc    |         8 |         123 | Server ver: 5.7.32-log, Binlog ver: 4                              |
| mysql-bin.000021 |  123 | Previous_gtids |         8 |         154 |                                                                    |
| mysql-bin.000021 |  154 | Gtid           |         8 |         219 | SET @@SESSION.GTID_NEXT= 'b7ccf235-5f7b-11eb-a983-000c29a61c0a:1'  |
| mysql-bin.000021 |  219 | Query          |         8 |         325 | create database test3308                                           |
| mysql-bin.000021 |  325 | Gtid           |         8 |         390 | SET @@SESSION.GTID_NEXT= 'b7ccf235-5f7b-11eb-a983-000c29a61c0a:2'  |
| mysql-bin.000021 |  390 | Query          |         8 |         497 | drop database back_gtid                                            |
| mysql-bin.000021 |  497 | Gtid           |         8 |         562 | SET @@SESSION.GTID_NEXT= 'b7ccf235-5f7b-11eb-a983-000c29a61c0a:3'  |
| mysql-bin.000021 |  562 | Query          |         8 |         687 | create database back_gtid charset utf8mb4                          |
| mysql-bin.000021 |  687 | Gtid           |         8 |         752 | SET @@SESSION.GTID_NEXT= 'b7ccf235-5f7b-11eb-a983-000c29a61c0a:4'  |
| mysql-bin.000021 |  752 | Query          |         8 |         878 | use `back_gtid`; create table tmp(id int, name varchar(20))        |
| mysql-bin.000021 |  878 | Gtid           |         8 |         943 | SET @@SESSION.GTID_NEXT= 'b7ccf235-5f7b-11eb-a983-000c29a61c0a:5'  |
| mysql-bin.000021 |  943 | Query          |         8 |        1020 | BEGIN                                                              |
| mysql-bin.000021 | 1020 | Table_map      |         8 |        1074 | table_id: 109 (back_gtid.tmp)                                      |
| mysql-bin.000021 | 1074 | Write_rows     |         8 |        1149 | table_id: 109 flags: STMT_END_F                                    |
| mysql-bin.000021 | 1149 | Xid            |         8 |        1180 | COMMIT /* xid=21 */                                                |
| mysql-bin.000021 | 1180 | Gtid           |         8 |        1245 | SET @@SESSION.GTID_NEXT= 'b7ccf235-5f7b-11eb-a983-000c29a61c0a:6'  |
| mysql-bin.000021 | 1245 | Query          |         8 |        1322 | BEGIN                                                              |
| mysql-bin.000021 | 1322 | Table_map      |         8 |        1376 | table_id: 111 (back_gtid.tmp)                                      |
| mysql-bin.000021 | 1376 | Update_rows    |         8 |        1428 | table_id: 111 flags: STMT_END_F                                    |
| mysql-bin.000021 | 1428 | Xid            |         8 |        1459 | COMMIT /* xid=650 */                                               |
| mysql-bin.000021 | 1459 | Gtid           |         8 |        1524 | SET @@SESSION.GTID_NEXT= 'b7ccf235-5f7b-11eb-a983-000c29a61c0a:7'  |
| mysql-bin.000021 | 1524 | Query          |         8 |        1601 | BEGIN                                                              |
| mysql-bin.000021 | 1601 | Table_map      |         8 |        1655 | table_id: 111 (back_gtid.tmp)                                      |
| mysql-bin.000021 | 1655 | Update_rows    |         8 |        1707 | table_id: 111 flags: STMT_END_F                                    |
| mysql-bin.000021 | 1707 | Xid            |         8 |        1738 | COMMIT /* xid=652 */                                               |
| mysql-bin.000021 | 1738 | Gtid           |         8 |        1803 | SET @@SESSION.GTID_NEXT= 'b7ccf235-5f7b-11eb-a983-000c29a61c0a:8'  |
| mysql-bin.000021 | 1803 | Query          |         8 |        1880 | BEGIN                                                              |
| mysql-bin.000021 | 1880 | Table_map      |         8 |        1934 | table_id: 111 (back_gtid.tmp)                                      |
| mysql-bin.000021 | 1934 | Write_rows     |         8 |        1993 | table_id: 111 flags: STMT_END_F                                    |
| mysql-bin.000021 | 1993 | Xid            |         8 |        2024 | COMMIT /* xid=654 */                                               |
| mysql-bin.000021 | 2024 | Gtid           |         8 |        2089 | SET @@SESSION.GTID_NEXT= 'b7ccf235-5f7b-11eb-a983-000c29a61c0a:9'  |
| mysql-bin.000021 | 2089 | Query          |         8 |        2166 | BEGIN                                                              |
| mysql-bin.000021 | 2166 | Table_map      |         8 |        2220 | table_id: 111 (back_gtid.tmp)                                      |
| mysql-bin.000021 | 2220 | Delete_rows    |         8 |        2263 | table_id: 111 flags: STMT_END_F                                    |
| mysql-bin.000021 | 2263 | Xid            |         8 |        2294 | COMMIT /* xid=656 */                                               |
| mysql-bin.000021 | 2294 | Gtid           |         8 |        2359 | SET @@SESSION.GTID_NEXT= 'b7ccf235-5f7b-11eb-a983-000c29a61c0a:10' |
| mysql-bin.000021 | 2359 | Query          |         8 |        2436 | BEGIN                                                              |
| mysql-bin.000021 | 2436 | Table_map      |         8 |        2490 | table_id: 111 (back_gtid.tmp)                                      |
| mysql-bin.000021 | 2490 | Delete_rows    |         8 |        2581 | table_id: 111 flags: STMT_END_F                                    |
| mysql-bin.000021 | 2581 | Xid            |         8 |        2612 | COMMIT /* xid=661 */                                               |
| mysql-bin.000021 | 2612 | Gtid           |         8 |        2677 | SET @@SESSION.GTID_NEXT= 'b7ccf235-5f7b-11eb-a983-000c29a61c0a:11' |
| mysql-bin.000021 | 2677 | Query          |         8 |        2754 | BEGIN                                                              |
| mysql-bin.000021 | 2754 | Table_map      |         8 |        2808 | table_id: 111 (back_gtid.tmp)                                      |
| mysql-bin.000021 | 2808 | Write_rows     |         8 |        2859 | table_id: 111 flags: STMT_END_F                                    |
| mysql-bin.000021 | 2859 | Xid            |         8 |        2890 | COMMIT /* xid=666 */                                               |
+------------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
45 rows in set (0.00 sec)

11.将二进制文件导出为sql文件,从6-11也就是现在位置,但是需要排除恢复误删除的区间内的10

mysqlbinlog --skip-gtids --include-gtids="b7ccf235-5f7b-11eb-a983-000c29a61c0a:6-11" --exclude-gtids="b7ccf235-5f7b-11eb-a983-000c29a61c0a:10" /data/3308/mysql-bin.000021 > /tmp/gtid-bin.sql;

12.将备份恢复到临时库,先恢复之前备份的数据,在恢复提取出来的数据是否正确

mysql -uroot -p -S /data/3309/mysql.sock

 set sql_log_bin=0;
 source /tmp/full_2021-02-01.sql;
 source /tmp/gtid-bin.sql;

13.查询验证

use back_gtid; 
select * from tmp;

14.没问题可以恢复到生产

mysqldump -uroot -p --set-gtid-purged=OFF -S /data/3309/mysql.sock back_gtid tmp > /tmp/gtid-tmp.sql
mysql
-uroot -p -S /data/3308/mysql.sock set sql_log_bin=0; use back_gtid; source /tmp/gtid-tmp.sql;

15.查询验证3308是否恢复

use back_gtid;
select * from tmp;
set sql_log_bin=1;

 

 

posted @ 2021-02-02 09:47  我爱睡莲  阅读(191)  评论(0编辑  收藏  举报