一、简介

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

二、详细说明

1.安装:

该工具推荐用户在下载源码之后,进行动态编译链接安装

git clone




mysql 数据库回滚操作 mysql数据库回滚工具_sql


动态编译链接

[root@bogon ~/soft]# cd MyFlash/

gcc -w `pkg-config --cflags --libs glib-2.0` source/binlogParseGlib.c -o binary/flashback


mysql 数据库回滚操作 mysql数据库回滚工具_mysql_02


解决方法:

yum install gcc glib2-devel -y

gcc -w `pkg-config --cflags --libs glib-2.0` source/binlogParseGlib.c -o binary/flashback

至此,安装完成。

静态编译链接

然而用户不想每次去重新编译,可以选择使用静态链接,但是该方法需要知道glib库的版本和位置,因此对于每台机器略有不同,请谨慎使用

为了保证在一台机器上编译后,可以在其它机器上使用,需要满足以下两个条件 a) 将glib做成静态链接 b)在编译的那台机器的glibc版本(查看方法为ldd --version)要小于等于要运行该软件的那台机器glibc版本 因此需要你在一台glibc版本较低的机器上运行如下命令即可。

gcc -w -g `pkg-config --cflags glib-2.0` source/binlogParseGlib.c -o binary/flashback /usr/lib64/libglib-2.0.a -lrt

2.使用:

1.How to use

cd binary
./flashback --help
Usage:
flashback [OPTION...]
Help Options:
-?, --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

2.Parameter explantions

下面的这些参数是可以任意组合的。

· 1.databaseNames

指定需要回滚的数据库名。多个数据库可以用","隔开。如果不指定该参数,相当于指定了所有数据库。

· 2.tableNames

指定需要回滚的表名。多个表可以用","隔开。如果不指定该参数,相当于指定了所有表。

· 3.start-position

指定回滚开始的位置。如不指定,从文件的开始处回滚。请指定正确的有效的位置,否则无法回滚

· 4.stop-position

指定回滚结束的位置。如不指定,回滚到文件结尾。请指定正确的有效的位置,否则无法回滚

· 5.start-datetime

指定回滚的开始时间。注意格式必须是 %Y-%m-%d %H:%M:%S。 如不指定,则不限定时间

· 6.stop-datetime

指定回滚的结束时间。注意格式必须是 %Y-%m-%d %H:%M:%S。 如不指定,则不限定时间

· 7.sqlTypes

指定需要回滚的sql类型。目前支持的过滤类型是INSERT, UPDATE ,DELETE。多个类型可以用","隔开。

· 8.maxSplitSize

一旦指定该参数,对文件进行固定尺寸的分割(单位为M),过滤条件有效,但不进行回滚操作。该参数主要用来将大的binlog文件切割,防止单次应用的binlog尺寸过大,对线上造成压力

· 9.binlogFileNames

指定需要回滚的binlog文件,目前只支持单个文件,后续会增加多个文件支持

· 10.outBinlogFileNameBase

指定输出的binlog文件前缀,如不指定,则默认为binlog_output_base.flashback

· 11.logLevel

仅供开发者使用,默认级别为error级别。在生产环境中不要修改这个级别,否则输出过多

· 12.include-gtids

指定需要回滚的gtid,支持gtid的单个和范围两种形式。

· 13.exclude-gtids

指定不需要回滚的gtid,用法同include-gtids

3.example

1.回滚整个文件

./flashback --binlogFileNames=haha.000041

mysqlbinlog binlog_output_base.flashback | mysql -h -u -p

2.回滚该文件中的所有insert语句

./flashback --sqlTypes='INSERT' --binlogFileNames=haha.000041

mysqlbinlog binlog_output_base.flashback | mysql -h -u -p

3.回滚大文件

回滚

./flashback --binlogFileNames=haha.000042

切割大文件

./flashback --maxSplitSize=1 --binlogFileNames=binlog_output_base.flashback

应用

mysqlbinlog binlog_output_base.flashback.000001 | mysql -h -u -p

...

mysqlbinlog binlog_output_base.flashback. | mysql -h -u -p

三、测试

建表插入测试数据如下:

create database t2;

use t2;

create table t1 (id int primary key,name int);

insert into t1 values(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10);

(1)单表删除恢复测试

mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 7 |
| 8 | 8 |
| 9 | 9 |
| 10 | 10 |
+------+------+
mysql> delete from t1 where id<6;
Query OK, 5 rows affected (0.00 sec)
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 6 | 6 |
| 7 | 7 |
| 8 | 8 |
| 9 | 9 |
| 10 | 10 |
+------+------+
5 rows in set (0.00 sec)

进入binlog所在目录:

查看删除操作的位置信息

mysqlbinlog --base64-output=decode-rows -vv mysql-bin.000004|grep -C 100 delete|more


mysql 数据库回滚操作 mysql数据库回滚工具_mysql_03


确认删除的开始和结束的位点信息后,生成flashback文件。

/root/soft/MyFlash/binary/flashback --binlogFileNames=mysql-bin.000004 --outBinlogFileNameBase=mysql-bin-000004 --tableNames=t1 --start-position=329 --stop-position=533

查看并确认即将恢复内容

mysqlbinlog --base64-output=decode-rows -vv mysql-bin-000004.flashback

恢复数据:

mysqlbinlog --skip-gtids mysql-bin-000004.flashback | mysql -uroot -p

查看恢复后数据:

mysql> select * from t1;
+----+------+
| id | name |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 7 |
| 8 | 8 |
| 9 | 9 |
| 10 | 10 |
+----+------+

(2)单表更新恢复测试

mysql> update t1 set name=10 where id >5;
Query OK, 4 rows affected (0.01 sec)
Rows matched: 5 Changed: 4 Warnings: 0
mysql> select * from t1;
+----+------+
| id | name |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 10 |
| 7 | 10 |
| 8 | 10 |
| 9 | 10 |
| 10 | 10 |
+----+------+
10 rows in set (0.00 sec)

进入binlog所在目录:

查看删除操作的位置信息

mysqlbinlog --base64-output=decode-rows -vv mysql-bin.000004|grep -C 100 update|more


mysql 数据库回滚操作 mysql数据库回滚工具_sql_04


确认删除的开始和结束的位点信息后,生成flashback文件(flashback文件名不能重复)。

/root/soft/MyFlash/binary/flashback --binlogFileNames=mysql-bin.000004 --outBinlogFileNameBase=mysql-bin-000004 --tableNames=t1 --start-position=2790 --stop-position=3030

查看并确认即将恢复内容

mysqlbinlog --base64-output=decode-rows -vv mysql-bin-000004.flashback

恢复数据:

mysqlbinlog --skip-gtids mysql-bin-000004.flashback | mysql -uroot -p

查看恢复后数据:

mysql> select * from t1;
+----+------+
| id | name |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 7 |
| 8 | 8 |
| 9 | 9 |
| 10 | 10 |
+----+------+

(3)单表插入恢复测试

mysql> insert into t1 values(11,11);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+----+------+
| id | name |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 7 |
| 8 | 8 |
| 9 | 9 |
| 10 | 10 |
| 11 | 11 |
+----+------+

进入binlog所在目录:

查看删除操作的位置信息

mysqlbinlog --base64-output=decode-rows -vv mysql-bin.000004|grep -C 100 'insert into t1 values(11,11)'


mysql 数据库回滚操作 mysql数据库回滚工具_sql_05


确认删除的开始和结束的位点信息后,生成flashback文件(flashback文件名不能重复)。

/root/soft/MyFlash/binary/flashback --binlogFileNames=mysql-bin.000004 --outBinlogFileNameBase=mysql-bin-000004 --tableNames=t1 --start-position=3722 --stop-position=3893

查看并确认即将恢复内容

mysqlbinlog --base64-output=decode-rows -vv mysql-bin-000004.flashback


mysql 数据库回滚操作 mysql数据库回滚工具_mysql_06


恢复数据:

mysqlbinlog --skip-gtids mysql-bin-000004.flashback | mysql -uroot -p

查看恢复后数据:

mysql> select * from t1;
+----+------+
| id | name |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
| 7 | 7 |
| 8 | 8 |
| 9 | 9 |
| 10 | 10 |
+----+------+

四、限制

1. binlog格式必须为row,且binlog_row_image=full

2. 仅支持5.6与5.7

3. 只能回滚DML(增、删、改)

参考链接:https://github.com/Meituan-Dianping/MyFlash