删除 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
其中的一条记录
去除不相关的内容
grep -A 5 "DELETE FROM \`sbtest\`.\`sbtest1\`" result.sql > 1.txt
提取数据
grep "=" 1.txt > 2.txt
sed -i 's/### //g' 2.txt
使用以下脚本生成 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 表中
[root@mysql03 tmp]# mysql < 3.txt
mysql> select count(*) from sbtest1;
+----------+
| count(*) |
+----------+
| 1000 |
+----------+
1 row in set (0.00 sec)