MySQL实时在线备份恢复方案:Replication+LVM Snapshot【上篇】
快照和复制技术的结合可以保证我们得到一个实时的在线MySQL备份解决方案
当主库发生误操作时,只需要恢复备库上的快照,然后再根据binlog执行point-in-time的恢复即可
下面假定一个场景:
主从架构,没有延迟,某DBA误操作:drop database
接下来我们按照以上场景进行备份恢复模拟测试
⑴ 主库准备测试数据
mysql> create database cnfol;
Query OK, 1 row affected (0.00 sec)
mysql> create table cnfol.t (id int primary key);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into cnfol.t select 1;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into cnfol.t select 2;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
到备库确认:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cnfol |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> select * from cnfol.t;
+----+
| id |
+----+
| 1 |
| 2 |
+----+
2 rows in set (0.00 sec)
⑵ 加个全局读锁
在备库:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
⑶ 为备库所在分区创建快照
[root@localhost ~]# lvcreate --size 1G --snapshot --name backup_mysql /dev/vg/mysql
Logical volume "backup_mysql" created
[root@localhost ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
backup_mysql vg swi-a- 1.00G mysql 0.00
mysql vg owi-ao 2.00G
⑷ 获取二进制日志坐标
在备库:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 727 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
⑸ 解锁
在备库:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
⑹ 挂载快照
[root@localhost ~]# mount /dev/vg/backup_mysql /mnt/backup
[root@localhost ~]# cd /mnt/backup/mysql/data/cnfol/ && ls -alh
总计 32K
drwx------ 2 mysql dba 4.0K 10-14 09:57 .
drwx------ 5 mysql dba 4.0K 10-14 09:57 ..
-rw-rw---- 1 mysql dba 61 10-14 09:57 db.opt
-rw-rw---- 1 mysql dba 8.4K 10-14 09:57 t.frm
-rw-rw---- 1 mysql dba 14 10-14 09:57 t.MYD
-rw-rw---- 1 mysql dba 2.0K 10-14 10:06 t.MYI
⑺ 主库某无经验DBA误操作
mysql> drop database cnfol;
Query OK, 1 row affected (0.05 sec)
记录下此时时间:2013-10-14 10:17:10
备库确认是否存在库cnfol:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test &nbs