1. 程式人生 > >【mysql 資料表恢復】誤刪整張表資料,需要緊急回滾

【mysql 資料表恢復】誤刪整張表資料,需要緊急回滾

應用案例

誤刪整張表資料,需要緊急回滾

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)

恢復資料步驟

  1. 登入mysql,檢視目前的binlog檔案

    mysql> show master status;
    +------------------+-----------+
    | Log_name         | File_size |
    +------------------+-----------+
    | mysql-bin.000051
    | 967 | | mysql-bin.000052 | 965 | +------------------+-----------+
  2. 最新的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
  3. 我們得到了誤操作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
  4. 確認回滾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 |
    +----+--------+---------------------+