1. 程式人生 > >binlog-rollback.pl基於binlog位置點和時間點恢復delete誤刪語句

binlog-rollback.pl基於binlog位置點和時間點恢復delete誤刪語句

基於位置點和時間點恢復delete誤刪

一、基於binlog位置點的恢復

為了演示,刷新一個新的binlog文件,讓它單獨記錄delete刪除語句:

MySQL [zhangyou]> flush logs
Query OK, 0 rows affected (0.09 sec)

MySQL [zhangyou]> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000009
         Position: 120
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)
MySQL [zhangyou]> select * from dede_scores;
+----+--------+------+----------+-----------+
| id | titles | icon | integral | isdefault |
+----+--------+------+----------+-----------+
|  2 | 列兵   |    1 |        0 |         1 |
|  3 | 班長   |    2 |     1000 |         1 |
|  4 | 少尉   |    3 |     2000 |         1 |
|  5 | 中尉   |    4 |     3000 |         1 |
|  6 | 上尉   |    5 |     4000 |         1 |
|  7 | 少校   |    6 |     5000 |         1 |
|  8 | 中校   |    7 |     6000 |         1 |
|  9 | 上校   |    8 |     9000 |         1 |
| 10 | 少將   |    9 |    14000 |         1 |
| 11 | 中將   |   10 |    19000 |         1 |
| 12 | 上將   |   11 |    24000 |         1 |
| 15 | 大將   |   12 |    29000 |         1 |
+----+--------+------+----------+-----------+

delete不加條件刪除表數據:

MySQL [zhangyou]> delete from dede_scores;
Query OK, 12 rows affected (0.00 sec)

MySQL [zhangyou]> select * from dede_scores;
Empty set (0.00 sec)

MySQL [zhangyou]> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000009
         Position: 600
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

按照binlog位置點來生成回滾的sql語句:

[root@git-server opt]# perl binlog-rollback.pl -f ‘/data/mysql/data/mysql-bin.000009‘ -o ‘/tmp/t.sql‘ -h ‘127.0.0.1‘ -u ‘admin‘ -p ‘admin‘ --start-position=120 --stop-position=600
Warning: Using a password on the command line interface can be insecure.

[root@git-server data]# less /tmp/t.sql 
INSERT INTO `zhangyou`.`dede_scores` SET `id`=15, `titles`=‘大將‘, `icon`=12, `integral`=29000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=12, `titles`=‘上將‘, `icon`=11, `integral`=24000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=11, `titles`=‘中將‘, `icon`=10, `integral`=19000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=10, `titles`=‘少將‘, `icon`=9, `integral`=14000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=9, `titles`=‘上校‘, `icon`=8, `integral`=9000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=8, `titles`=‘中校‘, `icon`=7, `integral`=6000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=7, `titles`=‘少校‘, `icon`=6, `integral`=5000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=6, `titles`=‘上尉‘, `icon`=5, `integral`=4000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=5, `titles`=‘中尉‘, `icon`=4, `integral`=3000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=4, `titles`=‘少尉‘, `icon`=3, `integral`=2000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=3, `titles`=‘班長‘, `icon`=2, `integral`=1000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=2, `titles`=‘列兵‘, `icon`=1, `integral`=0, `isdefault`=1;

MySQL [zhangyou]> source /tmp/t.sql
MySQL [zhangyou]> select * from dede_scores;
+----+--------+------+----------+-----------+
| id | titles | icon | integral | isdefault |
+----+--------+------+----------+-----------+
|  2 | 列兵   |    1 |        0 |         1 |
|  3 | 班長   |    2 |     1000 |         1 |
|  4 | 少尉   |    3 |     2000 |         1 |
|  5 | 中尉   |    4 |     3000 |         1 |
|  6 | 上尉   |    5 |     4000 |         1 |
|  7 | 少校   |    6 |     5000 |         1 |
|  8 | 中校   |    7 |     6000 |         1 |
|  9 | 上校   |    8 |     9000 |         1 |
| 10 | 少將   |    9 |    14000 |         1 |
| 11 | 中將   |   10 |    19000 |         1 |
| 12 | 上將   |   11 |    24000 |         1 |
| 15 | 大將   |   12 |    29000 |         1 |
+----+--------+------+----------+-----------+
12 rows in set (0.00 sec)

到此處基於binlog位置點的恢復成功

二、基於binlog時間點的恢復

[root@git-server opt]# perl binlog-rollback.pl -f ‘/data/mysql/data/mysql-bin.000009‘ -o ‘/tmp/t1.sql‘ -h ‘127.0.0.1‘ -u ‘admin‘ -p ‘admin‘ --start-datetime=‘2018-06-06 15:37:53‘ --stop-datetime=‘2018-06-06 15:52:54‘
Warning: Using a password on the command line interface can be insecure.
[root@git-server opt]# less /tmp/t1.sql 
INSERT INTO `zhangyou`.`dede_scores` SET `id`=15, `titles`=‘大將‘, `icon`=12, `integral`=29000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=12, `titles`=‘上將‘, `icon`=11, `integral`=24000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=11, `titles`=‘中將‘, `icon`=10, `integral`=19000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=10, `titles`=‘少將‘, `icon`=9, `integral`=14000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=9, `titles`=‘上校‘, `icon`=8, `integral`=9000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=8, `titles`=‘中校‘, `icon`=7, `integral`=6000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=7, `titles`=‘少校‘, `icon`=6, `integral`=5000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=6, `titles`=‘上尉‘, `icon`=5, `integral`=4000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=5, `titles`=‘中尉‘, `icon`=4, `integral`=3000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=4, `titles`=‘少尉‘, `icon`=3, `integral`=2000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=3, `titles`=‘班長‘, `icon`=2, `integral`=1000, `isdefault`=1;
INSERT INTO `zhangyou`.`dede_scores` SET `id`=2, `titles`=‘列兵‘, `icon`=1, `integral`=0, `isdefault`=1;

binlog-rollback.pl基於binlog位置點和時間點恢復delete誤刪語句