mysql之 Innobackupex(全備+增量)備份恢復

張衝andy發表於2017-06-08
MySQL的熱備(物理備份)可以採取全備加增量備份的方式來減輕資料庫I/O壓力及系統資源的佔用。增量備份主要是以全備或增量備份為基礎,備份那些變更過的頁面。其備份的原理是基於一個不斷增長的LSN序列,這個LSN與Oracle的SCN類似。在恢復期間,我們需要將已提交的事務前滾,未提交的事務回滾。本文主要描述了增量備份及增量恢復。
 
1、增備的相關知識點
    As not all information changes between each backup, the incremental backup strategy uses this to reduce the storage needs and the duration of making a backup. This can be done because each InnoDB page has a log sequence number, LSN, which acts as a version number of the entire database. Every time the database is modi?ed, this number gets incremented. An incremental backup copies all pages since a speci?c LSN. Once the pages have been put together in their respective order, applying the logs will recreate the process that affected the database, yielding the data at the moment of the most recently created backup.
    增備是備份上次以來發生變化的頁面,透過增備可以減輕儲存以及系統資源開銷。增量備份主要針對於InnoDB,因為InnoDB採用了日誌序列號(LSN)的方式。InnoDB的LSN是一個增長的序列,類似於Oracle的SCN,記錄了InnoDB的變化情況。增量備份則是備份特定的LSN之後變化的情況。透過按序重組這些LSN即可將資料庫恢復到故障點或任意時刻。


    innobackupex --incremental /data/backups --incremental-lsn=1291135
    innobackupex --incremental /data/backups --incremental-lsn=1358967  
    如上,我們可以使用--incremental-lsn選項來實施增量備份
 
    Warning: This procedure only affects XtraDB or InnoDB-based tables. Other tables with a different storage engine, e.g. MyISAM, will be copied entirely each time an incremental backup is performed.
    對於非XtraDB或者InnoDB儲存引擎,熱備方式依舊會全部備份所有的資料檔案,索引檔案,格式檔案等。
 
    Preparing an Incremental Backup with innobackupex Preparing incremental backups is a bit different than full ones. This is, perhaps, the stage where more attention is needed:
    ? First, only the committed transactions must be replayed on each backup. This will merge the base full backup with the incremental ones.
    ? Then, the uncommitted transaction must be rolled back in order to have a ready-to-use backup.
    對於增量備份的Prepare階段,有2個需要注意的地方,一個是提交的事務需要replayed,一個未提交的事務需要rollback。
 
    If you replay the committed transactions and rollback the uncommitted ones on the base backup, you will not be able to add the incremental ones. If you do this on an incremental one, you won’t be able to add data from that moment and the remaining increments. Having this in mind, the procedure is very straight-forward using the --redo-only option, starting with the base backup:
    如果在Prepare階段replay了已提交的事務以及回滾了未提交的事務,則後續的增量備份無法新增到當前全備。因此在Prepare階段全備應使用--redo-only選項。
 
    --redo-only should be used when merging all incrementals except the last one. That’s why the previous line doesn’t contain the --redo-only option. Even if the --redo-only was used on the last step, backup would still be consistent but in that case server would perform the rollback phase.
    對於存在多次增量的情形,僅僅只有最後一個增量不需要使用--redo-only 選項。如果使用了的話,rollback將由伺服器啟動的時候來完成。


二、 演示
1. 準備實驗環境
mysql> select version();
+------------+
| version()  |
+------------+
| 5.6.25-log |
+------------+
1 row in set (0.00 sec)
mysql> create database inc_rec;
Query OK, 1 row affected (0.00 sec)
mysql> use inc_rec;
Database changed
mysql> create table andy (id int);
Query OK, 0 rows affected (0.08 sec)
mysql> insert into andy values(1),(2);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
2. 全備
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=oracle --port=3606 /xtrabackup/full/
xtrabackup: Transaction log of lsn (1648193) to (1648193) was copied.
170609 03:53:56 completed OK!
3. 檢視全備生成檔案
[root@mysql02 full]# ll /xtrabackup/full/
total 4
drwxr-x---. 6 root root 4096 Jun  9 03:53 2017-06-09_03-53-51
4. 模擬業務新資料
mysql> insert into andy values(3),(4);
Query OK, 2 rows affected (0.14 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
5. 增量備份
-- 建立存放增量備份目錄並賦權
[root@mysql02 full]# mkdir -p /xtrabackup/increament/
[root@mysql02 full]# chown -R mysql:mysql /xtrabackup/increament/
[root@mysql02 full]# ll /xtrabackup/
total 8
drwxr-xr-x. 3 mysql mysql 4096 Jun  9 03:53 full
drwxr-xr-x. 2 mysql mysql 4096 Jun  9 04:00 increament
-- 正式開始增量備份
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=oracle  --incremental  \
--incremental-basedir=/xtrabackup/full/2017-06-09_03-53-51/ /xtrabackup/increament/
########################################下面是增量備份輸出
170609 04:02:21 Backup created in directory '/xtrabackup/increament/2017-06-09_04-02-16/'
MySQL binlog position: filename 'binlog.000003', position '894'
。。。省略
xtrabackup: Transaction log of lsn (1652210) to (1652210) was copied.
170609 04:02:22 completed OK!


補充:
[root@mysql02 2017-06-09_04-02-16]# pwd
/xtrabackup/increament/2017-06-09_04-02-16
-- 檢視增備產生的相關檔案  
[root@mysql02 2017-06-09_04-02-16]# ll
total 8580
-rw-r-----. 1 root root     418 Jun  9 04:02 backup-my.cnf
-rw-r-----. 1 root root  360448 Jun  9 04:02 ibdata1.delta
-rw-r-----. 1 root root      44 Jun  9 04:02 ibdata1.meta
drwxr-x---. 2 root root    4096 Jun  9 04:02 inc_rec
drwxr-x---. 2 root root    4096 Jun  9 04:02 mysql
drwxr-x---. 2 root root    4096 Jun  9 04:02 performance_schema
drwxr-x---. 2 root root    4096 Jun  9 04:02 test
-rw-r-----. 1 root root      18 Jun  9 04:02 xtrabackup_binlog_info
-rw-r-----. 1 root root     117 Jun  9 04:02 xtrabackup_checkpoints
-rw-r-----. 1 root root     584 Jun  9 04:02 xtrabackup_info
-rw-r-----. 1 root root 8388608 Jun  9 04:24 xtrabackup_logfile
-- 檔案 xtrabackup_info 含有備份型別
[root@mysql02 2017-06-09_04-02-16]# more xtrabackup_info|grep ^incremental 
incremental = Y
-- 檔案xtrabackup_checkpoints包含了備份的相關檢查點資訊  
[root@mysql02 2017-06-09_04-02-16]# more xtrabackup_checkpoints  
backup_type = incremental
from_lsn = 1648193
to_lsn = 1652210
last_lsn = 1652210
compact = 0
recover_binlog_info = 0
-- 檔案xtrabackup_binlog_info包含了binlog的位置  
[root@mysql02 2017-06-09_04-02-16]# more xtrabackup_binlog_info  
binlog.000003 894
6. 誤操作,truncate表
mysql> truncate table andy;
Query OK, 0 rows affected (0.22 sec)
mysql> select * from andy;
Empty set (0.01 sec)
7. 停止mysql資料庫
[root@mysql02 ~]# service mysql stop
[root@mysql02 ~]# ps -ef|grep mysql
8.恢復資料庫  (先恢復全備,再按增量備份時間先後順序,依次恢復增量備份)
8.1 先恢復完整的備份集:
[root@mysql02 full]#  innobackupex --defaults-file=/etc/my.cnf --user=root --apply-log --redo-only /xtrabackup/full/2017-06-09_03-53-51/
170609 04:19:30 completed OK!
8.2 在恢復增量備份集:   (如果有多份增量備份,最好最後一份增量不用 --redo-only , 其他的都用 --redo-only)
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --apply-log  /xtrabackup/full/2017-06-09_03-53-51  --incremental-dir=/xtrabackup/increament/2017-06-09_04-02-16/
170609 04:24:45 completed OK!  #結果出現completed OK表示完全成功
說明: /xtrabackup/full/2017-06-09_03-53-51 為全備基目錄 , incremental-dir 為增量備份目錄
9.將原有資料夾重新命名到新位置,並建立原資料夾 
[root@mysql02 full]# mv /data/mysql /data/mysqlbak
[root@mysql02 full]# mkdir -p /data/mysql
10.執行複製恢復的檔案到原來的資料位置
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --copy-back  /xtrabackup/full/2017-06-09_03-53-51/
170609 04:33:06 completed OK! #結果出現completed OK表示完全成功
說明: /xtrabackup/full/2017-06-09_03-53-51/ 為全備基目錄
11. 許可權修改
[root@mysql02 ~]# mkdir -p /data/mysql/binarylog (說明:這裡我binlog在datadir在路徑下,所以要單獨為binlog建立目錄)
chown -R mysql:mysql /data/mysql 
12. 啟動被恢復的例項
[root@mysql02 mysql]# mysqld_safe --defaults-file=/etc/my.cnf & 
[root@mysql02 ~]# mysql -uroot -poracle
mysql> use inc_rec;
mysql> select * from andy;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |     > 恢復成功!
|    4 |
+------+
總結:
a、增量備份是基於增量或全備的基礎之上完成的。
b、增量備份的基礎是InnoDB引擎使用了LSN機制,非InnoDB引擎不存在增量備份的說法,每次都是全備。
c、對於增量備份的恢復期間需要對已提交的事務前滾,未提交的事務回滾。
d、增量備份的恢復應按照備份的順利逐個逐個replay,需要使用--apply-log --redo-only選項。
e、僅僅最後一個增量備份不需要使用--redo-only選項。
f、如果要做完全恢復或時點恢復,需要結合binlog來實現。
參考:http://blog.csdn.net/leshami/article/details/42141627    感謝大師

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31383567/viewspace-2140506/,如需轉載,請註明出處,否則將追究法律責任。

相關文章