MySQL闪回恢复数据

一、应用场景

表误删除,特别是批量数据,生产环境线上业务,影响非常大,需要快速回滚。

二、闪回工具有3类

1、官方的mysqlbinlog:需要脚本处理,在插入数据库;

2、第三方的mysqlbinlog工具:用参数-B;

3、开源的binlog2sql:兼容性好,使用难度小,线上闪回。

三、闪回恢复

3.1  利用官方的mysqlbinlog

确定日志类型为row

mysql> show variables like '%binlog%';
+-----------------------------------------+----------------------+
| Variable_name                           | Value                |
+-----------------------------------------+----------------------+
| binlog_cache_size                       | 32768                |
| binlog_checksum                         | CRC32                |
| binlog_direct_non_transactional_updates | OFF                  |
| binlog_error_action                     | ABORT_SERVER         |
| binlog_format                           | ROW                  |

查看原表的内容:

mysql> select * from yddl2;
+------+---------------------+
| id   | timepoint           |
+------+---------------------+
|   10 | 2022-10-13 15:01:36 |
|    7 | 2022-10-18 21:45:37 |
|    8 | 2022-10-18 21:45:40 |
+------+---------------------+
3 rows in set (0.00 sec)

原来操作要删除一行数据,结果将整个表删除:

mysql> delete from yddl2;
Query OK, 3 rows affected (0.03 sec)

mysql> select * from yddl2;
Empty set (0.00 sec)

 为了不产生新的数据先锁表:

mysql> lock tables yddl2 read;
Query OK, 0 rows affected (0.00 sec)

查看当前的日志:

mysql> show master status;
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| BIN_LOG.000002 |      433 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

查看详细的日志:

[root@localhost binlog]# mysqlbinlog --base64-output=DECODE-ROWS -v -v BIN_LOG.000002;

过滤删除一段的日志:

[root@localhost binlog]# mysqlbinlog --base64-output=DECODE-ROWS -v -v BIN_LOG.000002 |sed -n '/BEGIN/,/COMMIT/p'>11.txt

继续过滤日志变为插入语句:

[root@localhost binlog]# cat 11.txt | sed -n '/###/p' |sed 's/### //g;s/\/\*.*/,/g;s/DELETE FROM/INSERT INTO/g;s/WHERE/SELECT/g;'|sed -r 's/(@4.*),/\1;/g'| sed 's/@[1-9]=//g'>33.txt

查看过滤后的文件

[root@localhost binlog]# cat 33.txt
INSERT INTO `jl`.`yddl2`
SELECT
  10 ,
  '2022-10-13 15:01:36' ,
INSERT INTO `jl`.`yddl2`
SELECT
  7 ,
  '2022-10-18 21:45:37' ,
INSERT INTO `jl`.`yddl2`
SELECT
  8 ,
  '2022-10-18 21:45:40' ,

修改表的锁:

mysql> unlock tables;
Query OK, 0 rows affected (0.01 sec)

mysql> lock tables yddl2 write;
Query OK, 0 rows affected (0.00 sec)

导入数据:

mysql> source /mysql/data/33.sql
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

解锁表:

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

查询数据恢复正常。

3.2 利用 mysqlbinlog_flushback恢复

3.2.1 下载软件https://github.com/58daojia-dba/mysqlbinlog_flashback

 

 

 

3.2.2 上传主机解压

[root@localhost mysqlbinlog_flashback-master]# ll
总用量 104
-rw-r--r-- 1 root root  4766 12月 19 2016 binlogstream.py.diff
-rw-r--r-- 1 root root   524 12月 19 2016 CHANGELOG.txt
-rw-r--r-- 1 root root  1365 12月 19 2016 constant.py
-rw-r--r-- 1 root root 13730 12月 19 2016 flashback.py
-rw-r--r-- 1 root root 12666 10月 20 22:08 flashback.pyc
-rw-r--r-- 1 root root  1800 12月 19 2016 func.py
-rw-r--r-- 1 root root  1781 10月 20 22:08 func.pyc
drwxr-xr-x 2 root root    36 12月 19 2016 internal
-rw-r--r-- 1 root root 10015 12月 19 2016 joint_sql.py
-rw-r--r-- 1 root root 11357 12月 19 2016 LICENSE
drwxr-xr-x 2 root root    22 12月 19 2016 log
-rw-r--r-- 1 root root  9192 12月 19 2016 mysqlbinlog_back.py
-rw-r--r-- 1 root root  2782 12月 19 2016 mysql_table.py
drwxr-xr-x 4 root root   240 10月 20 22:08 pymysqlreplication
-rw-r--r-- 1 root root  5033 12月 19 2016 README.md
drwxr-xr-x 3 root root   136 12月 19 2016 test

3.3.3  安装相关组件

[root@localhost ~]# yum install python-pip

参考资料:

 https://blog.csdn.net/weixin_36337403/article/details/113201571

3.4 利用第三方工具binlog2sql

 

posted @ 2022-10-21 09:43  中仕  阅读(399)  评论(0编辑  收藏  举报