Use python script to achieve flush back of mysql misoperation

1. Introduction
In an oracle database, when a misoperation is committed, we can flash the table back to the state before the misoperation through the flashback function provided by oracle. There is no native flush back function in mysql. When the DBA misuses, the traditional recovery method is to use full backup + binary log roll forward for recovery.
Today, I will introduce to you a method of using python script to implement a flashback function similar to the flush back table in oracle in mysql. Compared with the traditional full backup + additional backup, this method is faster and simpler.

2. The principle of flashback
: call mysql_rollback.py (the script is in my other blog flashback script: mysql_rollback.py ) to reverse the binlog in rows format, delete reversely generates insert, update generates reverse update, insert generates delete in reverse.

3. Description
0. Python and MySQLdb modules need to be installed
1. The format of binlog must be row
2. The table structure to be restored has not changed before and after the operation, otherwise the script cannot be parsed
3. Only DML (insert/update/delete) is generated rollback statement, DDL statement cannot be rolled back
4. The final generated SQL is in reverse order, so the latest DML will be generated at the front of the input file, with timestamp and offset point, which is convenient to find the target
5. It is necessary to provide a The read-only user connecting to MySQL is mainly to obtain the table structure.
6. If the binlog is too large, it is recommended to bring a time range. You can also specify to restore only the SQL of a certain database.
7. After the SQL is generated, be sure to test the recovery on the test environment to be applied online

4. Actual combat
step1. Log in to mysql to view table information

mysql> use db1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from test;
+------+-------+------+---------+----------+
| id   | name  | age  | country | city     |
+------+-------+------+---------+----------+
|    1 | alex  |   26 | china   | shanghai |
|    2 | bob   |   25 | britain | london   |
|    3 | simon |   24 | france  | paris    |
+------+-------+------+---------+----------+
3 rows in set (0.00 sec)

step2. Simulate misoperation (update)

mysql> update test set country='europe' where name='bob';   --bob的国家被改为europe
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from test;
+------+-------+------+---------+----------+
| id   | name  | age  | country | city     |
+------+-------+------+---------+----------+
|    1 | alex  |   26 | china   | shanghai |
|    2 | bob   |   25 | europe  | london   |
|    3 | simon |   24 | france  | paris    |
+------+-------+------+---------+----------+
3 rows in set (0.00 sec)

mysql> exit;
Bye

step3. Analyze binlog and generate reverse statement
to find the latest binlog

SZD-L0087668:gzz3306:Master > ll
-rw-rw---- 1 mysql mysql      167 May  2 14:30 mysql-bin.000001
-rw-rw---- 1 mysql mysql 11400402 May  2 19:28 mysql-bin.000002
-rw-rw---- 1 mysql mysql     1807 May  2 19:49 mysql-bin.000003
-rw-rw---- 1 mysql mysql      660 May  2 20:10 mysql-bin.000004
-rw-rw---- 1 mysql mysql      403 May  2 20:10 mysql-bin.000005
-rw-rw---- 1 mysql mysql      584 May  3 10:45 mysql-bin.000006
-rw-rw---- 1 mysql mysql      417 May  3 10:53 mysql-bin.000007
-rw-rw---- 1 mysql mysql     1973 May  3 13:28 mysql-bin.000008
-rw-rw---- 1 mysql mysql     2604 May  3 14:13 **mysql-bin.000009**
-rw-rw---- 1 mysql mysql      369 May  3 13:28 mysql-bin.index
-rw-r--r-- 1 root  root     12222 Apr 13  2017 mysql_rollback.py

Find the misoperation sql in binlog according to the keyword europe, and output 30 lines before and after europe (the number of lines depends on the specific situation, be sure to output the BEGIN and COMMIT corresponding to the statement)

SZD-L0087668:gzz3306:Master > mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS --set-charset=utf8 mysql-bin.000009 | grep -C 30 'europe'
...
BEGIN
/*!*/;
#** at 2426**
#180503 14:13:36 server id 1  end_log_pos 2482 CRC32 0xe79b9612         Table_map: `db1`.`test` mapped to number 76
# at 2482
#180503 14:13:36 server id 1  end_log_pos 2573 CRC32 0xacd94a0b         Update_rows: table id 76 flags: STMT_END_F
### UPDATE `db1`.`test`
### WHERE
###   @1=2 /* INT meta=0 nullable=1 is_null=0 */
###   @2='bob' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */
###   @3=25 /* INT meta=0 nullable=1 is_null=0 */
###   @4='britain' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */
###   @5='london' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */
### SET
###   @1=2 /* INT meta=0 nullable=1 is_null=0 */
###   @2='bob' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */
###   @3=25 /* INT meta=0 nullable=1 is_null=0 */
###   @4='europe' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */
###   @5='london' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */
# at 2573
#180503 14:13:36 server id 1  end_log_pos **2604** CRC32 0x63b3d5fa         Xid = 118
COMMIT/*!*/;
...

Select 2426 and 2604 as the starting position for analyzing binlog

SZD-L0087668:gzz3306:Master > python2.7 mysql_rollback.py -f mysql-bin.000009 -o rollback.sql -h127.0.0.1 -P3306 -uroot -p123456 --start-position='2426' --stop-position='2604' -d db1
正在获取参数.....
正在解析binlog.....
正在初始化列名.....
正在开始拼凑sql.....
done!

View the reverse statement of the misoperation in rollback.sql

SZD-L0087668:gzz3306:Master > cat rollback.sql 
## at 2482
##180503 14:13:36 server id 1  end_log_pos 2573 CRC32 0xacd94a0b        Update_rows: table id 76 flags: STMT_END_F
UPDATE `db1`.`test`
SET
  id=2
  ,name='bob'
  ,age=25
  ,country='britain'
  ,city='london'
WHERE
  id=2
  AND name='bob'
  AND age=25
  AND country='europe'
  AND city='london';

step4. Rollback

SZD-L0087668:gzz3306:Master > mysql -uroot -p <rollback.sql 
Enter password: 
SZD-L0087668:gzz3306:Master > mysql -uroot -p -e 'select * from db1.test';
Enter password: 
+------+-------+------+---------+----------+
| id   | name  | age  | country | city     |
+------+-------+------+---------+----------+
|    1 | alex  |   26 | china   | shanghai |
|    2 | bob   |   25 | britain | london   |
|    3 | simon |   24 | france  | paris    |
+------+-------+------+---------+----------+

The test table has been rolled back.

Guess you like

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