天天看點

Percona XtraBackup 核心文檔

features

percona xtrabackup

mysql enterprise backup

license

gpl

proprietary

price

free

included in subscription at $5000 per server

streaming and encryption formats

open source

supported mysql flavors

mysql, percona server, mariadb, percona xtradb cluster, mariadb galera cluster

mysql

supported operating systems

linux

linux, solaris, windows, osx, freebsd.

external graphical user interfaces to backup/recovery

zmanda recovery manager for mysql

mysql workbench, mysql en- terprise monitor

non-blocking innodb backups

yes

blocking myisam backups

full compressed backups

encrypted backups

streaming backups

parallel local backups

parallel compression

parallel encryption

partial backups

throttling

point-in-time recovery support

backup history table

individual tables export

restoring tables to a different server

innodb secondary indexes defragmentation

-

rsync support to minimize lock time

improved ftwrl handling

incremental compressed backups

fast incremental backups

incremental backups with archived logs feature in percona server

backup locks

partial backups of individual partitions

parallel apply-log

safe slave backups

compact backups

data & index file statistics

buffer pool state backups

individual partitions export

backup progress table

offline backups

parallel copy-back

backup to tape media managers

backup image validation

incremental backups with redo log only

cloud backups support

amazon s3

通過以上對比,你能夠清楚的知道

哪些是兩者共有的特點

哪些是隻有percona xtrabackup 才有的特點

哪些是ibbackup的特點

下面簡單羅列下,如需知道更多特性,請看後面的詳細内容

熱備,可以不需要中斷db伺服器

增量備份

流式備份(stream compressed)到其他伺服器

線上遷移表

可以非常簡單高效的建立一個新的slave

備份任務不會給伺服器帶來很高的負載

下載下傳一個linux-generic 版本的二進制包即可,友善簡單。
innobackupex 是一個用perl寫的工具,主要是封裝了xtrabackup【c寫的】 innobackupex 整合了 xtrabackup & 其他功能如:檔案copying and streaming,提供更多友善好用的功能 不管是innodb,xtradb,myisam , 都提供point-in-time備份和恢複
連接配接和權限

mysql> create user ’bkpuser’@’localhost’ identified by ’xx‘;

mysql> grant reload, lock tables, replication client on *.* to ’bkpuser’@’localhost’;

mysql> flush privileges;

<code>3.1.2.1 creating a backup with innobackupex</code>

innobackupex 是一個可以組合xbstream &amp; xbcrypt 的全量備份mysql資料庫的工具

為了建立一個備份,除了基本的連接配接參數外,額外僅僅隻需要一個備份路徑參數即可,就這麼簡單

$ innobackupex --user=dbuser --password=dbuserpass /path/to/backup-dir/

檢查最後的輸出來確定備份正确

在這個例子當中,預設備份檔案名會以timestamp命名:/path/to/backup-dir/2013-03-25_00-00-09

其他選項

--no-timestamp : 備份結果不會存儲在一個 stamped directory 中

$ innobackupex --user=dbuser --password=dbuserpass /path/to/backup-dir/ --no-timestamp

--defaults-file : 可以指定配置檔案,唯一的限制就是這個參數必須是第一個option

$ innobackupex --defaults-file=/tmp/other-my.cnf --user=dbuser --password=dbuserpass /path/to/backup-dir

<code>3.1.2.2 preparing a full backup with innobackupex</code>

這個階段叫:prepare stage 。

建立備份後,這時候資料還不能被立刻使用。因為這時候還有很多uncommitted 事務沒有被復原,同樣還有很多事務沒有被replayed。

通過--apply-log 後,這些資料檔案才能算是一緻的資料。

$ innobackupex --apply-log /path/to/backup-dir

詳解prepare stage

innobackupex 通過backup-my.cnf檔案來開始prepare

然後,innobackupex 回放已經送出【committed】的事務,復原未送出【uncommitted】的事務

當以上步驟完成後,所有資訊都準備完畢,重做日志會被re-created

相當于調用了兩次xtrabackup --prepare 兩次。

--use-memory option: prepare stage的專用參數,預設100m,使用記憶體越多,恢複速度越快。

$ innobackupex --apply-log --use-memory=4g /path/to/backup-dir

<code>3.1.2.3 restoring a full backup with innobackupex</code>

為了友善,innobackupex提供了 --copy-back選項,它可以自動幫助我們将備份檔案copy到datadir

$ innobackupex --copy-back /path/to/backup-dir

它會根據伺服器上的my.cnf 來将所有資料相關檔案拷貝到指定的 datadir

你可以檢查最後的輸出

注意:一定要保證datadir為空目錄,否則有可能報錯,如果檔案已經存在,就不會複制。 除非指定了 --force-non-empty-directories.

并且記住:restore的時候,必須保證mysql處于shutdown狀态。

最後記住:重新給檔案賦予正确的屬組合權限

$ chown -r mysql:mysql /var/lib/mysql

<code>3.1.3.1 增量備份</code>

恢複速度太慢,忽略

percona xtrabackup 提供了部分表【partial】備份,顧名思義:隻備份一部分表或庫

前提是:你必須設定了 <code>innodb_file_per_table</code>

對于部分表備份,隻有一個忠告:千萬别将prepared backup copy back回家。

restoring partial backups,你應該importing 表,而不是 --copy-back選項。

<code>3.1.4.1 creating partial backups</code>

有三種備份方式: regular expressions(--include), enumerating (--tables-file)以及提供databases (--databases)

regular expressions(--include)

格式:databasename.tablename.

例如:

$ innobackupex --include=’^mydatabase[.]mytable’ /path/to/backup

以上指令,隻有被比對的表才會被備份

using the --tables-file option

可以使用檔案清單,一行一個,格式: databasename.tablename
echo "mydatabase.mytable" &gt; /tmp/tables.txt innobackupex --tables-file=/tmp/tables.txt /path/to/backup

using the --databases option

以空格為間隔符,指定表或者庫
$ innobackupex --databases="mydatabase.mytable mysql" /path/to/backup

以上指令,建立了一個包含 mytable表 和 mysql 庫的備份

<code>3.1.4.2 preparing partial backups</code>

對于部分表備份的prepare,步驟類似 restoring individual tables一樣 : 應用日志 并且 使用--export 選項:

innobackupex --apply-log --export /path/to/partial/backup

你會收到一部分warnings,因為這是部分表prepare和export,沒有被prepare的表會從資料字典移除。

<code>3.1.4.3 restoring partial backups</code>

方法參考: restoring individual tables 當然你也可以将其copying back 到一個全新的server上,系統表空間可以被重建
$ sudo mysql_install_db --user=mysql

compact備份指的是: 不備份二級索引,這樣空間占用少。

缺點是: prepare &amp; restore 的時候非常慢 , 因為需要rebuild 二級索引

backup size without --compact option 2.0g 2013-02-01_10-18-38 backup size taken with --compact option 1.4g 2013-02-01_10-29-48

<code>creating compact backups</code>

$ innobackupex --compact /data/backups

檢查 xtrabackup_checkpoints,當指定了--compact, compact 是1,否則是0

<code>preparing compact backups</code>

prepare compact 備份,必須加上這個新參數:--rebuild-indexes

$ innobackupex --apply-log --rebuild-indexes /data/backups/2013-02-01_10-29-48

日志結果:

<code>restoring compact backups</code>

不常用

<code>3.1.7.1 streaming and compressing backups</code>

streaming 模式,将備份以 tar 或者 xbstream 的格式發送,而不是傳統的直接copying 檔案

你可以寫自己的過濾規則來或者你想要的資料,這樣靈活性更大。比如,你還可以使用自己的指定的壓縮。

另一個使用streaming備份的好處就是: 自動加密

想要使用streaming 特性,必須使用 --stream 選項,并且提供這兩種format (tar 或者 xbstream)以及存儲temporary files的目錄

$ innobackupex --stream=tar /tmp

當壓縮功能打開時,xtrabackup 會用quicklz算法壓縮innodb表,*.qp結尾。

當使用xbstream的時候,備份在 copying 檔案 以及 compress 檔案上,使用并行【parallel】功能來提升速度

使用xbstream的例子

将一個完整的全備直接備份成一個檔案:

$ innobackupex --stream=xbstream /root/backup/ &gt; /root/backup/backup.xbstream

stream &amp; compress 模式開啟:

$ innobackupex --stream=xbstream --compress /root/backup/ &gt; /root/backup/backup.xbstream

解壓到/root/backup中:

$ xbstream -x &lt; backup.xbstream -c /root/backup/

将一個壓縮的備份傳送到其他伺服器并解壓:

$ innobackupex --compress --stream=xbstream /root/backup/ | ssh user@otherhost "xbstream -x -c /root/backup/

使用tar的例子

$ innobackupex --stream=tar /root/backup/ &gt; /root/backup/out.tar $ innobackupex --stream=tar ./ | ssh user@destination "cat - &gt; /data/backups/backup.tar" $ innobackupex --stream=tar ./ | gzip - &gt; backup.tar.gz $ innobackupex --stream=tar ./ | bzip2 - &gt; backup.tar.bz2





注意:以上streaming 模式并沒有對備份進行prepare ,是以在restore 之前,必須prepare

<code>3.1.8 taking backups in replication environments</code>

跟複制相關的備份需要關注的option

the --slave-info option

當你在slave上備份的時候,這個參數特别有用。它會保留相關的複制資訊如:對應的master binlog position 和 file。

也會将相關資訊change master等資訊寫入 xtrabackup_slave_info

the --safe-slave-backup option

為了確定一緻性複制狀态,這個選項會将sql_thread stop掉,并且等待開啟備份直到slave_open_temp_tables 為0

如果沒有open的tempory 表,那麼備份就會開始,否則sql_thread就會一直stop,直到slave_open_temp_tables 為0

如果--safe-slave-backup-timeout(預設300s)時間超過後,slave_open_temp_tables還不為0,那麼備份就會失敗,sql thread 會被restart

如果從slave上備份,這個參數推薦使用

<code>accelerating with --parallel copy and –compress-threads</code>

--parallel 可以指定并行線程數來并發copy innodb資料檔案

$ innobackupex --parallel=4 /path/to/backup

--compress-threads : 這個參數隻和xbstream綁定使用,并發壓縮

$ innobackupex --stream=xbstream --compress --compress-threads=4 ./ &gt; backup.xbstream

在prepare之前,必須先解壓xbstream

<code>accelerating with --rsync option</code>

這個參數是用來提升非innodb引擎的備份,意義不是很大

注意: --rsync 不能與 --remote-host ,--stream 一起使用

<code>throttling backups with innobackupex</code>

如果壓力特别大,這個參數可以限流,類似--sleep

<code>restoring individual tables (5.6+ 才支援)</code>

exporting tables

$ innobackupex --apply-log --export /path/to/backup

這個指令會為每個ibd表都增加一個.exp 的檔案,有這個檔案,才能夠正确的import到online server

importing tables

導入資料前,必須先在其他伺服器上建立好同樣的表結構,如果錯誤,會造成db server hang,小心

otherserver|mysql&gt; create table mytable (...) engine=innodb;

然後discard 表空間

otherserver|mysql&gt; alter table mydatabase.mytable discard tablespace;

最後,拷貝檔案.mydatabase.mytable.ibd , mytale.exp 到資料目錄,import到tablespace中

otherserver|mysql&gt; alter table mydatabase.mytable import tablespace;

<code>point-in-time recovery</code>

基于binlog

<code>improved flush tables with read lock handling</code>

percona server 才支援

<code>waiting for queries to finish</code>

<code>killing the blocking queries</code>

<code>store backup history on the server</code>

<code>making a backup</code>

innobackupex 調用 xtrabackup --suspend-at-end xtrabackup 開始拷貝 innodb 資料檔案 當xtrabackup拷貝完成innodb檔案後,會建立 xtrabackup_suspended_2 當innobackupex看到xtrabackup_suspended_2檔案被建立後,會調用指令flush table with read lock innobackupex開始檢查db server支援的特性,如:gtid,backup locks,changed page bitmap等 然後開始拷貝非innodb檔案 當所有的檔案都備份完成後,結束redo 事務的拷貝。 接下來釋放鎖 删除xtrabackup_suspended_2檔案 , 并且退出

再這些過程中,還建立了如下檔案

xtrabackup_checkpoints : 記錄了lsn和備份類型

xtrabackup_binlog_info : 記錄了備份開始時的binary日志資訊

xtrabackup_binlog_pos_innodb : 記錄了備份innodb redo事務時刻的binary log資訊

xtrabackup_slave_info : 記錄了slave上show slave status相關資訊

backup-my.cnf : 記錄了部分需要備份的資訊

xtrabackup_binary : 記錄了這段備份期間執行過的事務

<code>restoring a backup</code>

copy-back or move-back 它自己不會記錄日志,需要我們指定管道記錄日志

<code>references</code>

羅列了全的innobackupex相關的參數和選項
我們隻用innobackupex即可
用的少

<code>create the backup階段</code>

下面是一個簡單的案例

$ innobackupex /data/backups

<code>prepare the backup階段</code>

--apply-log --use-memory 是常用的選項

$ innobackupex --use-memory=4g --apply-log /data/backups/2010-03-13_02-42-44/

<code>restore the backup階段</code>

恢複一個已經prepared的備份,首先要停掉mysql server,然後使用--copy-back

innobackupex --copy-back /data/backups/2010-03-13_02-42-44/ 調整權限 chown -r mysql:mysql /data/mysql_data/mysql

<code>stream for tar</code>

流的方式備份可以将備份用tar寫到标準輸出來取代之前直接copy檔案,該參數必須是第一個option

然後可以通過管道的方式用gzip等壓縮工具壓縮,或者直接傳送其他遠端伺服器上

解壓的時候,必須使用-i參數,tar -ixvf backup.tar

stream the backup into a tar archived named ‘backup.tar’

the same, but compress it

send it to another server instead of storing it locally

the same thing with can be done with the ‘’netcat’‘.

the same thing, but done as a one-liner:

$ ssh user@desthost "( nc -l 9999 &gt; /data/backups/backup.tar &amp; )" &amp;&amp; innobackupex --stream=tar ./ | nc desthos

throttling the throughput to 10mb/sec. this requires the ‘pv’ tools; you can find them at the official site or install it from the distribution package (“apt-get install pv”)

$ innobackupex --stream=tar ./ | pv -q -l10m | ssh user@desthost "cat - &gt; /data/backups/backup.tar"

checksumming the backup during the streaming

<code>stream for xbstream</code>

舉個例子:

stream the backup into a tar archived named backup.xbstream

the same but with compression

to unpack the backup to the current directory:

sending backup compressed directly to another host and unpacking it:

parallel compression with parallel copying backup

恢複慢,用的少

--compress 可以幫你

$ innobackupex --compress /data/backup

如果想要多線程compress,可以使用 --compress-threads

$ innobackupex --compress --compress-threads=4 /data/backup

preparing compress backup時候,可以加上 --decompress

$ innobackupex --decompress /data/backup/2013-08-01_11-24-04/
innobackupex 就夠了
5.1 ,5.5 對于innodb 壓縮表的updates類型的redo事件存在bug,并沒有修複,直到5.6.12 (bug #16267120) 5.6中innodb_log_compressed_pages這個參數,不推薦設定off,預設是on 在optimize table(bug #1541763) 或者 alter table ... tablespace(bug #1532878) 的時候去備份,會導緻備份的資料無法recover compact 備份目前還存在bug,建議不要用 (bug #1192834)