使用冷备和binlog恢复误删数据库


  1. 背景描述:
  2. 原库,用来模拟生产库。
  3. mysql安装方式:源码安装。
  4. mysql安装家目录:/home/mysql/mysql

  5. 测试库:
  6. 用来恢复数据。
  7. mysql安装方式:源码安装。
  8. mysql安装家目录:/home/mysql/testdb

  9. 由于是做测试,故原库和测试库在一台机器上。


  10. [mysql@MYSQL-SVR1 mysql]$ pwd
  11. /home/mysql/mysql

  12. --停库
  13. [mysql@MYSQL-SVR1 mysql]$ ./bin/mysql.server stop
  14. Shutting down MySQL.. [确定]

  15. --冷备
  16. [mysql@MYSQL-SVR1 mysql]$ cp -r var/ /home/mysql/backup/var-20150728.bak

  17. --重新启动数据库
  18. [mysql@MYSQL-SVR1 mysql]$ ./bin/mysql.server start
  19. Starting MySQL... [确定]

  20. --使用sysbench打上流量,目的是为了模拟生产库有数据写入
  21. ./sysbench --test=oltp --mysql-user=root --mysql-password=123 --mysql-db=test1 --oltp-table-name=t3 prepare
  22. ./sysbench --test=oltp --mysql-user=root --mysql-password=123 --mysql-db=test1 --oltp-table-name=t3 --num-threads=3 run
  23.   
  24. --手动切换binlog日志
  25. mysql> flush logs;
  26. Query OK, 0 rows affected (0.17 sec)

  27. mysql> flush logs;
  28. Query OK, 0 rows affected (0.10 sec)

  29. mysql> flush logs;
  30. Query OK, 0 rows affected (0.02 sec)

  31. --模拟删除数据库
  32. mysql> drop database sakila;
  33. Query OK, 23 rows affected (0.31 sec)

  34. --继续切换binlog日志
  35. mysql> flush logs;
  36. Query OK, 0 rows affected (0.27 sec)

  37. mysql> flush logs;
  38. Query OK, 0 rows affected (0.10 sec)

  39. mysql> flush logs;
  40. Query OK, 0 rows affected (0.16 sec)

  41. --下面开始恢复

  42. --先用全备启动测试库
  43. --将备份替换到测试库的var目录下
  44. rm -rf /home/mysql/testdb/var
  45. mv /home/mysql/backup/var-20150728.bak /home/mysql/testdb/var

  46. --启动测试库
  47. [mysql@MYSQL-SVR1 var]$ cd ..
  48. [mysql@MYSQL-SVR1 testdb]$ ./bin/mysql.server start
  49. Starting MySQL.. [确定]

  50. 恢复全量之后:
  51. mysql> show databases;
  52. +--------------------+
  53. | Database |
  54. +--------------------+
  55. | information_schema |
  56. | db1 |
  57. | employees |
  58. | mysql |
  59. | sakila |
  60. | test |
  61. | test1 |
  62. +--------------------+
  63. 7 rows in set (0.07 sec)

  64. mysql> show master status;
  65. +------------------+----------+--------------+------------------+
  66. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  67. +------------------+----------+--------------+------------------+
  68. | mysql-bin.000040 | 106 | | |
  69. +------------------+----------+--------------+------------------+
  70. 1 row in set (0.00 sec)

  71. --接下来使用binlog将数据库恢复到删除sakila库的前一刻,这就需要找到binlog的起始点和结束点
  72. --在备份中找到增量恢复的起始点,也就是备份中的最后一个position.
  73. --在原库线上binlog中找到恢复的结束点,也就是删除sakila库前的一个position.

  74. --找起始点,也就是查询备份中的最后一个binlog
  75. [mysql@MYSQL-SVR1 var]$ tail -1 mysql-bin.index
  76. ./mysql-bin.000039

  77. --查看备份中的最后一个position,这里为mysql-bin.000039的106,查看整个binlog,可发现,里面没有什么实质内容,也就是说可以从mysql-bin.000040做增量恢复
  78. [mysql@MYSQL-SVR1 var]$ mysqlbinlog mysql-bin.000039|tail
  79. BINLOG '
  80. Wxy3VQ8JVk99ZgAAAGoAAAAAAAQANS4xLjcyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  81. AAAAAAAAAAAAAAAAAABbHLdVEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC
  82. '/*!*/;
  83. # at 106
  84. #150728 14:08:49 server id 2102351369 end_log_pos 125 Stop
  85. DELIMITER ;
  86. # End of log file
  87. ROLLBACK /* added by mysqlbinlog */;
  88. /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

  89. --在原库中找结束点
  90. --线上的binlog
  91. -rw-rw---- 1 mysql mysql 603958 07-28 14:06 mysql-bin.000037
  92. -rw-rw---- 1 mysql mysql 4002437 07-28 14:07 mysql-bin.000038
  93. -rw-rw---- 1 mysql mysql 125 07-28 14:08 mysql-bin.000039
  94. -rw-rw---- 1 mysql mysql 856802 07-28 14:10 mysql-bin.000040
  95. -rw-rw---- 1 mysql mysql 707848 07-28 14:10 mysql-bin.000041
  96. -rw-rw---- 1 mysql mysql 281308 07-28 14:10 mysql-bin.000042
  97. -rw-rw---- 1 mysql mysql 1124324 07-28 14:11 mysql-bin.000043
  98. -rw-rw---- 1 mysql mysql 108424 07-28 14:11 mysql-bin.000044
  99. -rw-rw---- 1 mysql mysql 69159 07-28 14:11 mysql-bin.000045
  100. -rw-rw---- 1 mysql mysql 4119553 07-28 14:12 mysql-bin.000046
  101. -rw-rw---- 1 mysql mysql 608 07-28 14:11 mysql-bin.index

  102. --14:10 左右发现删除日志
  103. [mysql@MYSQL-SVR1 var]$ mysqlbinlog mysql-bin.000041 | grep "drop database"
  104. [mysql@MYSQL-SVR1 var]$ mysqlbinlog mysql-bin.000042 | grep "drop database"
  105. [mysql@MYSQL-SVR1 var]$ mysqlbinlog mysql-bin.000043 | grep "drop database"
  106. drop database sakila
  107.     //找到了,在mysql-bin.000043中

  108. --使用mysqlbinlog查看 mysql-bin.000043,找到删除语句前的position,这里是979589
  109. # at 979562
  110. #150728 14:11:22 server id 2102351369 end_log_pos 979589 Xid = 81689
  111. COMMIT/*!*/;
  112. # at 979589
  113. #150728 14:11:22 server id 2102351369 end_log_pos 979674 Query thread_id=4 exec_time=0 error_code=0
  114. SET TIMESTAMP=1438063882/*!*/;
  115. drop database sakila
  116. /*!*/;
  117. # at 979674
  118. #150728 14:11:22 server id 2102351369 end_log_pos 979743 Query thread_id=7 exec_time=0 error_code=0
  119. SET TIMESTAMP=1438063882/*!*/;
  120. BEGIN
  121. /*!*/;


  122. --需要应用的日志
  123. mysql-bin.000040
  124. mysql-bin.000041
  125. mysql-bin.000042
  126. mysql-bin.000043 从其实4到 979589

  127. --将binlog导入到文本文件
  128. [mysql@MYSQL-SVR1 var]$ mysqlbinlog mysql-bin.000040 > /home/mysql/backup/binlog-40.sql
  129. [mysql@MYSQL-SVR1 var]$ mysqlbinlog mysql-bin.000041 > /home/mysql/backup/binlog-41.sql
  130. [mysql@MYSQL-SVR1 var]$ mysqlbinlog mysql-bin.000042 > /home/mysql/backup/binlog-42.sql
  131. [mysql@MYSQL-SVR1 var]$ mysqlbinlog --stop-position=979589 mysql-bin.000043 > /home/mysql/backup/binlog-43.sql

  132. --在测试数据库中应用
  133. mysql> source /home/mysql/backup/binlog-40.sql
  134. mysql> source /home/mysql/backup/binlog-41.sql
  135. mysql> source /home/mysql/backup/binlog-42.sql
  136. mysql> source /home/mysql/backup/binlog-43.sql

  137. --可以看到position增大
  138. mysql> show master status;
  139. +------------------+----------+--------------+------------------+
  140. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  141. +------------------+----------+--------------+------------------+
  142. | mysql-bin.000040 | 2825100 | | |
  143. +------------------+----------+--------------+------------------+
  144. 1 row in set (0.00 sec)

  145. --在测试库中导出sakila库
  146. [mysql@MYSQL-SVR1 testdb]$ ./bin/mysqldump -uroot -p123 --default-character-set=utf8 sakila > /home/mysql/backup/sakila.20150728.sql

  147. --在原库中创建sakila库,否则无法导入表
  148. mysql> create database sakila;

  149. --在原库中导入
  150. mysql> source /home/mysql/backup/sakila.20150728.sql

  151. mysql> use sakila;
  152. mysql> show tables;
  153. +----------------------------+
  154. | Tables_in_sakila |
  155. +----------------------------+
  156. | actor |
  157. | actor_info |
  158. | address |
  159. | category |
  160. | city |
  161. | country |
  162. | customer |
  163. | customer_list |
  164. | film |
  165. | film_actor |
  166. | film_category |
  167. | film_list |
  168. | film_text |
  169. | inventory |
  170. | language |
  171. | nicer_but_slower_film_list |
  172. | payment |
  173. | rental |
  174. | sales_by_film_category |
  175. | sales_by_store |
  176. | staff |
  177. | staff_list |
  178. | store |
  179. +----------------------------+
  180. 23 rows in set (0.00 sec)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29100640/viewspace-1754476/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29100640/viewspace-1754476/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值