mysql8通过binlog恢复数据

参考资料: 通过binlog恢复误update的数据(一)_binlog 恢复update-CSDN博客

记录一次工作中的误删除,使用binlog恢复数据的问题。

1:找到mysql8的binlog文件。

mysql8通过binlog恢复数据_第1张图片

2:把binlog文件转换为初始sql文件

mysqlbinlog -vv --base64-output=decode-rows参数的含义

mysqlbinlog -vv --base64-output=decode-rows 
--start-datetime='2024-02-01 13:49:00' 
--stop-datetime='2024-02-01 13:50:00' binlog文件路径 > 生成的sql文件

-vv 显示备注
--base64-output=decode-rows
--start-datetime和--stop-datetime时间筛选

3.把初始文件里面的需要恢复的操作(insert,update)提取出来

## 提取出insert操作

grep -A 17 -E  "### INSERT INTO \`数据库名称\`.\`表名\`" 生成的sql文件 > 新增sql文件;


## 提取出update操作

grep -A 34 -E  "### UPDATE \`数据库名称\`.\`表名\`" 生成的sql文件 > 更新sql文件;

4.把提取出来的新增或者更新sql文件转为为可执行的sql文件

## insert 转换为可执行sql文件

sed -i 's/###//g; s/@1=//g; s/@2=//g; s/@3=//g; s/@4=//g; s/@5=//g; s/@6=//g; s/@7=//g; s/@8=//g; s/@9=//g; s/@10=//g; s/@11=//g; s/@12=//g; s/@13=//g; s/@14=//g; s/@15=//g; s/@16=//g; s/SET/VALUES(/g; s|*/|*/,|g; s/--//g; s:/\*.*\*/::g' 新增sql文件;sed -i "s/'00'/'00')/g" 新增sql文件;sed -i 's/) ,/) ;/g' 新增sql文件


## update 转为为可执行sql文件

sed -i 's/### //g; s/--//g;s:/\*.*\*/::g' 更新sql文件;
sed -i '/WHERE/{:a;N;/SET/!ba;s#WHERE#set#g}' 更新sql文件;
sed -i '/SET/{:a;N;/\n/!ba;s#SET#where#g}' 更新sql文件;
sed -i '/set/{:a;N;/where/!ba;s#@5=#STATUS=#g}' 更新sql文件;
sed -i '/where/{:a;N;/\n/!ba;s#@1=#TASKID=#g}' 更新sql文件;
sed -i '/@/'d 更新sql文件;
sed -i -r 's#(TASKID=.*)#\1;#g' 更新sql文件

你可能感兴趣的:(mysql8,binlog数据恢复)