MySQL闪回工具之MyFlash


摘要:用于快速恢复MySQL的DML误操作,适用于mysql5.6、5.7且binlog格式为row。

下载安装

MyFlash是由美团点评公司技术工程部开发维护的一个回滚DML操作的工具。该工具通过解析v4版本的binlog,完成回滚操作。相对已有的回滚工具,其增加了更多的过滤选项,让回滚更加容易。

下载地址

https://github.com/Meituan-Dianping/MyFlash

安装

[root@mysql MyFlash-master]# cat build.sh
gcc -w `pkg-config --cflags --libs glib-2.0` source/binlogParseGlib.c -o binary/flashback
[root@mysql MyFlash-master]# sh build.sh
Package glib-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `glib-2.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'glib-2.0' found
source/binlogParseGlib.c:2:18: error: glib.h: No such file or directory
[root@mysql MyFlash-master]# yum install libgnomeui-devel -y #安装glib
[root@mysql MyFlash-master]# sh build.sh

查看帮助

[root@mysql MyFlash-master]# cd binary/
[root@mysql binary]# ./flashback --help
Usage:
flashback [OPTION...]
Help Options:
-h, --help Show help options
Application Options:
--databaseNames databaseName to apply. if multiple, seperate by comma(,)
--tableNames tableName to apply. if multiple, seperate by comma(,)
--start-position start position
--stop-position stop position
--start-datetime start time (format %Y-%m-%d %H:%M:%S)
--stop-datetime stop time (format %Y-%m-%d %H:%M:%S)
--sqlTypes sql type to filter . support INSERT, UPDATE ,DELETE. if multiple, seperate by comma(,)
--maxSplitSize max file size after split, the uint is M
--binlogFileNames binlog files to process. if multiple, seperate by comma(,)
--outBinlogFileNameBase output binlog file name base
--logLevel log level, available option is debug,warning,error
--include-gtids gtids to process
--exclude-gtids gtids to skip

测试用例

回滚insert

插入一条语句:

root@test 11:51:47>insert into t1 values(2,22,'b');
Query OK, 1 row affected (0.00 sec)
root@test 11:52:04>show master status \G;
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 15650
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
root@test 11:53:17>insert into t1 values(3,33,'c');
Query OK, 1 row affected (0.01 sec)
root@test 11:53:22>show master status \G;
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 15849
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
root@test 11:53:26>

生成回滚语句:–outBinlogFileNameBase指定输出的binlog文件前缀,如不指定,则默认为binlog_output_base.flashback。文件名后缀为:.flashback

[root@mysql binary]# ./flashback --sqlTypes='INSERT' --binlogFileNames=/data/mysql/log/mysql-bin.000001 --start-position=15650 --stop-position=15849 --outBinlogFileNameBase=insert

查看回滚语句:输出的回滚日志依然是binlog格式所以要用mysqlbinlog查看

[root@mysql binary]# ls -lrt insert.flashback
-rw-r--r--. 1 root root 216 Nov 17 11:55 insert.flashback
[root@mysql binary]# /opt/mysql/bin/mysqlbinlog -vvv insert.flashback
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#171117 9:49:12 server id 3 end_log_pos 120 CRC32 0x408f46ed Start: binlog v 4, server v 5.6.12-r5436-log created 171117 9:49:12 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
GEAOWg8DAAAAdAAAAHgAAAABAAQANS42LjEyLXI1NDM2LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAYQA5aEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAe1G
j0A=
'/*!*/;
# at 120
#171117 11:53:22 server id 3 end_log_pos 169 CRC32 0x462ec044 Table_map: `test`.`t1` mapped to number 2105
# at 169
#171117 11:53:22 server id 3 end_log_pos 216 CRC32 0x7cef2b2f Delete_rows: table id 2105 flags: STMT_END_F
BINLOG '
Ml0OWhMDAAAAMQAAAKkAAAAAADkIAAAAAAEABHRlc3QAAnQxAAMDAw8CgAEGRMAuRg==
Ml0OWiADAAAALwAAANgAAAAAADkIAAAAAAEAAgAD//gDAAAAIQAAAAEAYy8r73w=
'/*!*/;
### DELETE FROM `test`.`t1`
### WHERE
### @1=3 /* INT meta=0 nullable=0 is_null=0 */
### @2=33 /* INT meta=0 nullable=1 is_null=0 */
### @3='c' /* VARSTRING(384) meta=384 nullable=1 is_null=0 */
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;

执行回滚:

[root@mysql binary]# mysql -uxldev -pxueledev -h192.168.1.3 -Dtest -e"select * from t1;"
Warning: Using a password on the command line interface can be insecure.
+----+------+------+
| id | c1 | c2 |
+----+------+------+
| 1 | 11 | a |
| 2 | 22 | b |
| 3 | 33 | c |
+----+------+------+
[root@mysql binary]# /opt/mysql/bin/mysqlbinlog insert.flashback|mysql -uxldev -pxueledev -h192.168.1.3 -Dtest
Warning: Using a password on the command line interface can be insecure.
[root@mysql binary]# mysql -uxldev -pxueledev -h192.168.1.3 -Dtest -e"select * from t1;"
Warning: Using a password on the command line interface can be insecure.
+----+------+------+
| id | c1 | c2 |
+----+------+------+
| 1 | 11 | a |
| 2 | 22 | b |
+----+------+------+
可以看到最后插入的数据被delete掉了。

回滚update

[root@mysql binary]# mysql -uxldev -pxueledev -h192.168.1.3 -Dtest -e"update t1 set c2='bb' where id=2;"
Warning: Using a password on the command line interface can be insecure.
[root@mysql binary]# mysql -uxldev -pxueledev -h192.168.1.3 -Dtest -e"select * from t1;"
Warning: Using a password on the command line interface can be insecure.
+----+------+------+
| id | c1 | c2 |
+----+------+------+
| 1 | 11 | a |
| 2 | 22 | bb |
+----+------+------+
[root@mysql binary]# ./flashback --sqlTypes='UPDATE' --binlogFileNames=/data/mysql/log/mysql-bin.000001 --start-position=15849 --outBinlogFileNameBase=update
[root@mysql binary]# /opt/mysql/bin/mysqlbinlog update.flashback|mysql -uxldev -pxueledev -h192.168.1.3 -Dtest
Warning: Using a password on the command line interface can be insecure.
[root@mysql binary]# mysql -uxldev -pxueledev -h192.168.1.3 -Dtest -e"select * from t1;"
Warning: Using a password on the command line interface can be insecure.
+----+------+------+
| id | c1 | c2 |
+----+------+------+
| 1 | 11 | a |
| 2 | 22 | b |
+----+------+------+

回滚delete

[root@mysql binary]# mysql -uxldev -pxueledev -h192.168.1.3 -Dtest -e"select * from t1;show master status;"
Warning: Using a password on the command line interface can be insecure.
+----+------+------+
| id | c1 | c2 |
+----+------+------+
| 1 | 11 | a |
| 2 | 22 | b |
+----+------+------+
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 16474 | | | |
+------------------+----------+--------------+------------------+-------------------+
[root@mysql binary]# mysql -uxldev -pxueledev -h192.168.1.3 -Dtest -e"delete from t1;show master status;select * from t1;"
Warning: Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 16685 | | | |
+------------------+----------+--------------+------------------+-------------------+
[root@mysql binary]# ./flashback --sqlTypes='DELETE' --binlogFileNames=/data/mysql/log/mysql-bin.000001 --start-position=16474 --stop-position=16685 --outBinlogFileNameBase=delete
[root@mysql binary]# /opt/mysql/bin/mysqlbinlog delete.flashback|mysql -uxldev -pxueledev -h192.168.1.3 -Dtest
Warning: Using a password on the command line interface can be insecure.
[root@mysql binary]# mysql -uxldev -pxueledev -h192.168.1.3 -Dtest -e"select * from t1;"
Warning: Using a password on the command line interface can be insecure.
+----+------+------+
| id | c1 | c2 |
+----+------+------+
| 1 | 11 | a |
| 2 | 22 | b |
+----+------+------+
[root@mysql binary]#

回滚混合DML

[root@mysql binary]# ./flashback --sqlTypes='INSERT,UPDATE,DELETE' --binlogFileNames=/data/mysql/log/mysql-bin.000001 --start-position=15650 --stop-position=16685 --outBinlogFileNameBase=all
[root@mysql binary]# /opt/mysql/bin/mysqlbinlog -vvv all.flashback
输出结果省略

更多详情请参考:
https://github.com/Meituan-Dianping/MyFlash/blob/master/doc/how_to_use.md
http://url.cn/5yVTfLY

文章目录
  1. 1. 下载安装
    1. 1.1. 下载地址
    2. 1.2. 安装
    3. 1.3. 查看帮助
  2. 2. 测试用例
    1. 2.1. 回滚insert
    2. 2.2. 回滚update
    3. 2.3. 回滚delete
    4. 2.4. 回滚混合DML
|