实现MySQL回滚的Python脚本的编写教程
作者:goldensun 时间:2024-01-16 05:41:29
操作数据库时候难免会因为“大意”而误操作,需要快速恢复的话通过备份来恢复是不太可能的,因为需要还原和binlog差来恢复,等不了,很费时。这里先说明下因为Delete 操作的恢复方法:主要还是通过binlog来进行恢复,前提是binlog_format必须是Row格式,否则只能通过备份来恢复数据了。
方法:
条件:开启Binlog,Format为Row。
步骤:
1.通过MySQL自带工具mysqlbinlog 指定导出操作的记录:
mysqlbinlog
--no-defaults
--start-datetime='2012-12-25 14:56:00'
--stop-datetime='2012-12-25 14:57:00'
-vv mysql-bin.000001 > /home/zhoujy/restore/binlog.txt
2.数据取出来之后,需要把数据解析反转,原始数据:
### DELETE FROM test.me_info
### WHERE
### @1=2165974 /* INT meta=0 nullable=0 is_null=0 */
### @2='1984:03:17' /* DATE meta=0 nullable=1 is_null=0 */
### @3=NULL /* DATE meta=765 nullable=1 is_null=1 */
### @4=2012-10-25 00:00:00 /* DATETIME meta=0 nullable=0 is_null=0 */
### @5='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @6=0 /* TINYINT meta=0 nullable=1 is_null=0 */
### @7='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @8=-1 (4294967295) /* INT meta=0 nullable=1 is_null=0 */
### @9=0 /* MEDIUMINT meta=0 nullable=1 is_null=0 */
### @10=NULL /* MEDIUMINT meta=0 nullable=1 is_null=1 */
### @11=2 /* TINYINT meta=0 nullable=1 is_null=0 */
### @12=0 /* TINYINT meta=0 nullable=1 is_null=0 */
### @13='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @14='' /* VARSTRING(765) meta=765 nullable=1 is_null=0 */
### @15=0 /* MEDIUMINT meta=0 nullable=1 is_null=0 */
### @16=320 /* INT meta=0 nullable=1 is_null=0 */
……………………
……………………
……………………
Row格式的binlog记录的格式如上面所示,需要做的工作就是吧Delete的操作转换成Insert操作,发上面的都是有一定规律的,并且需要注意的是:
1、字段类型 DATETIME 日期。在日志中保存的格式为 @4=2012-10-25 00:00:00,需要将2012-10-25 00:00:00加上引号。
2、负数。在日志中保存的格式为 @1=-1 (4294967295), -2(4294967294),-3(4294967293),需要将()里面的数据去掉,只保留@1=-1。
3、转义字符集。如:'s,\,等。
上面3点清楚之后,可以写一个脚本(水平有限,在提升中,写的不好看):
#!/bin/env python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name: restore.py
# Purpose: 通过Binlog恢复Delete误操作数据
# Author: zhoujy
# Created: 2012-12-25
# update: 2012-12-25
# Copyright: (c) Mablevi 2012
# Licence: zjy
#-------------------------------------------------------------------------------
def read_binlog(file,column_num):
f=open(file)
num = '@'+str(column_num)
while True:
lines = f.readline()
if lines.strip()[0:3] == '###':
lines=lines.split(' ',3)
if lines[1] == 'DELETE' and lines[2] =='FROM': #该部分替换Delete为Insert
lines[1] = "INSERT"
lines[2] = 'INTO'
lines[-1] = lines[-1].strip()
if lines[1].strip() == 'WHERE':
lines[1] = 'VALUES ('
if ''.join(lines).find('@') <> -1 and lines[3].split('=',1)[0] <> num: #num为列数,要是小于最大的列数,后面均加,
lines[3] = lines[3].split('=',1)[-1].strip()
if lines[3].strip('\'').strip().find('\'') <> -1:
lines[3] = lines[3].split('/*')[0].strip('\'').strip().strip('\'').replace('\\','').replace('\'','\\\'') #这里过滤掉转义的字符串
lines[3] = '\'' + lines[3] + '\','
elif lines[3].find('INT meta') <> -1: #过滤Int类型的字段为负数后带的(),正数不受影响
lines[3] = lines[3].split('/*')[0].strip()
lines[3] = lines[3].split()[0] + ','
elif lines[3].find('NULL') <> -1:
lines[3] = lines[3].split('/*')[0].strip()
lines[3] = lines[3] + ','
else:
lines[3] = lines[3].split('/*')[0].strip('\'').strip().strip('\'').replace('\\','').replace('\'','\\\'') #这里过滤掉转义的字符串
lines[3] = '\'' + lines[3].strip('\''' ') + '\','
if ''.join(lines).find('@') <> -1 and lines[3].split('=',1)[0] == num: #num为列数,要是小于最大的列数,后面均加);
lines[3] = lines[3].split('=',1)[-1].strip()
if lines[3].find('\'') <> -1:
lines[3] = lines[3].split('/*')[0].strip('\'').strip().strip('\'').replace('\\','').replace('\'','\\\'') #同上
lines[3] = '\'' + lines[3] + '\');'
elif lines[3].find('INT meta') <> -1: #同上
lines[3] = lines[3].split('/*')[0].strip()
lines[3] = lines[3].split(' ')[0] + ');'
elif lines[3].find('NULL') <> -1:
lines[3] = lines[3].split('/*')[0].strip()
lines[3] = lines[3] + ');'
else:
lines[3] = lines[3].split('/*')[0].strip('\'').strip().strip('\'').replace('\\','').replace('\'','\\\'') #同上
lines[3] = '\'' + lines[3].strip('\''' ') + '\');'
print ' '.join(lines[1:])
if lines == '':
break
if __name__ == '__main__':
import sys
read_binlog(sys.argv[1],sys.argv[2])
执行脚本:
python restore.py binlog.txt 36 > binlog.sql
命令行中的36 表示 需要还原的表的字段有36个,效果:
INSERT INTO test.me_info
VALUES (
2123269,
'1990:11:12',
NULL,
2,
'',
0,
'',
-1,
0,
340800,
1,
0,
'',
……
……
1,
NULL
);
最后还原:
mysql test < binlog.sql
标签:MySQL,回滚
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ImageMagick FAQ 问题摘要
2008-10-21 13:14:00
PHP levenshtein()函数用法讲解
2023-06-01 15:20:29
![](https://img.aspxhome.com/file/2023/0/55380_0s.png)
Python入门必须知道的11个知识点
2023-12-19 05:32:47
Windows下MySQL详细安装过程及基本使用
2024-01-27 10:31:16
![](https://img.aspxhome.com/file/2023/5/104435_0s.jpg)
Node.js中的events事件模块知识点总结
2024-05-08 10:11:37
数据安全之MySQL安全的二十三条军规
2008-12-24 16:26:00
Python高级特性与几种函数的讲解
2021-12-09 03:37:17
基于Opencv的图像卡通化实现代码
2023-07-10 06:58:55
![](https://img.aspxhome.com/file/2023/9/123779_0s.jpg)
python散点图实例之随机漫步
2021-01-19 21:05:58
![](https://img.aspxhome.com/file/2023/9/109609_0s.jpg)
python判断变量是否为int、字符串、列表、元组、字典的方法详解
2022-09-28 05:11:57
如何通过配置自动实现ValueList中hql语句的整型参数转换
2024-01-24 14:59:21
Python语言实现机器学习的K-近邻算法
2023-04-25 02:09:58
![](https://img.aspxhome.com/file/2023/2/117572_0s.png)
python列表操作之extend和append的区别实例分析
2023-08-02 15:14:30
d3.js入门教程之数据绑定详解
2024-04-22 12:48:54
ORACLE实例的后台进程
2009-09-30 10:28:00
UCDChina.com 关于用户期望的讨论
2008-07-10 11:55:00
mysql 5.7.25 安装配置方法图文教程
2024-01-14 02:21:32
![](https://img.aspxhome.com/file/2023/1/128371_0s.jpg)
详解用Python为直方图绘制拟合曲线的两种方法
2021-06-15 23:58:08
![](https://img.aspxhome.com/file/2023/0/67350_0s.jpg)
sqlserver和oracle中对datetime进行条件查询的一点区别小结
2024-01-15 16:25:54
Python判断变量是否已经定义的方法
2023-08-01 07:14:01