利用binlog来恢复数据库

http://bbs.kaspersky.com.cn/thread-287149-1-1.html

开发库和线上表结构有很多不一样,分表个数也不同,我需要重新整理保持开发库和线上一致,之前已经告知开发数据是否允许丢失,开发说数据可以不要了,直接丢掉,等我搞好之后开发告诉我部分数据(ring)是不能删除的,我晕,只好想办法来恢复数据了,没有备份,不过还好有全部的binlog。

1.根据binlog解析出所有ring数据库的所有sql
[mysql@DEV_SNS1 log]$mysqlbinlog --no-defaults --database=ring --start-datetime="2005-04-20 9:55:00" --stop-datetim="2009-04-08 08:05:00" /u01/mysql/log/mysql-bin.000005 > /u01/mysql/log/mysql_restore5.sql

[mysql@DEV_SNS1 log]$ ls -l /u01/mysql/log/mysql_restore5.sql
-rw-r--r--    1 mysql    dba           407 Apr  8 15:33 /u01/mysql/log/mysql_restore5.sql
--start-datetime=datetime 从哪个点开始解析
--stop-datetim=datetime   从哪个点停止解析
--database=ring               指定需要解析哪个数据库,只提取某个数据库的sql语句
--如果有多个binlog的话需要解析多次,需要解析所有的binlog
2.重新在数据库中执行
[mysql@DEV_SNS1 log]$mysql -u root ERROR 1062 (23000) at line 2559580: Duplicate entry '175754263-140' for key 1 --恢复过程中出现主键冲突,导入失败
3.删除已经恢复的数据,重新恢复一遍
[mysql@DEV_SNS1 log]$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 7647
Server version: 5.0.67-log Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the buffer.
root@(none) 02:55:41>drop database ring;
Query OK, 8 rows affected (0.04 sec)
[mysql@DEV_SNS1 log]$ mysql -u root -f ERROR 1062 (23000) at line 2559580: Duplicate entry '175754263-140' for key 1
ERROR 1062 (23000) at line 2564671: Duplicate entry '138' for key 1
ERROR 1062 (23000) at line 2566216: Duplicate entry '139' for key 1
ERROR 1062 (23000) at line 2566224: Duplicate entry '140' for key 1
ERROR 1062 (23000) at line 2566232: Duplicate entry '141' for key 1
ERROR 1062 (23000) at line 2566240: Duplicate entry '142' for key 1
ERROR 1062 (23000) at line 2648410: Duplicate entry '143' for key 1
ERROR 1062 (23000) at line 2648418: Duplicate entry '144' for key 1
ERROR 1062 (23000) at line 2648581: Duplicate entry '145' for key 1
ERROR 1062 (23000) at line 2648589: Duplicate entry '146' for key 1
ERROR 1062 (23000) at line 2648597: Duplicate entry '147' for key 1
ERROR 1062 (23000) at line 2648605: Duplicate entry '148' for key 1
ERROR 1062 (23000) at line 2649279: Duplicate entry '149' for key 1
ERROR 1062 (23000) at line 2649287: Duplicate entry '150' for key 1
ERROR 1062 (23000) at line 2649295: Duplicate entry '151' for key 1
ERROR 1062 (23000) at line 2649303: Duplicate entry '152' for key 1
ERROR 1062 (23000) at line 2649311: Duplicate entry '153' for key 1
ERROR 1062 (23000) at line 2649319: Duplicate entry '154' for key 1
ERROR 1062 (23000) at line 2649327: Duplicate entry '155' for key 1
ERROR 1062 (23000) at line 2649335: Duplicate entry '156' for key 1
 
恢复成功,有部分数据冲突需要开发自己去处理了。
总的来说,不管是否重要,因为重要性很多时候我们自己是无法评定的,大操作之前一定先做好备份,备份才是王道。

你可能感兴趣的:(利用binlog来恢复数据库)