備份與恢復
數據備份就是將數據以某種方式加以保留,以便在系統需要時重新恢復和利用。其作用主要體現在如下兩個二方面:
在數據遭到意外事件破壞時,通過數據恢復還原數據
數據備份是歷史數據保存歸檔的最佳方式
數據恢復就是把遭到破壞、刪除和修改的數據還原為可使用的數據的過程
為什么要備份數據?
在生產環境中我們數據庫可能會遭遇各種各樣的不測從而導致數據丟失, 大概分為以下幾種.
硬件故障
軟件故障
自然災害
黑客攻擊
誤操作
所以, 為了在數據丟失之后能夠恢復數據, 我們就需要定期的備份數據, 備份數據的策略要根據不同的應用場景進行定制, 大致有幾個參考數值, 我們可以根據這些數值從而定制符合特定環境中的數據備份策略
能夠容忍丟失多少數據
恢復數據需要多長時間
需要恢復哪一些數據
數據備份類型
分為以下兩種:
完全備份
部分備份
完全備份是指將整個數據集即整個數據庫備份
而部分備份又分以下兩種:
增量備份
差異備份
增量備份指的是備份自上一次備份以來(增量或完全)以來變化的數據; 特點: 節約空間、還原麻煩
差異備份指的是備份自上一次完全備份以來變化的數據 特點: 浪費空間、還原比增量備份簡單
MySQL的備份方式
根據數據服務是否在線:
熱備:讀寫操作均可進行的狀態下所做的備份;
溫備:可讀但不可寫狀態下進行的備份;
冷備:讀寫操作均不可進行的狀態下所做的備份;
不同方式的備份還要考慮所使用的存儲引擎
MyISAM:只支持溫備、不能熱備
InnoDB:支持熱備
備份工具
mysqldump
: 邏輯備份工具, 適用于所有的存儲引擎, 支持溫備、完全備份、部分備份、對于InnoDB存儲引擎支持熱備cp, tar
等歸檔復制工具: 物理備份工具, 適用于所有的存儲引擎, 冷備、完全備份、部分備份lvm2 snapshot
: 幾乎熱備, 借助文件系統管理工具進行備份xtrabackup
: 一款非常強大的InnoDB/XtraDB熱備工具, 支持完全備份、增量備份, 由percona提供
備份恢復演練
使用cp備份
這里我們使用MariaDB 5.5版本進行演練
查看數據庫中的數據
MariaDB [(none)]> SHOW DATABASES; #查看數據庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES; #查看數據庫中的表信息
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
MariaDB [hellodb]>
向數據庫施加讀鎖
MariaDB [hellodb]> FLUSH TABLE WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
MariaDB [hellodb]>
備份數據文件
[root@admin1 ~]# mkdir /backup #建立備份目錄
[root@admin1 ~]# cp -a /var/lib/mysql/* /backup/ #備份,保留原屬性信息
[root@admin1 ~]# ls /backup/ #查看備份文件
aria_log.00000001 hellodb ib_logfile0 master-bin.000001 master-bin.000003 mysql performance_schema
aria_log_control ibdata1 ib_logfile1 master-bin.000002 master-bin.index mysql.sock
[root@admin1 ~]#
模擬數據丟失并恢復
[root@admin1 ~]# rm -rf /var/lib/mysql/* #模擬數據損壞
[root@admin1 ~]# systemctl restart mariadb.service #重啟服務,將初始化數據庫
MariaDB [(none)]> SHOW DATABASES; #查看數據庫,數據已丟失
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
#恢復數據
[root@admin1 ~]# rm -rf /var/lib/mysql/* #刪除現有數據目錄
[root@admin1 ~]# cp -a /backup/* /var/lib/mysql/ #還原數據
[root@admin1 ~]# systemctl restart mariadb.service #重啟服務
#驗證
MariaDB [(none)]> SHOW DATABASES; #數據已恢復
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
MariaDB [hellodb]>
mysqldummp+復制binlog
mysqldump工具介紹
是一個客戶端的邏輯備份工具, 可以生成一個重現創建原始數據庫和表的SQL語句, 可以支持所有的存儲引擎, 對于InnoDB支持熱備
#基本使用語法
mysqldump [OPTIONS] db_name [tables] #只備份指定數據庫當中的表,恢復需要手動CREATE DATABASE
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] #恢復不需要手動創建數據庫
mysqldump [OPTIONS] --all-databases [OPTIONS] #備份所有數據庫
MyISAM存儲引擎:支持溫備,備份時要鎖定表;
-x, --lock-all-tables:鎖定所有庫的所有表,讀鎖;
-l, --lock-tables:鎖定指定庫所有表;
InnoDB存儲引擎:支持溫備和熱備;
--single-transaction:創建一個事務,基于此快照執行備份;
其它選項:
-A, --all-databases:備份所有庫
-B, --databases DB1 DB2 ...:備份指定的庫
-E, --events:備份指定數據庫相關的所有event scheduler
-R, --routines:備份指定數據庫相關的所有存儲過程和存儲函數
--triggers:備份表相關的觸發器
--master-data[=#]:記錄備份時二進制日志文件所處的position
1:記錄為CHANGE MASTER TO語句,此語句不被注釋;
2:記錄為CHANGE MASTER TO語句,此語句被注釋;
--flush-logs:鎖定表完成后,即進行日志刷新操作;
查看數據庫信息
MariaDB [hellodb]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [hellodb]>
MariaDB [hellodb]> USE hellodb;
Database changed
MariaDB [hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
MariaDB [hellodb]>
mysqldump備份并恢復數據
[root@admin1 ~]# mysql -uroot -pmageedu -e 'SHOW MASTER STATUS;' #查看當前二進制文件的狀態, 并記錄下position的數字
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000004 | 245 | | |
+-------------------+----------+--------------+------------------+
[root@admin1 ~]# mysqldump -uroot -pmageedu --all-databases --lock-all-tables > all.sql #備份數據庫到all.sql文件中
MariaDB [(none)]> CREATE DATABASE test1; #創建一個數據庫
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> USE test1;
Database changed
MariaDB [test1]> CREATE TABLE IF NOT EXISTS students(id SMALLINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(10)); #創建表
Query OK, 0 rows affected (0.03 sec)
MariaDB [test1]> INSERT INTO students VALUES (1,'Tom'),(2,'Jerry'); #插入數據
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [test1]> SELECT * FROM students; #查看數據
+----+-------+
| id | name |
+----+-------+
| 1 | Tom |
| 2 | Jerry |
+----+-------+
2 rows in set (0.00 sec)
MariaDB [test1]> SHOW MASTER STATUS; #記錄現在的position
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000004 | 785 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
[root@admin1 ~]# cp /var/lib/mysql/master-bin.000004 /backup/binlog/ #備份二進制日志
[root@admin1 ~]# systemctl stop mariadb.service #停止服務
[root@admin1 ~]# rm -rf /var/lib/mysql/* #刪除所有數據文件
[root@admin1 ~]# systemctl start mariadb.service #啟動MySQL,重新初始化數據庫
MariaDB [(none)]> SHOW DATABASES; #查看數據庫,數據已丟失
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> SET sql_log_bin=OFF; #臨時關閉二進制日志
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> source /root/all.sql #恢復數據
MariaDB [mysql]> SET sql_log_bin=ON; #開啟二進制日志
Query OK, 0 rows affected (0.00 sec)
[root@admin1 ~]# mysqlbinlog --start-position=245 --stop-position=785 /backup/binlog/master-bin.000004 | mysql #通過二進制日志增量恢復
MariaDB [(none)]> SHOW DATABASES; #test1數據庫已經有了
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
| test1 |
+--------------------+
6 rows in set (0.00 sec)
MariaDB [(none)]> USE test1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test1]> SHOW TABLES; #查看表
+-----------------+
| Tables_in_test1 |
+-----------------+
| students |
+-----------------+
1 row in set (0.00 sec)
MariaDB [test1]> SELECT * FROM students; #數據已經恢復
+----+-------+
| id | name |
+----+-------+
| 1 | Tom |
| 2 | Jerry |
+----+-------+
2 rows in set (0.00 sec)
lvm2快照備份數據
LVM
快照簡單來說就是將所快照源分區一個時間點所有文件的元數據進行保存,如果源文件沒有改變,那么訪問快照卷的相應文件則直接指向源分區的源文件,如果源文件發生改變,則快照卷中與之對應的文件不會發生改變??煺站碇饕糜谳o助備份文件
創建快照卷
添加硬盤/dev/sdb,使用命令直接手動上識別新添加的硬盤,無需重啟
[root@admin1 ~]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@admin1 ~]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@admin1 ~]# echo "- - -" > /sys/class/scsi_host/host2/scan
[root@admin1 ~]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb
#分區
[root@admin1 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x432cc07d.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@admin1 ~]# partx -a /dev/sdb
partx: /dev/sdb: error adding partition 1
#創建邏輯卷
[root@admin1 ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
[root@admin1 ~]# vgcreate myvg /dev/sdb1
Volume group "myvg" successfully created
[root@admin1 ~]# lvcreate -n mydata -L 5G myvg
Logical volume "mydata" created.
[root@admin1 ~]# mkfs.ext4 /dev/mapper/myvg-mydata #格式化
[root@admin1 ~]# mkdir /lvm_data #創建數據目錄
[root@admin1 ~]# mount /dev/mapper/myvg-mydata /lvm_data/ #掛載
[root@admin1 ~]# vim /etc/my.cnf #編輯配置文件,修改數據目錄
datadir = /lvm_data
[root@admin1 ~]# chown -R mysql.mysql /lvm_data/ #修改數據目錄屬主屬組
[root@admin1 ~]# systemctl restart mariadb #重啟服務
#重新導入hellodb.sql數據庫文件
[root@admin1 ~]# mysql < hellodb.sql
查看數據庫信息
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
創建快照卷并備份
MariaDB [hellodb]> FLUSH TABLE WITH READ LOCK; #鎖定所有表
Query OK, 0 rows affected (0.00 sec)
[root@admin1 ~]# lvcreate -L 2G -n mydata-snap -p r -s /dev/mapper/myvg-mydata #創建快照卷
Logical volume "mydata-snap" created.
MariaDB [(none)]> UNLOCK TABLES; #解鎖
Query OK, 0 rows affected (0.00 sec)
[root@admin1 ~]# mkdir /lvm_snap #創建文件夾
[root@admin1 ~]# mount /dev/mapper/myvg-mydata--snap /lvm_snap/ #掛載快照卷
mount: /dev/mapper/myvg-mydata--snap is write-protected, mounting read-only
[root@admin1 ~]# cd /lvm_snap/
[root@admin1 lvm_snap]# ls #查看數據文件
aria_log.00000001 hellodb ib_logfile0 master-bin.000001 master-bin.000003 mysql test
aria_log_control ibdata1 ib_logfile1 master-bin.000002 master-bin.index performance_schema
[root@admin1 lvm_snap]# tar cf /backup/mysqldata.tar * #打包歸檔
[root@admin1 ~]# umount /lvm_snap/ #卸載快照卷
[root@admin1 ~]# lvremove myvg mydata-snap #刪除快照卷
Do you really want to remove active logical volume mydata-snap? [y/n]: y
Logical volume "mydata-snap" successfully removed
Logical volume myvg/mydata contains a filesystem in use.
Volume group "mydata-snap" not found
Cannot process volume group mydata-snap
恢復數據
[root@admin1 ~]# rm -rf /lvm_data/* #模擬數據損壞
[root@admin1 ~]# systemctl start mariadb.service #重啟服務
MariaDB [(none)]> SHOW DATABASES; #數據丟失
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
[root@admin1 ~]# cd /lvm_data/
[root@admin1 lvm_data]# rm -rf * #刪除所有數據文件
[root@admin1 lvm_data]# tar xf /backup/mysqldata.tar #將備份的數據解壓至數據目錄
[root@admin1 lvm_data]# ls #查看數據文件
aria_log.00000001 hellodb ib_logfile0 master-bin.000001 master-bin.000003 mysql test
aria_log_control ibdata1 ib_logfile1 master-bin.000002 master-bin.index performance_schema
[root@admin1 lvm_data]#
MariaDB [(none)]> SHOW DATABASES; #數據已恢復
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES; #數據已恢復
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
使用Xtrabackup備份
xtrabakcup
介紹
Xtrabackup是由percona提供的mysql數據庫備份工具,據官方介紹,這也是世界上惟一一款開源的能夠對innodb和xtradb數據庫進行熱備的工具。特點:
物理備份
備份速度快,可靠
備份完成后自動校驗
還原速度快
能夠基于壓縮等功能節約磁盤空間和流量
xtrabackup
實現完全備份
我們使用
xtrabackup
的前端工具innobackupex
來實現對數據完全備份使用innobackupex備份時, 會調用xtrabackup備份所有的
InnoDB
表, 復制所有關于表結構定義的相關文件(.frm)、以及MyISAM、MERGE、CSV
和ARCHIVE
表的相關文件, 同時還會備份觸發器和數據庫配置文件信息相關的文件, 這些文件會被保存至一個以時間命名的目錄
備份
#查看數據文件
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.01 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
#備份數據
[root@admin1 ~]# mkdir /backups #建立備份目錄
[root@admin1 ~]# innobackupex --user=root /backups/ #備份
####提示completed OK!表示成功####
[root@admin1 ~]# ls /backups/ #查看備份文件
2016-11-24_09-58-25
一般情況, 備份完成后, 數據不能用于恢復操作, 因為備份的數據中可能會包含尚未提交的事務或已經提交但尚未同步至數據文件中的事務。因此, 此時的數據文件仍不一致, 所以我們需要”準備”一個完全備份
[root@admin1 ~]# innobackupex --apply-log /backups/2016-11-24_09-58-25/
####提示completed OK!表示成功####
[root@admin1 ~]# ls /backups/2016-11-24_09-58-25/ #查看備份數據文件
backup-my.cnf ibdata1 ib_logfile1 performance_schema xtrabackup_binlog_info xtrabackup_checkpoints xtrabackup_logfile
hellodb ib_logfile0 mysql test xtrabackup_binlog_pos_innodb xtrabackup_info
[root@admin1 ~]#
恢復數據
[root@admin1 mysql]# systemctl stop mariadb.service #停止MySQL服務
[root@admin1 mysql]# rm -rf /var/lib/mysql/* #刪除數據文件
[root@admin1 mysql]# innobackupex --copy-back /backups/2016-11-24_09-58-25/ #還原
####提示completed OK!表示成功####
[root@admin1 mysql]# chown -R mysql.mysql /var/lib/mysql/* #修改數據目錄文件屬組屬主
[root@admin1 mysql]# vim /etc/my.cnf #編輯配置文件添加此項
innodb_log_file_size = 50331648 #值設定為事務日志文件的大小
[root@admin1 mysql]# systemctl start mariadb #啟動MySQL
MariaDB [(none)]> SHOW DATABASES; #數據已恢復
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.01 sec)
完全+增量+binglog
查看數據庫信息
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.01 sec)
完全備份
[root@admin1 ~]# mkdir /backups #創建備份目錄
[root@admin1 ~]# innobackupex --user=root --password= /backups/ #完全備份
####提示completed OK!表示備份成功####
#備份完成之后,我們再新創建一個數據庫,而后做增量備份
MariaDB [(none)]> CREATE DATABASE zhai; #創建新的數據庫
Query OK, 1 row affected (0.00 sec)
MariaDB [hellodb]> USE zhai;
Database changed
MariaDB [zhai]> CREATE TABLE IF NOT EXISTS students(id SMALLINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(10)); #創建表
Query OK, 0 rows affected (0.03 sec)
MariaDB [zhai]> INSERT INTO students VALUES (1,'xiaozhai'),(2,'xiaoxiao'); #插入數據
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [zhai]> SELECT * FROM students;
+----+----------+
| id | name |
+----+----------+
| 1 | xiaozhai |
| 2 | xiaoxiao |
+----+----------+
2 rows in set (0.01 sec)
MariaDB [zhai]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
| zhai |
+--------------------+
6 rows in set (0.01 sec)
增量備份
[root@admin1 ~]# cd /backups/ #切換至備份目錄
[root@admin1 backups]# ls #查看之前的完全備份
2016-11-25_00-32-52
[root@admin1 backups]# innobackupex --incremental /backups/ --incremental-basedir=/backups/2016-11-25_00-32-52 #增量備份
[root@admin1 backups]# ls
2016-11-25_00-32-52 2016-11-25_00-42-48
#增量備份后,我們再次創建一個數據庫,但是還沒來得及備份MySQL服務已經宕機,這時就需要借助于二進制日志文件來做恢復
MariaDB [(none)]> CREATE DATABASE laozhai; #創建新的數據
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| laozhai |
| mysql |
| performance_schema |
| test |
| zhai |
+--------------------+
7 rows in set (0.00 sec)
#我們需要將上一次增量備份所記錄的二進制日志position提取出來
[root@admin1 backups]# cat 2016-11-25_00-42-48/xtrabackup_binlog_info #查看二進制日志position
master-bin.000003 8304
[root@admin1 backups]# mysqlbinlog --start-position=8304 /var/lib/mysql/master-bin.000003 > /backups/binlog.sql #截取二進制日志
BASEDIR指的是完全備份所在的目錄,此命令執行結束后,innobackupex命令會在/extrabackup目錄中創建一個新的以時間命名的目錄以存放所有的增量備份數據。另外,在執行過增量備份之后再一次進行增量備份時,其–incremental-basedir應該指向上一次的增量備份所在的目錄。
整理增量備份
[root@admin1 backups]# innobackupex --apply-log --redo-only /backups/2016-11-25_00-32-52
[root@admin1 backups]# innobackupex --apply-log --redo-only /backups/2016-11-25_00-32-52 --incremental-dir=/backups/2016-11-25_00-42-48
恢復數據
[root@admin1 backups]# systemctl stop mariadb #停止MySQL服務
[root@admin1 backups]# rm -rf /var/lib/mysql/* #刪除數據目錄
[root@admin1 backups]# innobackupex --copy-back /backups/2016-11-25_00-32-52 #恢復數據
[root@admin1 mysql]# chown -R mysql.mysql /var/lib/mysql/* #修改屬主屬組
[root@admin1 mysql]# systemctl start mariadb #啟動MySQL
MariaDB [(none)]> SHOW DATABASES; #此時發現hellodb已經恢復,但是我們新創建的數據庫laozhai還沒有恢復,我們只需對我們之前所備份的二進制日志文件做重放就可以了
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
| zhai |
+--------------------+
6 rows in set (0.01 sec)
[root@admin1 mysql]# cat /backups/binlog.sql | mysql #重放二進制日志
MariaDB [(none)]> SHOW DATABASES; #數據恢復
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| laozhai |
| mysql |
| performance_schema |
| test |
| zhai |
+--------------------+
7 rows in set (0.00 sec)
#完成
原創文章,作者:zhai796898,如若轉載,請注明出處:http://www.www58058.com/60654