項目做者:danfengcaopython
項目地址:https://github.com/danfengcao/binlog2sqlmysql
mysql server必須開啓,離線模式下不能解析binloggit
binlog格式必須是行模式github
軟件版本:Python 2.7 MySQL 5.6sql
MySQL設置一下參數:微信
[mysqld] server-id = 1 log_bin = /var/log/mysql/mysql-bin.log max_binlog_size = 1000M binlog-format = row
建庫 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)
git clone https://github.com/danfengcao/binlog2sql.git pip install -r requirements.txt
刪除數據 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)
登錄MySQL,查看目前的binlog文件測試
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)
最新的binlog文件是mysql-bin.000002,咱們再定位誤操做SQL的binlog位置ui
[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
生成回滾sql,並檢查回滾sql是否正確spa
[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
確認回滾sql正確,執行回滾語句。code
[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
登陸mysql,數據回滾成功
root@localhost [test]>select * fromtest01; +----+--------+------+ | id | name | age | +----+--------+------+ | 1 |tom | 25 | | 2 | 小丫 | 23| | 3 | 飛飛 | 28| | 4 | 參參 | 16| | 5 | 順子 | 330| | 6 | 炸彈 | 48| +----+--------+------+ (END)
PS:本軟件適用於delete和updete誤操做快速閃回,其餘功能本人尚未測試過,再次感謝做者的開源。
爲了方便你們交流,本人開通了微信公衆號和QQ羣,QQ羣:291519319,喜歡技術的一塊兒來交流吧