Combined with snapshot and replication technology to ensure that we can get a real-time online MySQL backup solution, when the main library misuse, only need to restore the snapshot on the standby database, and then perform the recovery binlog according to a point-in-time.
Next, assume a scenario:
Master-slave architecture, there is no delay, a malfunction DBA: drop database
Next, we performed backup and recovery simulation testing in accordance with the above scenario
⑴ main library ready to test data
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
Library equipment to confirm:
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)
⑵ add a global read lock
In standby database:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
⑶ create a snapshot for the partition where the library equipment
[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
⑷ obtain binary log coordinates
In standby database:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 727 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
⑸ Unlock
In standby database:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
⑹ Mount Snapshot
[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
⑺ a main library inexperienced DBA misuse
mysql> drop database cnfol;
Query OK, 1 row affected (0.05 sec)
At this time, the recording time: 2013-10-14 10:17:10
Prepared by the library to confirm whether there is a library cnfol:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.01 sec)
⑻ backup snapshot
[root@localhost backup]# pwd
/mnt/backup
[root@localhost backup]# tar -jcv -f /mnt/snapshot/mysql.tar.bz2 *
There are reasons for doing backups 2:00
- First, expensive IO, because the head back and forth in the area and the system area ran snapshot
- Second, the lack of space snapshots area, because it is the principle COW
⑼ delete snapshots
[root@localhost ~]# umount /mnt/backup
[root@localhost ~]# lvremove --force /dev/vg/backup_mysql
Logical volume "backup_mysql" successfully removed
Format the partition by the library where ⑽
[mysql@localhost ~]$ mysqladmin -uroot -p shutdown
131014 10:32:40 mysqld_safe mysqld from pid file /mnt/lvm/mysql/data/localhost.localdomain.pid ended
[1]+ Done mysqld_safe
[root@localhost ~]# umount /mnt/lvm
[root@localhost ~]# mkfs -t ext3 /dev/vg/mysql
[root@localhost ~]# mount /dev/vg/mysql /mnt/lvm
[root@localhost ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
mysql vg -wi-ao 2.00G
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg 4 1 0 wz--n- 3.81G 1.81G
⑾ decompress the partition where the snapshot to the backup repository
# tar -jxv -f /mnt/snapshot/mysql.tar.bz2 -C /mnt/lvm/
[root@localhost lvm]# pwd
/mnt/lvm
[root@localhost lvm]# ls
lost+found mysql
⑿ start MySQL
⒀ use binlog perform point-in-time recovery
[mysql@localhost ~]$ mysqlbinlog --stop-datetime="2013-10-14 10:17:10" /mnt/lvm/mysql/data/mysql-bin.000003 | mysql -uroot -poracle
⒁ confirmation data
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)