Xtrabackup備份實(shí)戰(zhàn)
一、Xtrabackup介紹
MySQL冷備、mysqldump、MySQL熱拷貝都無(wú)法實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)進(jìn)行增量備份。在實(shí)際生產(chǎn)環(huán)境中增量備份是非常實(shí)用的,如果數(shù)據(jù)大于50G或100G,存儲(chǔ)空間足夠的情況下,可以每天進(jìn)行完整備份,如果每天產(chǎn)生的數(shù)據(jù)量較大,需要定制數(shù)據(jù)備份策略。例如每周實(shí)用完整備份,周一到周六實(shí)用增量備份。而Percona-Xtrabackup就是為了實(shí)現(xiàn)增量備份而出現(xiàn)的一款主流備份工具,xtrabakackup有2個(gè)工具,分別是xtrabakup、innobakupe。
Percona-xtrabackup是 Percona公司開發(fā)的一個(gè)用于MySQL數(shù)據(jù)庫(kù)物理熱備的備份工具,支持MySQL、Percona server和MariaDB,開源免費(fèi),是目前較為受歡迎的主流備份工具。xtrabackup只能備份innoDB和xtraDB兩種數(shù)據(jù)引擎的表,而不能備份MyISAM數(shù)據(jù)表。
二、Xtrabackup優(yōu)點(diǎn)
(1)備份速度快,物理備份可靠
(2)備份過(guò)程不會(huì)打斷正在執(zhí)行的事務(wù)(無(wú)需鎖表)
(3)能夠基于壓縮等功能節(jié)約磁盤空間和流量
(4)自動(dòng)備份校驗(yàn)
(5)還原速度快
(6)可以流傳將備份傳輸?shù)搅硗庖慌_(tái)機(jī)器上
(7)在不增加服務(wù)器負(fù)載的情況備份數(shù)據(jù)
三、Xtrabackup備份原理
Xtrabackup備份流程圖:

(1)innobackupex啟動(dòng)后,會(huì)先f(wàn)ork一個(gè)進(jìn)程,用于啟動(dòng)xtrabackup,然后等待xtrabackup備份ibd數(shù)據(jù)文件;
(2)xtrabackup在備份innoDB數(shù)據(jù)是,有2種線程:redo拷貝線程和ibd數(shù)據(jù)拷貝線程。xtrabackup進(jìn)程開始執(zhí)行后,會(huì)啟動(dòng)一個(gè)redo拷貝的線程,用于從最新的checkpoint點(diǎn)開始順序拷貝redo.log;再啟動(dòng)ibd數(shù)據(jù)拷貝線程,進(jìn)行拷貝ibd數(shù)據(jù)。這里是先啟動(dòng)redo拷貝線程的。在此階段,innobackupex進(jìn)行處于等待狀態(tài)(等待文件被創(chuàng)建)
(4)xtrabackup拷貝完成ibd數(shù)據(jù)文件后,會(huì)通知innobackupex(通過(guò)創(chuàng)建文件),同時(shí)xtrabackup進(jìn)入等待狀態(tài)(redo線程依舊在拷貝redo.log)
(5)innobackupex收到xtrabackup通知后哦,執(zhí)行FLUSH TABLES WITH READ LOCK(FTWRL),取得一致性位點(diǎn),然后開始備份非InnoDB文件(如frm、MYD、MYI、CSV、opt、par等格式的文件),在拷貝非InnoDB文件的過(guò)程當(dāng)中,數(shù)據(jù)庫(kù)處于全局只讀狀態(tài)。
(6)當(dāng)innobackup拷貝完所有的非InnoDB文件后,會(huì)通知xtrabackup,通知完成后,進(jìn)入等待狀態(tài);
(7)xtrabackup收到innobackupex備份完成的通知后,會(huì)停止redo拷貝線程,然后通知innobackupex,redo.log文件拷貝完成;
(8)innobackupex收到redo.log備份完成后,就進(jìn)行解鎖操作,執(zhí)行:UNLOCK TABLES;
(9)最后innbackupex和xtrabackup進(jìn)程各自釋放資源,寫備份元數(shù)據(jù)信息等,innobackupex等xtrabackup子進(jìn)程結(jié)束后退出。
四、xtrabackup的安裝部署以及備份恢復(fù)實(shí)現(xiàn)
1、xtrabackup的安裝
下載地址:https://www.percona.com/downloads/XtraBackup/LATEST/
可以選擇rpm包方式安裝,也可以下載源碼包編譯安裝,這里直接采用rpm包的方式進(jìn)行安裝
[root@master tools]# wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.9/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.9-1.el7.x86_64.rpm
[root@master tools]# yum install -y percona-xtrabackup-24-2.4.9-1.el7.x86_64.rpm
[root@master ~]# rpm -qa |grep xtrabackup
percona-xtrabackup-24-2.4.9-1.el7.x86_64
Xtrabackup中主要包含兩個(gè)工具:
xtrabackup:是用于熱備innodb,xtradb表中數(shù)據(jù)的工具,不能備份其他類型的表,也不能備份數(shù)據(jù)表結(jié)構(gòu);
innobackupex:是將xtrabackup進(jìn)行封裝的perl腳本,提供了備份myisam表的能力。
常用選項(xiàng):
--host 指定主機(jī)
--user 指定用戶名
--password 指定密碼
--port 指定端口
--databases 指定數(shù)據(jù)庫(kù)
--incremental 創(chuàng)建增量備份
--incremental-basedir 指定包含完全備份的目錄
--incremental-dir 指定包含增量備份的目錄
--apply-log 對(duì)備份進(jìn)行預(yù)處理操作
一般情況下,在備份完成后,數(shù)據(jù)尚且不能用于恢復(fù)操作,因?yàn)閭浞莸臄?shù)據(jù)中可能會(huì)包含尚未提交的事務(wù)或已經(jīng)提交但尚未同步至數(shù)據(jù)文件中的事務(wù)。因此,此時(shí)數(shù)據(jù)文件仍處理不一致狀態(tài)。“準(zhǔn)備”的主要作用正是通過(guò)回滾未提交的事務(wù)及同步已經(jīng)提交的事務(wù)至數(shù)據(jù)文件也使得數(shù)據(jù)文件處于一致性狀態(tài)。
--redo-only 不回滾未提交事務(wù)
--copy-back 恢復(fù)備份目錄
使用innobackupex備份時(shí),其會(huì)調(diào)用xtrabackup備份所有的InnoDB表,復(fù)制所有關(guān)于表結(jié)構(gòu)定義的相關(guān)文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相關(guān)文件,同時(shí)還會(huì)備份觸發(fā)器和數(shù)據(jù)庫(kù)配置信息相關(guān)的文件,這些文件會(huì)被保存到一個(gè)以時(shí)間命名的目錄當(dāng)中。在備份的同時(shí),innobackupex還會(huì)在備份目錄中創(chuàng)建如下文件:
(1)xtrabackup_checkpoints -- 備份類型(如完全或增量)、備份狀態(tài)(如是否已經(jīng)為prepared狀態(tài))和LSN(日志序列號(hào))范圍信息:
每個(gè)InnoDB頁(yè)(通常為16k大小)
都會(huì)包含一個(gè)日志序列號(hào),即LSN,LSN是整個(gè)數(shù)據(jù)庫(kù)系統(tǒng)的系統(tǒng)版本號(hào),每個(gè)頁(yè)面相關(guān)的LSN能夠表明此頁(yè)面最近是如何發(fā)生改變的。
(2)xtrabackup_binlog_info -- mysql服務(wù)器當(dāng)前正在使用的二進(jìn)制日志文件及備份這一刻位置二進(jìn)制日志時(shí)間的位置。
(3)xtrabackup_binlog_pos_innodb -- 二進(jìn)制日志文件及用于InnoDB或XtraDB表的二進(jìn)制日志文件的當(dāng)前position。
(4)xtrabackup_binary -- 備份中用到的xtrabackup的可執(zhí)行文件;
(5)backup-my.cnf -- 備份命令用到的配置選項(xiàng)信息:
在使用innobackupex進(jìn)行備份時(shí),還可以使用--no-timestamp選項(xiàng)來(lái)阻止命令自動(dòng)創(chuàng)建一個(gè)以時(shí)間命名的目錄:如此一來(lái),innobackupex命令將會(huì)創(chuàng)建一個(gè)BACKUP-DIR目錄來(lái)存儲(chǔ)備份數(shù)據(jù)。
如果要使用一個(gè)最小權(quán)限的用戶進(jìn)行備份,則可基于如下命令創(chuàng)建此類用戶:如果要使用一個(gè)最小權(quán)限的用戶進(jìn)行備份,則可基于如下命令創(chuàng)建此類用戶:
mysql> CREATE USER 'bkpuser'@'localhost' IDENTIFIED BY '123456'; #創(chuàng)建用戶
mysql> REVOKE ALL PRIVILEGES,GRANT OPTION FROM 'bkpuser'; #回收此用戶所有權(quán)限
mysql> GRANT RELOAD,LOCK TABLES,RELICATION CLIENT ON *.* TO 'bkpuser'@'localhost'; #授權(quán)刷新、鎖定表、用戶查看服務(wù)器狀態(tài)
mysql> FLUSH PRIVILEGES; #刷新授權(quán)表
注意:備份時(shí)需啟動(dòng)MySQL,恢復(fù)時(shí)需關(guān)閉MySQL,清空mysql數(shù)據(jù)目錄且不能重新初始化,恢復(fù)數(shù)據(jù)后應(yīng)該立即進(jìn)行一次完全備份
2、xtrabackup全量備份與恢復(fù)
備份:
innobackupex --user=DBUSER --password=DBUSERPASS --defaults-file=/etc/my.cnf /path/to/BACKUP-DIR/
恢復(fù):
innobackupex --apply-log /backups/2018-07-30_11-04-55/
innobackupex --copy-back --defaults-file=/etc/my.cnf /backups/2018-07-30_11-04-55/
(1)準(zhǔn)備(prepare)一個(gè)完全備份
一般情況下,在備份完成后,數(shù)據(jù)尚且不能用于恢復(fù)操作,因?yàn)閭浞莸臄?shù)據(jù)中可能會(huì)包含尚未提交的事務(wù)或者已經(jīng)提交但尚未同步至數(shù)據(jù)文件中的事務(wù)。因此,此時(shí)數(shù)據(jù)文件仍處于不一致狀態(tài)。"準(zhǔn)備"的主要作用正是通過(guò)回滾未提交的事務(wù)及同步已經(jīng)提交的事務(wù)至數(shù)據(jù)文件也使用得數(shù)據(jù)文件處于一致性狀態(tài)。
innobackupex命令的--apply-log選項(xiàng)可用于實(shí)現(xiàn)上述功能,如下面的命令:
# innobackupex --apply-log /path/to/BACKUP-DIR
如果執(zhí)行正確,其最后輸出的幾行信息通常如下:
120407 09:01:04 innobackupex: completed OK!
在實(shí)現(xiàn)"準(zhǔn)備"的過(guò)程中,innobackupex通常還可以使用--user-memory選項(xiàng)來(lái)指定其可以使用的內(nèi)存的大小,默認(rèn)為100M.如果有足夠的內(nèi)存空間可用,可以多劃分一些內(nèi)存給prepare的過(guò)程,以提高其完成備份的速度。
(2)從一個(gè)完全備份中恢復(fù)數(shù)據(jù)
注意:恢復(fù)不用啟動(dòng)MySQL
innobackupex命令的--copy-back選項(xiàng)用于恢復(fù)操作,其通過(guò)復(fù)制所有數(shù)據(jù)相關(guān)的文件至mysql服務(wù)器DATADIR目錄中來(lái)執(zhí)行恢復(fù)過(guò)程。innobackupex通過(guò)backup-my.cnf來(lái)獲取DATADIR目錄的相關(guān)信息。
# innobackupex --copy-back /path/to/BACKUP-DIR
當(dāng)數(shù)據(jù)恢復(fù)至DATADIR目錄以后,還需要確保所有的數(shù)據(jù)文件的屬主和屬組均為正確的用戶,如mysql,否則,在啟動(dòng)mysqld之前還需要事先修改數(shù)據(jù)文件的屬主和屬組。如:
# chown -R mysql.mysql /mydata/data/
(3)實(shí)戰(zhàn)練習(xí)
(1)全量備份
[root@master backups]# innobackupex --user=root --password=123456 --host=127.0.0.1 /backups/ #在master上進(jìn)行全庫(kù)備份#語(yǔ)法解釋說(shuō)明:
#--user=root 指定備份用戶
#--password=123456 指定備份用戶密碼
#--host 指定主機(jī)
#/backups 指定備份目錄
[root@master backups]# ll
total 0
drwxr-x--- 7 root root 232 Jul 30 11:01 2018-07-30_11-01-37
[root@master backups]# ll 2018-07-30_11-01-37/ #查看備份數(shù)據(jù)
total 77856
-rw-r----- 1 root root 418 Jul 30 11:01 backup-my.cnf #備份用到的配置選項(xiàng)信息文件
-rw-r----- 1 root root 79691776 Jul 30 11:01 ibdata1 #數(shù)據(jù)文件
drwxr-x--- 2 root root 20 Jul 30 11:01 kim
drwxr-x--- 2 root root 4096 Jul 30 11:01 mysql
drwxr-x--- 2 root root 4096 Jul 30 11:01 performance_schema
drwxr-x--- 2 root root 20 Jul 30 11:01 repppp
drwxr-x--- 2 root root 4096 Jul 30 11:01 wordpress
-rw-r----- 1 root root 21 Jul 30 11:01 xtrabackup_binlog_info #mysql服務(wù)器當(dāng)前正在使用的二進(jìn)制日志文件和此時(shí)二進(jìn)制日志時(shí)間的位置信息文件
-rw-r----- 1 root root 113 Jul 30 11:01 xtrabackup_checkpoints #備份的類型、狀態(tài)和LSN狀態(tài)信息文件
-rw-r----- 1 root root 482 Jul 30 11:01 xtrabackup_info
-rw-r----- 1 root root 2560 Jul 30 11:01 xtrabackup_logfile #備份的日志文件
(2)恢復(fù)
[root@slave ~]# /etc/init.d/mysqld stop #停止slave上的mysql
Shutting down MySQL.. SUCCESS!
[root@slave tools]# yum install -y percona-xtrabackup-24-2.4.9-1.el7.x86_64.rpm #安裝xtrabackup
[root@master backups]# scp -r 2018-07-30_11-01-37/ root@192.168.56.12:/backups/ #從master上拷貝備份數(shù)據(jù)
[root@slave tools]# innobackupex --apply-log /backups/2018-07-30_11-01-37/ #合并數(shù)據(jù),使數(shù)據(jù)文件處于一致性的狀態(tài)
180729 23:18:23 innobackupex: Starting the apply-log operation
IMPORTANT: Please check that the apply-log run completes successfully.
At the end of a successful apply-log run innobackupex
prints "completed OK!".
innobackupex version 2.4.9 based on MySQL server 5.7.13 Linux (x86_64) (revision id: a467167cdd4)
xtrabackup: cd to /backups/2018-07-30_11-01-37/
xtrabackup: This target seems to be not prepared yet.
InnoDB: Number of pools: 1
xtrabackup: xtrabackup_logfile detected: size=8388608, start_lsn=(3127097)
......
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 3129915
180729 23:18:30 completed OK!
[root@slave ~]# rm -rf /usr/local/mysql/data/ #在slave上刪除原有的數(shù)據(jù)
[root@slave ~]# vim /etc/my.cnf #配置my.cnf的數(shù)據(jù)目錄路徑,否則會(huì)報(bào)錯(cuò),要和master一致
datadir=/usr/local/mysql/data
[root@slave ~]# innobackupex --copy-back /backups/2018-07-30_11-01-37/ #在slave上數(shù)據(jù)恢復(fù)
180729 23:32:03 innobackupex: Starting the copy-back operation
IMPORTANT: Please check that the copy-back run completes successfully.
At the end of a successful copy-back run innobackupex
prints "completed OK!".
......
180729 23:32:08 completed OK! #看到completed OK就是恢復(fù)正常了
[root@slave ~]# ll /usr/local/mysql/data/ #slave上查看數(shù)據(jù)目錄,可以看到數(shù)據(jù)已經(jīng)恢復(fù),但是屬主會(huì)有問題,需要進(jìn)行修改,所以一般使用mysql的運(yùn)行用戶進(jìn)行恢復(fù),否則需要進(jìn)行修改屬主和屬組信息
total 188432
-rw-r----- 1 root root 79691776 Jul 29 23:32 ibdata1
-rw-r----- 1 root root 50331648 Jul 29 23:32 ib_logfile0
-rw-r----- 1 root root 50331648 Jul 29 23:32 ib_logfile1
-rw-r----- 1 root root 12582912 Jul 29 23:32 ibtmp1
drwxr-x--- 2 root root 20 Jul 29 23:32 kim
drwxr-x--- 2 root root 4096 Jul 29 23:32 mysql
drwxr-x--- 2 root root 4096 Jul 29 23:32 performance_schema
drwxr-x--- 2 root root 20 Jul 29 23:32 repppp
drwxr-x--- 2 root root 4096 Jul 29 23:32 wordpress
-rw-r----- 1 root root 482 Jul 29 23:32 xtrabackup_info
[root@slave ~]# chown -R mysql.mysql /usr/local/mysql/data/ #修改屬主屬組
[root@slave ~]# /etc/init.d/mysqld start #啟動(dòng)mysql
Starting MySQL. SUCCESS!
[root@slave ~]# mysql -uroot -p -e "show databases;" #查看數(shù)據(jù),是否恢復(fù)
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| kim |
| mysql |
| performance_schema |
| repppp |
| wordpress |
+--------------------+
總結(jié)全庫(kù)備份與恢復(fù)三步曲:
a. innobackupex全量備份,并指定備份目錄路徑;
b. 在恢復(fù)前,需要使用--apply-log參數(shù)先進(jìn)行合并數(shù)據(jù)文件,確保數(shù)據(jù)的一致性要求;
c. 恢復(fù)時(shí),直接使用--copy-back參數(shù)進(jìn)行恢復(fù),需要注意的是,在my.cnf中要指定數(shù)據(jù)文件目錄的路徑。
3、xtrabackup增量備份與恢復(fù)
使用innobackupex進(jìn)行增量備份,每個(gè)InnoDB的頁(yè)面都會(huì)包含一個(gè)LSN信息,每當(dāng)相關(guān)的數(shù)據(jù)發(fā)生改變,相關(guān)的頁(yè)面的LSN就會(huì)自動(dòng)增長(zhǎng)。這正是InnoDB表可以進(jìn)行增量備份的基礎(chǔ),即innobackupex通過(guò)備份上次完全備份之后發(fā)生改變的頁(yè)面來(lái)實(shí)現(xiàn)。在進(jìn)行增量備份時(shí),首先要進(jìn)行一次全量備份,第一次增量備份是基于全備的,之后的增量備份都是基于上一次的增量備份的,以此類推。
要實(shí)現(xiàn)第一次增量備份,可以使用下面的命令進(jìn)行:
基于全量備份的增量備份與恢復(fù)
做一次增量備份(基于當(dāng)前最新的全量備份)
innobackupex --user=root --password=root --defaults-file=/etc/my.cnf --incremental /backups/ --incremental-basedir=/backups/2018-07-30_11-01-37
1. 準(zhǔn)備基于全量
innobackupex --user=root --password=root --defaults-file=/etc/my.cnf --apply-log --redo-only /backups/2018-07-30_11-01-37
2. 準(zhǔn)備基于增量
innobackupex --user=root --password=root --defaults-file=/etc/my.cnf --apply-log --redo-only /backups/2018-07-30_11-01-37 --incremental-dir=/backups/2018-07-30_13-51-47/
3. 恢復(fù)
innobackupex --copy-back --defaults-file=/etc/my.cnf /opt/2017-01-05_11-04-55/
解釋:
1. 2018-07-30_11-01-37指的是完全備份所在的目錄。
2. 2018-07-30_13-51-47指定是第一次基于2018-07-30_11-01-37增量備份的目錄,其他類似以此類推,即如果有多次增量備份。每一次都要執(zhí)行如上操作。
需要注意的是,增量備份僅能應(yīng)用于InnoDB或XtraDB表,對(duì)于MyISAM表而言,執(zhí)行增量備份時(shí)其實(shí)進(jìn)行的是完全備份。
"準(zhǔn)備"(prepare)增量備份與整理完全備份有著一些不同,尤其要注意的是:
①需要在每個(gè)備份 (包括完全和各個(gè)增量備份)上,將已經(jīng)提交的事務(wù)進(jìn)行"重放"。"重放"之后,所有的備份數(shù)據(jù)將合并到完全備份上。
②基于所有的備份將未提交的事務(wù)進(jìn)行"回滾"
(1)增量備份演示
[root@master backups]# innobackupex --user=root --password=123456 --host=127.0.0.1 /backups/ #全備數(shù)據(jù)[root@master ~]# mysql -uroot -p #在master上創(chuàng)建student庫(kù)并創(chuàng)建testtb表插入若干數(shù)據(jù)
Enter password:
mysql> create database student;
Query OK, 1 row affected (0.03 sec)
mysql> use student;
Database changed
mysql> create table testtb(id int);
Query OK, 0 rows affected (0.07 sec)
mysql> insert into testtb values(1),(10),(99);
Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from testtb;
+------+
| id |
+------+
| 1 |
| 10 |
| 99 |
+------+
3 rows in set (0.00 sec)
mysql> quit;
Bye
#使用innobackupex進(jìn)行增量備份
[root@master backups]# innobackupex --user=root --password=123456 --host=127.0.0.1 --incremental /backups/ --incremental-basedir=/backups/2018-07-30_11-01-37/
......
180730 13:51:50 Executing UNLOCK TABLES
180730 13:51:50 All tables unlocked
180730 13:51:50 Backup created in directory '/backups/2018-07-30_13-51-47/'
MySQL binlog position: filename 'mysql-bin.000005', position '664'
180730 13:51:50 [00] Writing /backups/2018-07-30_13-51-47/backup-my.cnf
180730 13:51:50 [00] ...done
180730 13:51:50 [00] Writing /backups/2018-07-30_13-51-47/xtrabackup_info
180730 13:51:50 [00] ...done
xtrabackup: Transaction log of lsn (3158741) to (3158741) was copied.
180730 13:51:50 completed OK!
[root@master backups]# ll #查看備份數(shù)據(jù)
total 0
drwxr-x--- 7 root root 232 Jul 30 11:01 2018-07-30_11-01-37 #全量備份數(shù)據(jù)目錄
drwxr-x--- 8 root root 273 Jul 30 13:51 2018-07-30_13-51-47 #增量備份數(shù)據(jù)目錄
[root@master 2018-07-30_11-01-37]# cat xtrabackup_checkpoints #查看全量備份的xtrabackup_checkpoints
backup_type = full-backuped #備份類型為全量備份
from_lsn = 0 #lsn從0開始
to_lsn = 3127097 #lsn到3127097結(jié)束
last_lsn = 3127097
compact = 0
recover_binlog_info = 0
[root@master 2018-07-30_13-51-47]# cat xtrabackup_checkpoints #查看增量備份的xtrabackup_checkpoints
backup_type = incremental #備份類型為增量備份
from_lsn = 3127097 #lsn從3127097開始
to_lsn = 3158741 #lsn到啊3158741結(jié)束
last_lsn = 3158741
compact = 0
recover_binlog_info = 0
(2)增量備份后數(shù)據(jù)恢復(fù)演示
(1)模擬mysql故障,刪除數(shù)據(jù)目錄所有數(shù)據(jù)
[root@master ~]# /etc/init.d/mysqld stop #模擬mysql故障,停止mysql
Shutting down MySQL.. SUCCESS!
[root@master ~]# rm -rf /usr/local/mysql/data/* #刪除數(shù)據(jù)目錄中的所有數(shù)據(jù)
(2)合并全備數(shù)據(jù)目錄,確保數(shù)據(jù)的一致性
[root@master ~]# innobackupex --apply-log --redo-only /backups/2018-07-30_11-01-37/
180730 14:05:27 innobackupex: Starting the apply-log operation
IMPORTANT: Please check that the apply-log run completes successfully.
At the end of a successful apply-log run innobackupex
prints "completed OK!".
innobackupex version 2.4.9 based on MySQL server 5.7.13 Linux (x86_64) (revision id: a467167cdd4)
xtrabackup: cd to /backups/2018-07-30_11-01-37/
......
......
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 3127106
InnoDB: Number of pools: 1
180730 14:05:29 completed OK!
(3)將增量備份數(shù)據(jù)合并到全備數(shù)據(jù)目錄當(dāng)中
[root@master ~]# innobackupex --apply-log --redo-only /backups/2018-07-30_11-01-37/ --incremental-dir=/backups/2018-07-30_13-51-47/
180730 14:06:42 innobackupex: Starting the apply-log operation
IMPORTANT: Please check that the apply-log run completes successfully.
At the end of a successful apply-log run innobackupex
prints "completed OK!".
......
......
180730 14:06:44 [00] ...done
180730 14:06:44 completed OK!
[root@master ~]# cat /backups/2018-07-30_11-01-37/xtrabackup_checkpoints
backup_type = log-applied #查看到數(shù)據(jù)備份類型是增加
from_lsn = 0 #lsn從0開始
to_lsn = 3158741 #lsn結(jié)束號(hào)為最新的lsn
last_lsn = 3158741
compact = 0
recover_binlog_info = 0
(4)恢復(fù)數(shù)據(jù)
[root@master ~]# innobackupex --copy-back /backups/2018-07-30_11-01-37/
180730 14:07:51 innobackupex: Starting the copy-back operation
IMPORTANT: Please check that the copy-back run completes successfully.
At the end of a successful copy-back run innobackupex
prints "completed OK!".
.......
.......
180730 14:08:17 [01] ...done
180730 14:08:17 completed OK!
[root@master ~]# ll /usr/local/mysql/data/
total 77844
-rw-r----- 1 root root 79691776 Jul 30 14:08 ibdata1
drwxr-x--- 2 root root 20 Jul 30 14:08 kim
drwxr-x--- 2 root root 4096 Jul 30 14:08 mysql
drwxr-x--- 2 root root 4096 Jul 30 14:08 performance_schema
drwxr-x--- 2 root root 20 Jul 30 14:08 repppp
drwxr-x--- 2 root root 56 Jul 30 14:08 student
drwxr-x--- 2 root root 4096 Jul 30 14:08 wordpress
-rw-r----- 1 root root 21 Jul 30 14:08 xtrabackup_binlog_pos_innodb
-rw-r----- 1 root root 554 Jul 30 14:08 xtrabackup_info
[root@master ~]# chown -R mysql.mysql /usr/local/mysql/data #更改數(shù)據(jù)的屬主屬組
[root@master ~]# /etc/init.d/mysqld start #啟動(dòng)mysql
Starting MySQL.Logging to '/usr/local/mysql/data/master.err'.
.. SUCCESS!
[root@master ~]# mysql -uroot -p -e "show databases;" #查看數(shù)據(jù)是否恢復(fù)
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| kim |
| mysql |
| performance_schema |
| repppp |
| student |
| wordpress |
+--------------------+
總結(jié):
(1)增量備份需要使用參數(shù)--incremental指定需要備份到哪個(gè)目錄,使用incremental-dir指定全備目錄;
(2)進(jìn)行數(shù)據(jù)備份時(shí),需要使用參數(shù)--apply-log redo-only先合并全備數(shù)據(jù)目錄數(shù)據(jù),確保全備數(shù)據(jù)目錄數(shù)據(jù)的一致性;
(3)再將增量備份數(shù)據(jù)使用參數(shù)--incremental-dir合并到全備數(shù)據(jù)當(dāng)中;
(4)最后通過(guò)最后的全備數(shù)據(jù)進(jìn)行恢復(fù)數(shù)據(jù),注意,如果有多個(gè)增量備份,需要逐一合并到全備數(shù)據(jù)當(dāng)中,再進(jìn)行恢復(fù)。

浙公網(wǎng)安備 33010602011771號(hào)