通過和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、創建快照:
創建快照卷,這裏使用的是額外啓動的終端,因爲數據庫一旦退出,鎖就釋放了。
[root@MariaDB ~]# lvcreate -L 200M -n mydata-snap-s -p r /dev/myvg/mydata
4、釋放鎖:
MariaDB[(none)]> UNLOCK TABLES;
5、掛載快照卷,複製數據進行備份;
掛載快照卷
[root@MariaDB ~]# mkdir /snap [root@MariaDB ~]# mount /dev/myvg/mydata-snap/snap/ [root@Mariadb ~]# ls /snap/data/ aria_log.00000001 ibdata1 Mariadb.err mysql-bin.000001 performance_schema aria_log_control ib_logfile0 Mariadb.pid mysql-bin.000002 test hellodb ib_logfile1 mysql mysql-bin.index
複製數據進行備份
只是備份單個庫使用
[root@MariaDB ~]# rsync -a /snap/data/hellodb//backup/hellodb-`date +%F-%H-%M-%S` [root@Mariadb ~]# ls /backup/hellodb-2015-05-28-02-57-12/hellodb/ classes.frm coc.MYD courses.MYI scores.MYI tb1.frm toc.frm classes.MYD coc.MYI db.opt students.frm teachers.frm toc.MYD classes.MYI courses.frm scores.frm students.MYD teachers.MYD toc.MYI coc.frm courses.MYD scores.MYD students.MYI teachers.MYI
備份整個庫
[root@MariaDB ~]# rsync -a /snap/data/*/backup/mariadb-all-`date +%F-%H-%M-%S` [root@Mariadb ~]# ls/backup/mariadb-all-2015-05-28-02-57-51/ aria_log.00000001 ibdata1 Mariadb.err mysql-bin.000001 performance_schema aria_log_control ib_logfile0 Mariadb.pid mysql-bin.000002 test hellodb ib_logfile1 mysql mysql-bin.index
6、備份完成之後,刪除快照卷;
[root@Mariadb ~]# umount /snap/ [root@Mariadb ~]# lvremove /dev/myvg/mydata-snap Do 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;
然後還把整個數據庫誤刪除了(說明:如果是生產環境二進制日誌文件和數據文件一定是分開存放的,但是實驗安裝時如果是二進制日誌文件和數據目錄在一起,應該先複製一份二進制日誌文件到備份目錄)
[root@MariaDB ~]# rm -rf/mydata/data/*
數據庫故障恢復
Mysql出現大故障時,最好停掉數據庫,如果無法停止就殺死進程
[root@MariaDB ~]# service mysqld stop MySQL server PID file could not be found! [FAILED] [root@MariaDB ~]# killall mysqld mysqld: 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:1 End_log_pos: 729 Info:drop database hellodb 7 rows in set (0.00 sec)
得到語句記錄位置之後,不導出記錄刪除位置
[root@Mariadb ~]# mysqlbinlog --stop-position=729/mydata/data/mysql-bin.000002 > /backup/lvm.sql
複製所有備份的文件和目錄到數據目錄下
[root@MariaDB ~]# cp -a /backup/mariadb-all-2015-05-28-02-57-51/*/mydata/data/ [root@MariaDB ~]# 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_control drwx------ 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.pid drwx------ 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.index drwx------ 2 mysql mysql 4096 May 27 22:30 performance_schema drwx------ 2 mysql root 4096 May 27 22:30 test
這個時候啓動mysqld服務就可以看到備份之前的數據了
[root@MariaDB ~]# mysql -u root -p MariaDB [(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文件了
[root@MariaDB ~]# 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 | +------+