基於Innobackupex的增備及恢複

來源:互聯網
上載者:User

基於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將由伺服器啟動的時候來完成。

2、示範增量備份

a、建立示範環境
robin@localhost[(none)]> create database tempdb;

robin@localhost[(none)]> use tempdb;

robin@localhost[tempdb]> create table tb(id smallint,val varchar(20));

robin@localhost[tempdb]> insert into tb  values(1,'fullbak');

b、啟動一個全備
SHELL> innobackupex --user=robin -password=xxx --port=3606 --socket=/tmp/mysql3606.sock --defaults-file=/etc/my3606.cnf \
> /hotbak/full --no-timestamp

--再新增一條記錄,以便區分全備與增備
robin@localhost[tempdb]> insert into tb values(2,'Incbak');

c、啟動一個增量備份
SHELL> innobackupex --user=robin -password=xxx --port=3606 --socket=/tmp/mysql3606.sock --defaults-file=/etc/my3606.cnf \
> --incremental /hotbak/inc --incremental-basedir=/hotbak/full --no-timestamp
      --........非重要訊息忽略........
innobackupex: Using mysql server version 5.6.12-log            --當前mysql的版本
innobackupex: Created backup directory /hotbak/inc              -- 建立備份目錄             
innobackupex: Suspend file '/hotbak/inc/xtrabackup_suspended_2' -- 同時會建立相應的suspended目錄供臨時使用
            --........非重要訊息忽略........
xtrabackup: using the full scan for incremental backup
[01] Copying ./ibdata1 to /hotbak/inc/ibdata1.delta            --可以看到生產了相應的delta檔案,即增量部分
[01]        ...done                                           
>> log scanned up to (391476794)
xtrabackup: Creating suspend file '/hotbak/inc/xtrabackup_suspended_2' with pid '25001'
  --........非重要訊息忽略........
141222 14:55:08  innobackupex: Executing FLUSH TABLES WITH READ LOCK...        --這個主要是針對非innodb
141222 14:55:08  innobackupex: All tables locked and flushed to disk

141222 14:55:08  innobackupex: Starting to backup non-InnoDB tables and files  --開始備份非innodb
innobackupex: in subdirectories of '/data/inst3606/data3606'                  --所有的非innodb會被重新備份一次
innobackupex: Backing up files '/data/inst3606/data3606/mysql/*.{frm,isl,MYD,...,CSV,opt,par}' (77 files)

141222 14:55:09  innobackupex: Executing FLUSH ENGINE LOGS...                  --日誌切換
141222 14:55:09  innobackupex: Waiting for log copying to finish

xtrabackup: The latest check point (for incremental): '391476794'              --檢查點位置
xtrabackup: Stopping log copying thread.
.>> log scanned up to (391476794)
  --........非重要訊息忽略........
xtrabackup: Creating suspend file '/hotbak/inc/xtrabackup_log_copied' with pid '25001'
xtrabackup: Transaction log of lsn (391476794) to (391476794) was copied.        --複製交易記錄
141222 14:55:10  innobackupex: All tables unlocked                              --表解鎖
  --........非重要訊息忽略........
141222 14:55:10  innobackupex: completed OK!

d、查看增備後的相關檔案
SHELL>  ls -hltr /hotbak/inc/*delta*
-rw-r----- 1 root root 96K 2014/12/22 14:55 /hotbak/inc/ibdata1.delta

SHELL>  more /hotbak/inc/xtrabackup_info|grep ^incremental 
incremental = Y

--檔案xtrabackup_checkpoints包含了備份的相關檢查點資訊
SHELL>  more /hotbak/inc/xtrabackup_checkpoints
backup_type = incremental
from_lsn = 391476482
to_lsn = 391476794
last_lsn = 391476794
compact = 0

--檔案xtrabackup_binlog_info包含了binlog的位置
SHELL>  more xtrabackup_binlog_info
inst3606bin.000010      874

--再次新增一條記錄,看看增備能否恢複
robin@localhost[tempdb]> insert into tb values(3,'Inbinlog');
Query OK, 1 row affected (0.00 sec)

3、恢複增量備份

a、先做基於全備的apply,注意,此時使用了--redo-only
SHELL> innobackupex --apply-log --redo-only --user=robin -password=xxx --port=3606 \
> --defaults-file=/etc/my3606.cnf /hotbak/full

b、基於增備的apply,
--此時沒有--redo-only,如果有多個增備,僅僅最後一個增備無需指定--redo-only
SHELL> innobackupex --apply-log --user=robin -password=xxx --port=3606 --defaults-file=/etc/my3606.cnf \
> /hotbak/full --incremental-dir=/hotbak/inc
  --........非重要訊息忽略........
xtrabackup: page size for /hotbak/inc/tempdb/tb.ibd.delta is 16384 bytes
Applying /hotbak/inc/tempdb/tb.ibd.delta to ./tempdb/tb.ibd...  --可以看到資料庫tempdb下表tb被apply到全備目錄
xtrabackup: page size for /hotbak/inc/sakila/t.ibd.delta is 16384 bytes
Applying /hotbak/inc/sakila/t.ibd.delta to ./sakila/t.ibd...
          -- ........非重要訊息忽略........
xtrabackup: Starting InnoDB instance for recovery.              --啟動InnoDB執行個體恢複
xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
  --........非重要訊息忽略........
InnoDB: Highest supported file format is Barracuda.
InnoDB: The log sequence numbers 391476482 and 391476482 in ibdata files do not match
  the log sequence number 391476794 in the ib_logfiles! --提示log sequence不一致
InnoDB: Database was not shutdown normally!             
InnoDB: Starting crash recovery.                        --啟動crash recovery
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages
InnoDB: from the doublewrite buffer...
InnoDB: Last MySQL binlog file position 0 874, file name inst3606bin.000010
InnoDB: 128 rollback segment(s) are active.            --提示有一些rollback段是活動的
InnoDB: Waiting for purge to start
InnoDB: 5.6.21 started; log sequence number 391476794
  --........非重要訊息忽略........ Author: Leshami Blog: <A href="

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.