標籤:lvm快照卷 Database Backup
通過和mysqldump進行對比介紹LVM快照卷
Mysqldump:無法進行熱備份,資料庫較大時備份時間較長,優點就是備份恢複非常的簡單。
LVM優缺點如下:
優點:
1、幾乎熱備,只是在備份的時候為了方式產生資料不一致問題,需要鎖定資料庫。
2、支援所有儲存引擎,因為LVM備份的原理只是拷貝檔案
3、備份和恢複速度塊,因為備份和恢複的原理只是拷貝檔案。
缺點:
1、資料庫必須放在LVM邏輯卷上
2、如果是生產環境有使用者啟動了事務,那麼鎖定資料庫的過程可能需要很長的時間,而且也無法準確獲得資料庫停止時間
使用快照卷備份Mariadb
備份的前提條件:mysql的資料必須放在LVS邏輯卷之上,否則無法實現LVS備份。
如果使用LVS對單個資料庫進行備份的話,需要使用innodb儲存引擎,因為innodb儲存引擎是每表一個表檔案。
MariaDB[(none)]> SHOW GLOBAL VARIABLES LIKE ‘innodb_file_%‘;+--------------------------+----------+|Variable_name | Value |+--------------------------+----------+|innodb_file_format | Antelope ||innodb_file_format_check | ON ||innodb_file_format_max | Antelope ||innodb_file_per_table | ON |+--------------------------+----------+
備份六步走:
1、請求鎖定所有表:
主要是為了防止在備份過程中有使用者修改資料,造成備份和原資料的不一致性,需要在備份之前鎖定表,防止使用者修改,備份完成之後在解鎖。
MariaDB[(none)]> FLUSH TABLES WITH READ LOCK;
說明:如果是生產環境有使用者啟動了事務,那麼鎖定資料庫的過程可能需要很長的時間。
2、記錄二進位記錄檔及事件位置:
備份完成之後滾動日誌,然後記錄記錄檔的位置,
MariaDB [(none)]> flush logs;MariaDB [(none)]> show master status;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000002 | 245 | | |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)
3、建立快照:
建立快照卷,這裡使用的是額外啟動的終端,因為資料庫一旦退出,鎖就釋放了。
[[email protected] ~]# lvcreate -L 200M -n mydata-snap-s -p r /dev/myvg/mydata
4、釋放鎖:
MariaDB[(none)]> UNLOCK TABLES;
5、掛載快照卷,複製資料進行備份;
掛載快照卷
[[email protected] ~]# mkdir /snap[[email protected] ~]# mount /dev/myvg/mydata-snap/snap/[[email protected] ~]# ls /snap/data/aria_log.00000001 ibdata1 Mariadb.err mysql-bin.000001 performance_schemaaria_log_control ib_logfile0 Mariadb.pid mysql-bin.000002 testhellodb ib_logfile1 mysql mysql-bin.index
複製資料進行備份
只是備份單個庫使用
[[email protected] ~]# rsync -a /snap/data/hellodb//backup/hellodb-`date +%F-%H-%M-%S`[[email protected] ~]# ls /backup/hellodb-2015-05-28-02-57-12/hellodb/classes.frm coc.MYD courses.MYI scores.MYI tb1.frm toc.frmclasses.MYD coc.MYI db.opt students.frm teachers.frm toc.MYDclasses.MYI courses.frm scores.frm students.MYD teachers.MYD toc.MYIcoc.frm courses.MYD scores.MYD students.MYI teachers.MYI
備份整個庫
[[email protected] ~]# rsync -a /snap/data/*/backup/mariadb-all-`date +%F-%H-%M-%S`[[email protected] ~]# ls/backup/mariadb-all-2015-05-28-02-57-51/aria_log.00000001 ibdata1 Mariadb.err mysql-bin.000001 performance_schemaaria_log_control ib_logfile0 Mariadb.pid mysql-bin.000002 testhellodb ib_logfile1 mysql mysql-bin.index
6、備份完成之後,刪除快照卷;
[[email protected] ~]# umount /snap/[[email protected] ~]# lvremove /dev/myvg/mydata-snapDo you really want to remove active logical volumemydata-snap? [y/n]: y
類比誤刪除
備份完成之後修改了一些資料
MariaDB [hellodb]> drop table tb1;MariaDB [hellodb]> create table tb2 (id int);MariaDB [hellodb]> insert into tb2 values(1),(2),(3);MariaDB [hellodb]> select * from tb2;+------+| id |+------+| 1 || 2 || 3 |+------+
3 rows in set (0.00 sec)
修改完成之後把hellodb資料庫誤刪除了
MariaDB [hellodb]> drop database hellodb;
然後還把整個資料庫誤刪除了(說明:如果是生產環境二進位記錄檔和資料檔案一定是分開存放的,但是實驗安裝時如果是二進位記錄檔和資料目錄在一起,應該先複製一份二進位記錄檔到備份目錄)
[[email protected] ~]# rm -rf/mydata/data/*
資料庫故障恢複
Mysql出現大故障時,最好停掉資料庫,如果無法停止就殺死進程
[[email protected] ~]# service mysqld stopMySQL server PID file could not be found! [FAILED][[email protected] ~]# killall mysqldmysqld: no process killed
從二進位記錄檔中匯出所有和hellodb資料庫相關的操作。
匯出之前需要去掉誤刪除語句,查看語句在二進位記錄檔中的記錄位置
MariaDB [(none)]> show binlog events in‘mysql-bin.000002‘\G;*************************** 7. row*************************** Log_name:mysql-bin.000003 Pos:642 Event_type:Query Server_id:1End_log_pos: 729 Info:drop database hellodb7 rows in set (0.00 sec)
得到語句記錄位置之後,不匯出記錄刪除位元置
[[email protected] ~]# mysqlbinlog --stop-position=729/mydata/data/mysql-bin.000002 > /backup/lvm.sql
複製所有備份的檔案和目錄到資料目錄下
[[email protected] ~]# cp -a /backup/mariadb-all-2015-05-28-02-57-51/*/mydata/data/[[email protected] ~]# ll /mydata/data/total 28764-rw-rw---- 1 mysql mysql 16384 May 27 22:30 aria_log.00000001-rw-rw---- 1 mysql mysql 52 May 27 22:30 aria_log_controldrwx------ 2 mysql mysql 4096 May 28 00:49 hellodb-rw-rw---- 1 mysql mysql 18874368 May 28 00:49ibdata1-rw-rw---- 1 mysql mysql 5242880 May 28 00:49 ib_logfile0-rw-rw---- 1 mysql mysql 5242880 May 27 22:34 ib_logfile1-rw-r----- 1 mysql root 5674 May 28 00:43 MariaDB.err-rw-rw---- 1 mysql mysql 5May 28 00:43 MariaDB.piddrwx------ 2 mysql root 4096 May 27 22:30 mysql-rw-rw---- 1 mysql mysql 632 May 27 23:12 mysql-bin.000001-rw-rw---- 1 mysql mysql 8667 May 27 23:22 mysql-bin.000002-rw-rw---- 1 mysql mysql 114 May 28 00:50 mysql-bin.indexdrwx------ 2 mysql mysql 4096 May 27 22:30 performance_schemadrwx------ 2 mysql root 4096 May 27 22:30 test
這個時候啟動mysqld服務就可以看到備份之前的資料了
[[email protected] ~]# mysql -u root -pMariaDB [(none)]> use hellodb;MariaDB [hellodb]> show tables;+-------------------+| Tables_in_hellodb |+-------------------+| classes || coc || courses || scores || students || tb1 || teachers || toc |+-------------------+MariaDB [hellodb]> select * from tb1;+------+| id |+------+| 1 || 2 || 3 || 21 || 22 || 23 |+------+6 rows in set (0.01 sec)
恢複的資料只是備份之前的資料,備份之後更改的資料還沒有回了,這個時候就需要匯入二進位日誌轉換成的sql檔案了
[[email protected] ~]# mysql -u root -p </backup/lvm.sql
查看資料就可以看到tb2了
MariaDB [hellodb]> show tables;+-------------------+| Tables_in_hellodb |+-------------------+| classes || coc || courses || scores || students || tb2 || teachers || toc |+-------------------+MariaDB [hellodb]> select * from tb2;+------+| id |+------+| 1 || 2 || 3 |+------+
本文出自 “梅花香自苦寒來” 部落格,請務必保留此出處http://ximenfeibing.blog.51cto.com/8809812/1664768
使用LVM備份與還原Mariadb資料庫