天天看點

mysql multi innobackupex,mysql之 Innobackupex(全備+增量)備份恢複

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 modified, this number gets incremented. An incremental backup copies all pages since a specific 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. 全備

[[email protected] 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. 檢視全備生成檔案

[[email protected] 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. 增量備份

-- 建立存放增量備份目錄并賦權

[[email protected] full]# mkdir -p /xtrabackup/increament/

[[email protected] full]# chown -R mysql:mysql /xtrabackup/increament/

[[email protected] 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

-- 正式開始增量備份

[[email protected] 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!

補充:

[[email protected] 2017-06-09_04-02-16]# pwd

/xtrabackup/increament/2017-06-09_04-02-16

-- 檢視增備産生的相關檔案

[[email protected] 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 含有備份類型

[[email protected] 2017-06-09_04-02-16]# more xtrabackup_info|grep ^incremental

incremental = Y

-- 檔案xtrabackup_checkpoints包含了備份的相關檢查點資訊

[[email protected] 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的位置

[[email protected] 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資料庫

[[email protected] ~]# service mysql stop

[[email protected] ~]# ps -ef|grep mysql

8.恢複資料庫  (先恢複全備,再按增量備份時間先後順序,依次恢複增量備份)

8.1 先恢複完整的備份集:

[[email protected] 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)

[[email protected] 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.将原有檔案夾重命名到新位置,并建立原檔案夾

[[email protected] full]# mv /data/mysql /data/mysqlbak

[[email protected] full]# mkdir -p /data/mysql

10.執行拷貝恢複的檔案到原來的資料位置

[[email protected] 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. 權限修改

[[email protected] ~]# mkdir -p /data/mysql/binarylog (說明:這裡我binlog在datadir在路徑下,是以要單獨為binlog建立目錄)

chown -R mysql:mysql /data/mysql

12. 啟動被恢複的執行個體

[ro[email protected] mysql]# mysqld_safe --defaults-file=/etc/my.cnf &

[[email protected] ~]# 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    感謝大師