通过 MySQL 二进制日志恢复删除的记录

栏目: 数据库 · 发布时间: 7年前

内容简介:删除 MySQL 表中的数据mysql> use sbtest;mysql> select count(*) from sbtest1;

删除 MySQL 表中的数据

mysql> use sbtest;

mysql> select count(*) from sbtest1;

+----------+

| count(*) |

+----------+

|    1000 |

+----------+

1 row in set (0.00 sec)

mysql> delete from sbtest1;

Query OK, 1000 rows affected (0.21 sec)

mysql> select count(*) from sbtest1;

+----------+

| count(*) |

+----------+

|        0 |

+----------+

1 row in set (0.00 sec)

确认时间点和当前二进制日志文件,从二进制日志中读取操作记录

mysqlbinlog \

--start-datetime="2018-09-27 15:55:00" \

--stop-datetime="2018-09-27 15:00:00" \

--base64-output=decode-rows \

--result-file=result.sql \

-v /var/lib/mysql/binlog.000022

其中的一条记录

通过 MySQL 二进制日志恢复删除的记录

去除不相关的内容

grep -A 5 "DELETE FROM \`sbtest\`.\`sbtest1\`" result.sql > 1.txt

通过 MySQL 二进制日志恢复删除的记录

提取数据

grep "=" 1.txt > 2.txt

sed -i 's/###  //g' 2.txt

通过 MySQL 二进制日志恢复删除的记录

使用以下脚本生成 INSERT 语句

[root@mysql03 tmp]# cat r2.sh

#!/bin/bash

vs=""

while read line

do

n=`echo $line | awk -NF "=" '{print $1}'`

v=`echo $line | awk -NF "=" '{print $2}'`

if [ "$n" = "@1" ]; then

vs="INSERT INTO \`sbtest\`.\`sbtest1\` VALUES("$v

elif [ "$n" = "@2" ]; then

vs=$vs" , "$v

elif [ "$n" = "@3" ]; then

vs=$vs" , "$v

elif [ "$n" = "@4" ]; then

vs=$vs" , "$v");"

echo $vs >> 3.txt

fi

done < 2.txt

[root@mysql03 tmp]# sh r2.sh

通过 MySQL 二进制日志恢复删除的记录

将数据导入到 MySQL 表中

[root@mysql03 tmp]# mysql < 3.txt

mysql> select count(*) from sbtest1;

+----------+

| count(*) |

+----------+

|    1000 |

+----------+

1 row in set (0.00 sec)


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pattern Recognition and Machine Learning

Pattern Recognition and Machine Learning

Christopher Bishop / Springer / 2007-10-1 / USD 94.95

The dramatic growth in practical applications for machine learning over the last ten years has been accompanied by many important developments in the underlying algorithms and techniques. For example,......一起来看看 《Pattern Recognition and Machine Learning》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具