逻辑备份Mysqldump
基于时间点“不完全”恢复(point-to-time recovery)
T0:查看数据库的logs
root@(none) 03:22:05>show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 1036642 |
| mysql-bin.000002 | 143 |
| mysql-bin.000003 | 53376 |
| mysql-bin.000004 | 120 |
+------------------+-----------+
4 rows in set (0.00 sec) 目前的log位置是000004T1:备份数据库
T2:准备需要恢复的数据
root@(none) 03:22:57>show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 1036642 |
| mysql-bin.000002 | 143 |
| mysql-bin.000003 | 53376 |
| mysql-bin.000004 | 167 |
| mysql-bin.000005 | 167 |
| mysql-bin.000006 | 167 |
| mysql-bin.000007 | 120 |
+------------------+-----------+
7 rows in set (0.00 sec)000005 000006 000007是在mysqldump备份时指定-F切换日志产生的。
root@(none) 03:23:20>use test;
Database changed
root@test 03:23:31>create table t2(id int(10));
Query OK, 0 rows affected (0.12 sec)
root@test 03:23:44>insert into t2 values(1);
Query OK, 1 row affected (0.04 sec)
root@test 03:23:57>flush logs;
Query OK, 0 rows affected (0.05 sec)以上SQL日志在000007中,再切换一次日志。
root@test 03:24:05>show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 1036642 |
| mysql-bin.000002 | 143 |
| mysql-bin.000003 | 53376 |
| mysql-bin.000004 | 167 |
| mysql-bin.000005 | 167 |
| mysql-bin.000006 | 167 |
| mysql-bin.000007 | 476 |
| mysql-bin.000008 | 120 |
+------------------+-----------+
8 rows in set (0.00 sec)root@test 03:24:10>insert into t2 values(2);
Query OK, 1 row affected (0.00 sec)
root@test 03:24:18>show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 1036642 |
| mysql-bin.000002 | 143 |
| mysql-bin.000003 | 53376 |
| mysql-bin.000004 | 167 |
| mysql-bin.000005 | 167 |
| mysql-bin.000006 | 167 |
| mysql-bin.000007 | 476 |
| mysql-bin.000008 | 328 |
+------------------+-----------+
8 rows in set (0.00 sec)
T3:破坏数据库
root@test 03:24:23>drop table t2;
Query OK, 0 rows affected (0.03 sec)这个时间点数据库遭到破坏!
T4:恢复数据库
[root@cluster4 ~]# mysql -uroot <all.db.T1.sql 这时候恢复的库里面没有t2这张表。只恢复到T1时间点。[root@cluster4 mysql_3306]# mysqlbinlog --stop-datetime='2014-8-4 15:24:23' mysql-bin.000007 |mysql -uroot第一条数据恢复[root@cluster4 mysql_3306]# mysqlbinlog --stop-datetime='2014-8-4 15:24:23' mysql-bin.000008 |mysql -uroot
第二条数据也恢复了。基于位置的“不完全”恢复
。。。省略。。。
# at 199
#140804 15:24:18 server id 1 end_log_pos 297 CRC32 0x3bf9e3b6 Query thread_id=21 exec_time=0 error_code=0
use `test`/*!*/;
SET TIMESTAMP=1407137058/*!*/;
insert into t2 values(2)
/*!*/;
# at 297
#140804 15:24:18 server id 1 <strong> end_log_pos 328</strong> CRC32 0x4ca081d0 Xid = 883
COMMIT/*!*/;
# at 328
[root@cluster4 mysql_3306]# mysqlbinlog --stop-position="328" mysql-bin.000008 |mysql -uroot
恢复完成!完全恢复
T0:查看数据库LOG
root@(none) 05:02:49>show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 1036642 |
| mysql-bin.000002 | 143 |
| mysql-bin.000003 | 53376 |
| mysql-bin.000004 | 167 |
| mysql-bin.000005 | 167 |
| mysql-bin.000006 | 167 |
| mysql-bin.000007 | 476 |
| mysql-bin.000008 | 3255287 |
+------------------+-----------+
8 rows in set (0.00 sec)
T1:备份
[root@cluster4 ~]# mysqldump -uroot --all-databases -F --all-databases>all.db.T2.sql
T2:写数据
root@test 05:09:18>select * from tfull;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
root@test 05:09:31>insert into tfull select * from tfull;
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
root@test 05:09:47>select * from tfull;
+------+
| id |
+------+
| 1 |
| 2 |
| 1 |
| 2 |
+------+
4 rows in set (0.00 sec)备份当前binlogT3:恢复
[root@cluster4 ~]# mysql -uroot <all.db.T2.sql
[root@cluster4 mysql_3306]# mysqlbinlog mysql-bin.XXXXXX |mysql -uroot恢复完成!
157



被折叠的 条评论
为什么被折叠?



