基於Innobackupex的增備及恢複,innobackupex增備

來源:互聯網
上載者:User

基於Innobackupex的增備及恢複,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...        --這個主要是針對非innodb141222 14:55:08  innobackupex: All tables locked and flushed to disk141222 14:55:08  innobackupex: Starting to backup non-InnoDB tables and files  --開始備份非innodbinnobackupex: 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 finishxtrabackup: 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.deltaSHELL>  more /hotbak/inc/xtrabackup_info|grep ^incremental  incremental = Y--檔案xtrabackup_checkpoints包含了備份的相關檢查點資訊SHELL>  more /hotbak/inc/xtrabackup_checkpointsbackup_type = incrementalfrom_lsn = 391476482to_lsn = 391476794last_lsn = 391476794compact = 0--檔案xtrabackup_binlog_info包含了binlog的位置SHELL>  more xtrabackup_binlog_infoinst3606bin.000010      874--再次新增一條記錄,看看增備能否恢複robin@localhost[tempdb]> insert into tb values(3,'Inbinlog');Query OK, 1 row affected (0.00 sec)

3、恢複增量備份

a、先做基於全備的apply,注意,此時使用了--redo-onlySHELL> innobackupex --apply-log --redo-only --user=robin -password=xxx --port=3606 \> --defaults-file=/etc/my3606.cnf /hotbak/fullb、基於增備的apply,--此時沒有--redo-only,如果有多個增備,僅僅最後一個增備無需指定--redo-onlySHELL> 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 bytesApplying /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 bytesApplying /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 recoveryInnoDB: 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.000010InnoDB: 128 rollback segment(s) are active.             --提示有一些rollback段是活動的InnoDB: Waiting for purge to startInnoDB: 5.6.21 started; log sequence number 391476794   --........非重要訊息忽略........ Author: Leshami Blog: http://blog.csdn.net/leshamiinnobackupex: Starting to copy non-InnoDB files in '/hotbak/inc'innobackupex: to the full backup directory '/hotbak/full'   --將非InnoDB表複製到全備目錄,覆蓋方式innobackupex: Copying '/hotbak/inc/xtrabackup_binlog_info' to '/hotbak/full/xtrabackup_binlog_info'   ........非重要訊息忽略........141222 16:19:52  innobackupex: completed OK!             --完成基於增量的合并c、進行copy backSHELL>  mv /data/inst3606/data3606 /data/inst3606/data3606bk --更正@2014122251018,在mv之前建議先關閉執行個體,即先執行後面的mysqldownSHELL>  mkdir -p /data/inst3606/data3606SHELL> mysqldown -P3606     --copy back前關閉執行個體SHELL> netstat -nltp|grep mysql|grep 3606 SHELL> innobackupex --user=robin -password=xxx --port=3606 --copy-back /hotbak/full --defaults-file=/etc/my3606.cnfSHELL> chown -R mysql:mysql /data/inst3606/data3606  --啟動恢複後的執行個體SHELL> mysqld_safe --defaults-file=/etc/my3606.cnf &--驗證結果[mysql@app inst3606]$ sql -P3606--如下查詢,由於我們未使用binlog做完全恢複,因此無法查詢到id為3的記錄robin@localhost[(none)]> select * from tempdb.tb;+------+---------+| id   | val     |+------+---------+|    1 | fullbak ||    2 | Incbak  |+------+---------+

4、小結
a、增量備份是基於增量或全備的基礎之上完成的。
b、增量備份的基礎是InnoDB引擎使用了LSN機制,非InnoDB引擎不存在增量備份的說法,每次都是全備。
c、對於增量備份的恢複期間需要對已提交的事務前滾,未提交的交易回復。
d、增量備份的恢複應按照備份的順利逐個逐個replay,需要使用--apply-log --redo-only選項。
e、僅僅最後一個增量備份不需要使用--redo-only選項。
f、如果要做完全恢複或時間點復原,需要結合binlog來實現。

相關文章

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.