詳解:MySQL如何進行實時在線備份恢復

快照和複製技術的結合能夠保證咱們獲得一個實時的在線MySQL備份解決方案 當主庫發生誤操做時,只須要恢復備庫上的快照,而後再根據binlog執行point-in-time的恢復便可 下面假定一個場景:主從架構,沒有延遲,某DBA誤操做:drop database 接下來咱們按照以上場景進行備份恢復模擬測試.python

1.主庫準備測試數據mysql

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

到備庫確認:linux

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)

2.加個全局讀鎖 在備庫sql

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

3.爲備庫所在分區建立快照架構

[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

4.獲取二進制日誌座標oracle

在備庫:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      727 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

解鎖 在備庫:dom

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

6.掛載快照學習

[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

7.主庫某無經驗DBA誤操做測試

mysql> drop database cnfol;
Query OK, 1 row affected (0.05 sec)

記錄下此時時間:2013-10-14 10:17:103d

備庫確認是否存在庫cnfol:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.01 sec)

8.備份快照

[root@localhost backup]# pwd
/mnt/backup

[root@localhost backup]# tar -jcv -f /mnt/snapshot/mysql.tar.bz2 *

這裏作備份的緣由有2點 * 其一,昂貴的IO,由於磁頭要在快照區和系統區來回跑 * 其二,快照區空間不足,由於是COW原理

9.刪除快照

[root@localhost ~]# umount /mnt/backup
[root@localhost ~]# lvremove --force /dev/vg/backup_mysql 
  Logical volume "backup_mysql" successfully removed

10.格式化備庫所在分區

[mysql@localhost ~]$ mysqladmin -uroot -poracle 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

11.解壓縮快照到備庫所在分區

# tar -jxv -f /mnt/snapshot/mysql.tar.bz2 -C /mnt/lvm/
[root@localhost lvm]# pwd
/mnt/lvm

[root@localhost lvm]# ls
lost+found  mysql

12.啓動MySQL

13.利用binlog執行point-in-time恢復

[mysql@localhost ~]$ mysqlbinlog --stop-datetime="2013-10-14 10:17:10" /mnt/lvm/mysql/data/mysql-bin.000003 | mysql -uroot -poracle

14.確認數據

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)

福利:豆花同窗爲你們精心整理了一份關於linux和python的學習資料大合集!有須要的小夥伴們,關注豆花我的公衆號:python頭條!回覆關鍵詞「資料合集」便可免費領取!

相關文章
相關標籤/搜索