Quick flashback of MySQL misoperation

Project author: danfengcao

Project address: https://github.com/danfengcao/binlog2sql

 

1. Environmental restrictions

mysql server must be enabled, binlog cannot be parsed in offline mode

binlog format must be line mode

 

Software version: Python 2.7 MySQL 5.6

MySQL set the following parameters:

[mysqld]

server-id = 1

log_bin = /var/log/mysql/mysql-bin.log

max_binlog_size = 1000M

binlog-format = row

2. Build data

建库

root@localhost [mysql]>create databasetest;

Query OK, 1 row affected (0.10 sec)



建表 

root@localhost [mysql]>create tabletest01( id int(4) not null auto_increment, name varchar(3), age int(4),primarykey(id))engine=innodb charset=utf8;

Query OK, 0 rows affected (0.13 sec)



插入测试数据

insert into test01(id,name,age)values(1,'tom',25);

insert into test01(id,name,age) values(2,'小丫',23);

insert into test01(id,name,age) values(3,'飞飞',28);

insert into test01(id,name,age) values(4,'参参',16);

insert into test01(id,name,age) values(5,'顺子',330);

insert into test01(id,name,age) values(6,'炸弹',48);



检查测试数据 

root@localhost [mysql]>select * fromtest01;

+----+--------+------+

| id | name  | age  |

+----+--------+------+

|  1 |tom    |  25 |

|  2 | 小丫   |   23|

|  3 | 飞飞   |   28|

|  4 | 参参   |   16|

|  5 | 顺子   |  330|

|  6 | 炸弹   |   48|

+----+--------+------+

6 rows in set (0.00 sec)

3. Install binlog2sql

git clone https://github.com/danfengcao/binlog2sql.git
pip install -r requirements.txt

 

4. Simulate deleted data recovery

删除数据

root@localhost [test]>select * fromtest01;

+----+--------+------+

| id | name  | age  |

+----+--------+------+

|  1 |tom    |  25 |

|  2 | 小丫   |   23|

|  3 | 飞飞   |   28|

|  4 | 参参   |   16|

|  5 | 顺子   |  330|

|  6 | 炸弹   |   48|

+----+--------+------+

6 rows in set (0.14 sec)



root@localhost [test]>delete from test01;

Query OK, 6 rows affected (0.09 sec)



root@localhost [test]>select * fromtest01;

Empty set (0.00 sec)

Log in to MySQL and view the current binlog file

root@localhost [test]>show master logs;

+------------------+-----------+

| Log_name         | File_size |

+------------------+-----------+

| mysql-bin.000001 |     65396 |

| mysql-bin.000002 |   1179102 |

| mysql-bin.000003 |      3401 |

+------------------+-----------+

3 rows in set (0.03 sec)

The latest binlog file is mysql-bin.000002, we will locate the binlog location of misoperation SQL

[root@bogon ~]# pythonbinlog2sql/binlog2sql/binlog2sql.py -h127.0.0.1 -P3306 -dtest -ttest01--start-file='mysql-bin.000004'

INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (25, 1, 'tom'); #start 239 end 412

INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (23, 2, '小丫'); #start491 end 667

INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (28, 3, '飞飞'); #start746 end 922

INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (16, 4, '参参'); #start1001 end 1177

INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (330, 5, '顺子'); #start1256 end 1432

INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (48, 6, '炸弹'); #start1511 end 1687

DELETE FROM `test`.`test01` WHERE `age`=25AND `id`=1 AND `name`='tom' LIMIT 1; #start 1766 end 2019

DELETE FROM `test`.`test01` WHERE `age`=23AND `id`=2 AND `name`='小丫' LIMIT 1;#start 1766 end 2019

DELETE FROM `test`.`test01` WHERE `age`=28AND `id`=3 AND `name`='飞飞' LIMIT 1;#start 1766 end 2019

DELETE FROM `test`.`test01` WHERE `age`=16AND `id`=4 AND `name`='参参' LIMIT 1;#start 1766 end 2019

DELETE FROM `test`.`test01` WHERE `age`=330AND `id`=5 AND `name`='顺子' LIMIT 1;#start 1766 end 2019

DELETE FROM `test`.`test01` WHERE `age`=48AND `id`=6 AND `name`='炸弹' LIMIT 1;#start 1766 end 2019

Generate rollback sql and check whether the rollback sql is correct

[root@bogon ~]# pythonbinlog2sql/binlog2sql/binlog2sql.py -h127.0.0.1 -P3306 -dtest -ttest01--start-file='mysql-bin.000004' --start-pos=1766 --end-pos=2019 -B

INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (48, 6, '炸弹'); #start1766 end 2019

INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (330, 5, '顺子'); #start1766 end 2019

INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (16, 4, '参参'); #start1766 end 2019

INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (28, 3, '飞飞'); #start1766 end 2019

INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (23, 2, '小丫'); #start1766 end 2019

INSERT INTO `test`.`test01`(`age`, `id`,`name`) VALUES (25, 1, 'tom'); #start 1766 end 2019

Confirm that the rollback SQL is correct, and execute the rollback statement.

[root@bogon ~]# pythonbinlog2sql/binlog2sql/binlog2sql.py -h127.0.0.1 -P3306 -dtest -ttest01--start-file='mysql-bin.000004' --start-pos=1766 --end-pos=2019 -B | mysql-h127.0.0.1 -P3306

Log in to mysql, the data rollback is successful

root@localhost [test]>select * fromtest01;

+----+--------+------+

| id | name  | age  |

+----+--------+------+

|  1 |tom    |  25 |

|  2 | 小丫   |   23|

|  3 | 飞飞   |   28|

|  4 | 参参   |   16|

|  5 | 顺子   |  330|

|  6 | 炸弹   |   48|

+----+--------+------+

(END)

PS: This software is suitable for quick flashback of delete and update misoperations. I have not tested other functions. Thanks again to the author for the open source.

 

In order to facilitate everyone to communicate, I have opened a WeChat public account and a QQ group, QQ group: 291519319, let’s communicate with those who like technology

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324937152&siteId=291194637