從MySQL binlog解析出你要的SQL。根據不一樣選項,你能夠獲得原始SQL、回滾SQL、去除主鍵的INSERT SQL等。python
正常維護。應用於部分公司線上環境。mysql
shell> git clone https://github.com/danfengcao/binlog2sql.git && cd binlog2sql shell> pip install -r requirements.txt
git與pip的安裝問題請自行搜索解決。git
[mysqld] server_id = 1 log_bin = /var/log/mysql/mysql-bin.log max_binlog_size = 1G binlog_format = row binlog_row_image = full
select, super/replication client, replication slave 建議受權 GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO
權限說明github
解析出標準SQLsql
shell> python binlog2sql.py -h127.0.0.1 -P3306 -uadmin -p'admin' -dtest -t test3 test4 --start-file='mysql-bin.000002' 輸出: INSERT INTO `test`.`test3`(`addtime`, `data`, `id`) VALUES ('2016-12-10 13:03:38', 'english', 4); #start 570 end 736 UPDATE `test`.`test3` SET `addtime`='2016-12-10 12:00:00', `data`='中文', `id`=3 WHERE `addtime`='2016-12-10 13:03:22' AND `data`='中文' AND `id`=3 LIMIT 1; #start 763 end 954 DELETE FROM `test`.`test3` WHERE `addtime`='2016-12-10 13:03:38' AND `data`='english' AND `id`=4 LIMIT 1; #start 981 end 1147
解析出回滾SQLshell
shell> python binlog2sql.py --flashback -h127.0.0.1 -P3306 -uadmin -p'admin' -dtest -ttest3 --start-file='mysql-bin.000002' --start-position=763 --stop-position=1147 輸出: INSERT INTO `test`.`test3`(`addtime`, `data`, `id`) VALUES ('2016-12-10 13:03:38', 'english', 4); #start 981 end 1147 UPDATE `test`.`test3` SET `addtime`='2016-12-10 13:03:22', `data`='中文', `id`=3 WHERE `addtime`='2016-12-10 12:00:00' AND `data`='中文' AND `id`=3 LIMIT 1; #start 763 end 954
mysql鏈接配置編輯器
-h host; -P port; -u user; -p password測試
解析模式ui
--stop-never 持續解析binlog。可選。默認False,同步至執行命令時最新的binlog位置。spa
-K, --no-primary-key 對INSERT語句去除主鍵。可選。默認False
-B, --flashback 生成回滾SQL,可解析大文件,不受內存限制。可選。默認False。與stop-never或no-primary-key不能同時添加。
--back-interval -B模式下,每打印一千行回滾SQL,加一句SLEEP多少秒,如不想加SLEEP,請設爲0。可選。默認1.0。
解析範圍控制
--start-file 起始解析文件,只需文件名,無需全路徑 。必須。
--start-position/--start-pos 起始解析位置。可選。默認爲start-file的起始位置。
--stop-file/--end-file 終止解析文件。可選。默認爲start-file同一個文件。若解析模式爲stop-never,此選項失效。
--stop-position/--end-pos 終止解析位置。可選。默認爲stop-file的最末位置;若解析模式爲stop-never,此選項失效。
--start-datetime 起始解析時間,格式'%Y-%m-%d %H:%M:%S'。可選。默認不過濾。
--stop-datetime 終止解析時間,格式'%Y-%m-%d %H:%M:%S'。可選。默認不過濾。
對象過濾
-d, --databases 只解析目標db的sql,多個庫用空格隔開,如-d db1 db2。可選。默認爲空。
-t, --tables 只解析目標table的sql,多張表用空格隔開,如-t tbl1 tbl2。可選。默認爲空。
--only-dml 只解析dml,忽略ddl。可選。默認False。
--sql-type 只解析指定類型,支持INSERT, UPDATE, DELETE。多個類型用空格隔開,如--sql-type INSERT DELETE。可選。默認爲增刪改都解析。用了此參數但沒填任何類型,則三者都不解析。
閃回詳細介紹可參見example目錄下《閃回原理與實戰》example/mysql-flashback-priciple-and-practice.md
test庫tbl表原有數據
mysql> select * from tbl; +----+--------+---------------------+ | id | name | addtime | +----+--------+---------------------+ | 1 | 小趙 | 2016-12-10 00:04:33 | | 2 | 小錢 | 2016-12-10 00:04:48 | | 3 | 小孫 | 2016-12-13 20:25:00 | | 4 | 小李 | 2016-12-12 00:00:00 | +----+--------+---------------------+ 4 rows in set (0.00 sec) mysql> delete from tbl; Query OK, 4 rows affected (0.00 sec) 20:28時,tbl表誤操做被清空 mysql> select * from tbl; Empty set (0.00 sec)
恢復數據步驟:
登陸mysql,查看目前的binlog文件
mysql> show master status; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000051 | 967 | | mysql-bin.000052 | 965 | +------------------+-----------+
最新的binlog文件是mysql-bin.000052,咱們再定位誤操做SQL的binlog位置。誤操做人只能知道大體的誤操做時間,咱們根據大體時間過濾數據。
shell> python binlog2sql/binlog2sql.py -h127.0.0.1 -P3306 -uadmin -p'admin' -dtest -ttbl --start-file='mysql-bin.000052' --start-datetime='2016-12-13 20:25:00' --stop-datetime='2016-12-13 20:30:00' 輸出: INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-13 20:26:00', 4, '小李'); #start 317 end 487 time 2016-12-13 20:26:26 UPDATE `test`.`tbl` SET `addtime`='2016-12-12 00:00:00', `id`=4, `name`='小李' WHERE `addtime`='2016-12-13 20:26:00' AND `id`=4 AND `name`='小李' LIMIT 1; #start 514 end 701 time 2016-12-13 20:27:07 DELETE FROM `test`.`tbl` WHERE `addtime`='2016-12-10 00:04:33' AND `id`=1 AND `name`='小趙' LIMIT 1; #start 728 end 938 time 2016-12-13 20:28:05 DELETE FROM `test`.`tbl` WHERE `addtime`='2016-12-10 00:04:48' AND `id`=2 AND `name`='小錢' LIMIT 1; #start 728 end 938 time 2016-12-13 20:28:05 DELETE FROM `test`.`tbl` WHERE `addtime`='2016-12-13 20:25:00' AND `id`=3 AND `name`='小孫' LIMIT 1; #start 728 end 938 time 2016-12-13 20:28:05 DELETE FROM `test`.`tbl` WHERE `addtime`='2016-12-12 00:00:00' AND `id`=4 AND `name`='小李' LIMIT 1; #start 728 end 938 time 2016-12-13 20:28:05
咱們獲得了誤操做sql的準確位置在728-938之間,再根據位置進一步過濾,使用flashback模式生成回滾sql,檢查回滾sql是否正確(注:真實環境下,此步常常會進一步篩選出須要的sql。結合grep、編輯器等)
shell> python binlog2sql/binlog2sql.py -h127.0.0.1 -P3306 -uadmin -p'admin' -dtest -ttbl --start-file='mysql-bin.000052' --start-position=3346 --stop-position=3556 -B > rollback.sql | cat 輸出: INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-12 00:00:00', 4, '小李'); #start 728 end 938 time 2016-12-13 20:28:05 INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-13 20:25:00', 3, '小孫'); #start 728 end 938 time 2016-12-13 20:28:05 INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-10 00:04:48', 2, '小錢'); #start 728 end 938 time 2016-12-13 20:28:05 INSERT INTO `test`.`tbl`(`addtime`, `id`, `name`) VALUES ('2016-12-10 00:04:33', 1, '小趙'); #start 728 end 938 time 2016-12-13 20:28:05
確認回滾sql正確,執行回滾語句。登陸mysql確認,數據回滾成功。
shell> mysql -h127.0.0.1 -P3306 -uadmin -p'admin' < rollback.sql mysql> select * from tbl; +----+--------+---------------------+ | id | name | addtime | +----+--------+---------------------+ | 1 | 小趙 | 2016-12-10 00:04:33 | | 2 | 小錢 | 2016-12-10 00:04:48 | | 3 | 小孫 | 2016-12-13 20:25:00 | | 4 | 小李 | 2016-12-12 00:00:00 | +----+--------+---------------------+