標籤:mysql備份 mysql lvm
好像一般大家都不用這個吧,這裡也僅是當做記錄吧。
其實lvm快照不管是什麼儲存引擎都可以做到接近熱備,看起來也還不錯的樣子。不過卻也需要一些所必須的條件。
如:LVM卷,而且也有足夠的空閑空間來做快照,並且交易記錄也要與資料檔案在一起。
好啦,廢話不多說,我們先看看怎麼用吧。
大體步驟:
1、全域鎖。
2、滾動二進位日誌,並做好記錄
3、建立lvm快照。
4、釋放鎖。
5、從快照盤手動複製資料。
6、卸載並移除快照盤。
來吧。
先來看看lvm的情況:
[[email protected] backup]# vgs #卷組空閑4G多。我這裡夠用了。 VG #PV #LV #SN Attr VSize VFree vg0 1 5 0 wz--n- 58.59g 4.82g[[email protected] backup]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert mysql vg0 -wi-ao---- 3.00g #mysql的邏輯卷,好小啊。 root vg0 -wi-ao---- 14.65g swap vg0 -wi-ao---- 1.94g usr vg0 -wi-ao---- 19.53g var vg0 -wi-ao---- 14.65g
因為鎖定的時間越短越好,所以用兩個終端分別操作。
終端A開始在Mysql命令列下建立全域鎖。在繁忙的環境下可能要等一會兒。
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK; #刷寫緩衝區中的資訊到磁碟並建立全域鎖Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> FLUSH LOGS; #滾動二進志日誌Query OK, 0 rows affected (0.01 sec)MariaDB [(none)]> SHOW MASTER STATUS; #查看當前日誌,並做好記錄+-------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+-------------------+----------+--------------+------------------+| master-bin.000002 | 245 | | |+-------------------+----------+--------------+------------------+1 row in set (0.00 sec)
終端B趕緊建立快照。注意快照盤的大小啊,太小的話會撐爆的。
[[email protected] backup]# lvcreate -L 2G -n backup -s -p r /dev/vg0/mysql Logical volume "backup" created
建立成功,終端A解除鎖.
MariaDB [(none)]> UNLOCK TABLE;Query OK, 0 rows affected (0.00 sec)
然後就可以開始複製資料啦,cp或其它命令。
[[email protected] backup]# mount /dev/vg0/backup /mnt/ -r[[email protected] backup]# cp -a /mnt/data .[[email protected] backup]# lsdata[[email protected] backup]# cd data[[email protected] data]# lsaria_log.00000001 BA hellodb ib_logfile0 mysql mysql-bin.index nfs.star.com.err performance_schema testdb1 testdb3aria_log_control DDD ibdata1 ib_logfile1 nfs-slow.log nfs.star.com.pid test testdb2 TTT[[email protected] data]#
複製完成,也就完成了完全備份了。 交易記錄可不能刪除,裡面或許還有已提交或尚未提交的事務。
卸載並移除快照盤。
[[email protected] data]# umount /mnt[[email protected] data]# pwd/backup/data[[email protected] data]# lvremove /dev/vg0/backupDo you really want to remove active logical volume backup? [y/n]: y Logical volume "backup" successfully removed[[email protected] data]#
在我們解除全域鎖以後,資料庫中可能就已經有資料寫入了。 而剛才的備份只是之前的備份。 如果想恢複到最新的狀態,就要用二進志日誌。 剛才不是記錄了一個二進志日誌的資訊嗎,從那個日誌以後的所有都是備份之後變化。
所以我們現在再建立一些表,插入一些資料試試。來類比一下備份之後的資料修改。
MariaDB [(none)]> CREATE DATABASE ABCD;Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> CREATE TABLE ABCD.tb1 (ID int,name char(30));Query OK, 0 rows affected (0.03 sec)MariaDB [(none)]> INSERT INTO ABCD.tb1 VALUES (1,‘tom‘),(2,‘lili‘),(3,‘tonny‘);Query OK, 3 rows affected (0.01 sec)Records: 3 Duplicates: 0 Warnings: 0
我這裡建立了一個ABCD的庫,並在庫裡建立了tb1表,插入了資料。
恢複資料:
類比資料損壞,把所有資料檔案都給刪掉。並停止資料庫。
[[email protected] data]# lsABCD aria_log_control DDD ibdata1 ib_logfile1 mysql-bin.000014 nfs-slow.log nfs.star.com.pid test testdb2 TTTaria_log.00000001 BA hellodb ib_logfile0 mysql mysql-bin.index nfs.star.com.err performance_schema testdb1 testdb3[[email protected] data]# rm -rf *[[email protected] data]# ls[[email protected] data]# killall mysqld[[email protected] binlogs]# service mysqld start #資料庫已經不能啟動了。Starting MySQL... [FAILED][[email protected] binlogs]#
匯出從上次記錄開始的二進位檔案的內容。並用head查看一下,萬是檔案裡面是報錯資訊呢。
[[email protected] binlogs]# lsmaster-bin.000001 master-bin.000002 master-bin.index[[email protected] binlogs]# mysqlbinlog --start-position=245 master-bin.000002 > /backup/backup.sql[[email protected] binlogs]# head -n 30 /backup/backup.sql
恢複:
1、把所備份的所有檔案複製到資料庫目錄。
[[email protected] data]# ls #剛才啟動資料庫所產生的檔案。aria_log.00000001 aria_log_control ibdata1 ib_logfile0 ib_logfile1 nfs-slow.log nfs.star.com.err[[email protected] data]# rm -rf *[[email protected] data]# cp -a /backup/data/* . #把我們備份的資料複製過來[[email protected] data]# lsaria_log.00000001 BA hellodb ib_logfile0 mysql mysql-bin.index nfs.star.com.err performance_schema testdb1 testdb3aria_log_control DDD ibdata1 ib_logfile1 mysql-bin.000014 nfs-slow.log nfs.star.com.pid test testdb2 TTT[[email protected] data]# ll #查看許可權是否有問題。[[email protected] data]# service mysqld startStarting MySQL.. [ OK ][[email protected] data]#
啟動成功了。但沒有我們剛才建立的庫ABCD。
MariaDB [(none)]> SHOW DATABASES;+--------------------+| Database |+--------------------+| information_schema || BA || DDD || TTT || hellodb || mysql || performance_schema || test || testdb1 || testdb2 || testdb3 |+--------------------+11 rows in set (0.00 sec)MariaDB [(none)]>
2、恢複二進位日誌。
MariaDB [(none)]> SET sql_log_bin=0 -> ;Query OK, 0 rows affected (0.01 sec)MariaDB [(none)]> SHOW VARIABLES LIKE ‘sql_log_bin‘;+---------------+-------+| Variable_name | Value |+---------------+-------+| sql_log_bin | OFF |+---------------+-------+1 row in set (0.00 sec)MariaDB [(none)]> source /backup/backup.sql
3、查看資料。
MariaDB [(none)]> use ABCD;Database changedMariaDB [ABCD]> SHOW TABLES;+----------------+| Tables_in_ABCD |+----------------+| tb1 |+----------------+1 row in set (0.00 sec)MariaDB [ABCD]> SELECT * FROM tb1;+------+-------+| ID | name |+------+-------+| 1 | tom || 2 | lili || 3 | tonny |+------+-------+3 rows in set (0.00 sec)MariaDB [ABCD]>
650) this.width=650;" src="http://img.baidu.com/hi/jx2/j_0004.gif" alt="j_0004.gif" />
本文出自 “大蕃茄” 部落格,請務必保留此出處http://fanqie.blog.51cto.com/9382669/1710751
mysql之lvm快照方式備份恢複