无聊研究binlog写了个提取单个表的binlog 的信息,便于误删恢复数据,这样做的目的是为了减少恢复时其他表可能引起的出错,以及缩短恢复时间
思路就是先将第一部分以及最后一部分提取出来,我们csplite文件(以COMMIT/*!*/; 为分隔标示,不熟悉csplit命令的先去熟悉一下用法),在第一以及最后一部分中间插入单个表的binlog 信息而拼接成一个符合binlog格式的日志文件
-
1:mysql> select * from TEAMS; 原始数据
-
+--------+----------+----------+
| TEAMNO | PLAYERNO | DIVISION |
+--------+----------+----------+
| 1 | 6 | first |
| 2 | 27 | second |
| 3 | 89 | asd |
| 4 | 89 | BBBB |
| 5 | 89 | DDDD |
| 6 | 67 | GGGG |
| 7 | 77 | KKKK |
-
-
-
模拟此时做了个备份
-
mysqldump -uroot -p`cat /etc/sqlpass ` -F TENNIS>TENNIS.sql --此时binlog到了19
-
模拟做些操作
-
mysql> insert into TEAMS values(8,77,'uuuuuu');
Query OK, 1 row affected (0.00 sec)
mysql> insert into TEAMS values(9,77,'iiiiii');
Query OK, 1 row affected (0.00 sec)
-
mysql> insert into TEAMS values(10,77,'oooo');
Query OK, 1 row affected (0.01 sec)
mysql> update TEAMS set DIVISION='pppp' where TEAMNO in(9,10); ---误操作
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0
-
恢复:
mysql> flush logs;
Query OK, 0 rows affected (0.00 sec)
[root@localhost chenliang]# mysql -uroot -p`cat /etc/sqlpass ` TENNIS
[root@localhost chenliang]# sh v1.sh --binlog=mysqlbin.000019 --database=TENNIS --table=TEAMS ---提前单表的binlog 信息
点击(此处)折叠或打开
目前创新互联公司已为成百上千家的企业提供了网站建设、域名、虚拟空间、网站运营、企业网站设计、黄骅网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
-
查看提取出来的信息,删除update那部分即可
-
COMMIT/*!*/;
# at 704
#160301 13:55:35 server id 162 end_log_pos 769 CRC32 0x0593de75 Anonymous_GTID last_committed=2 sequence_number=3
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 769
#160301 13:55:35 server id 162 end_log_pos 843 CRC32 0x2978f290 Query thread_id=2 exec_time=0 error_code=0
SET TIMESTAMP=1456811735/*!*/;
BEGIN
/*!*/;
# at 843
#160301 13:55:35 server id 162 end_log_pos 897 CRC32 0xe52bc252 Table_map: `TENNIS`.`TEAMS` mapped to number 134
# at 897
#160301 13:55:35 server id 162 end_log_pos 946 CRC32 0x7331e254 Update_rows: table id 134 flags: STMT_END_F
BINLOG '
1y7VVhOiAAAANgAAAIEDAAAAAIYAAAAAAAEABlRFTk5JUwAFVEVBTVMAAwMD/gL+EgBSwivl
1y7VVh7iAAAAMQAAALIDAAAAAIYAAAAAAAEAAgAD//gKAAAATQAAAARvb29vVOIxcw==
'/*!*/;
# at 946
#160301 13:55:35 server id 162 end_log_pos 977 CRC32 0x7da650c4 Xid = 271
COMMIT/*!*/; ----从sql中将update的binlog信息删除即可
-
恢复binlog信息
[root@localhost chenliang]# mysql -uroot -p`cat /etc/sqlpass ` TENNIS
检查
mysql> select * from TEAMS;
+--------+----------+----------+
| TEAMNO | PLAYERNO | DIVISION |
+--------+----------+----------+
| 1 | 6 | first |
| 2 | 27 | second |
| 3 | 89 | asd |
| 4 | 89 | BBBB |
| 5 | 89 | DDDD |
| 6 | 67 | GGGG |
| 7 | 77 | KKKK |
| 8 | 77 | uuuuuu |
| 9 | 77 | iiiiii |
| 10 | 77 | oooo |
+--------+----------+----------+
成功