Xtrabackup包含兩個主要的工具,即xtrabackup和innobackupex,二者區別如下:
• xtrabackup只能備份innodb和xtradb引擎表,而不能備份MyISAM表
• innobackupex是一個封裝了xtrabackup的Perl腳本,支持同時備份innodb和MyISAM,但在對MyISAM備份時需要加一個全局的讀鎖
CREATE USER 'backup'@'localhost' IDENTIFIED BY 'backup';
GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT,PROCESS ON *.* TO 'backup'@'localhost';
innobackupex調用xtrabackup備份xtradb和innodb,調用perl腳本備份MyISAM表
innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup'/data/mysqldata/backup/
如果使用–defaults-file選項,則必須在第一位
[mysql@master backup]$ pwd
/data/mysqldata/backup
[mysql@master backup]$ ls
2016-08-18_18-23-33
–no-timestamp 該選項告訴innobackupex不創建一個時間戳目錄存儲備份
innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup'--no-timestamp /data/mysqldata/backup/base
上例中備份會存放在base目錄下,如果目錄不存在會自動創建
prepare的目的是跑一下redo,將未提交的rollback,回滾的回滾,跑出一個一致性的備份
innobackupex --apply-log /data/mysqldata/backup/base/
查看輸出的最後一行 completed OK!表示prepare成功
prepare前
backup-my.cnf ibdata1 performance_schema test xtrabackup_checkpoints xtrabackup_logfile
fandb mysql sakila xtrabackup_binlog_info xtrabackup_info
prepare後
backup-my.cnf ib_logfile0 ibtmp1 sakila xtrabackup_binlog_pos_innodb xtrabackup_logfile
fandb ib_logfile1 mysql test xtrabackup_checkpoints
ibdata1 ib_logfile2 performance_schema xtrabackup_binlog_info xtrabackup_info
ib_logfile0,1,2由prepare生成
–use-memory 使用此參數可以提升prepare速度,默認100M
innobackupex --apply-log --use-memory=4G/data/mysqldata/backup/base/
為了方便,innobackupex有一個–copy-back選項,可以將備份拷貝回datadir下
innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --copy-back /data/mysqldata/backup/base
datadir必須是空的,除非使用了–force-non-empty-directories 參數.MySQL在copy-back時應該是關閉的
如果創建備份時使用的是其他OS用戶,那麼在copy-back後你應該修改權限
chown -R mysql:mysql
增量備份之前必須有一個全備份.
innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup'--no-timestamp /data/mysqldata/backup/base
備份成功後,會生成一個文件xtrabackup_checkpoints
[mysql@master backup]$ more base/xtrabackup_checkpoints
backup_type = full-backuped
from_lsn =0
to_lsn =143682470
last_lsn =143682470
compact =0
recover_binlog_info =0
使用上面的全備做為基礎,做增量備份
innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup'--no-timestamp --incremental /data/mysqldata/backup/incr1 --incremental-basedir=/data/mysqldata/backup/base
備份成功後,查看增備的xtrabackup_checkpoints
[mysql@master backup]$ more incr1/xtrabackup_checkpoints
backup_type = incremental
from_lsn =143682470
to_lsn =143683016
last_lsn =143683016
compact =0
recover_binlog_info =0
再次創建一個增量備份,將以剛才的增備作為基礎
innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup'--no-timestamp --incremental /data/mysqldata/backup/incr2 --incremental-basedir=/data/mysqldata/backup/incr1
[mysql@master backup]$ more incr2/xtrabackup_checkpoints
backup_type = incremental
from_lsn =143683016
to_lsn =143683562
last_lsn =143683562
compact =0
recover_binlog_info =0
增倍時應該是要讀取base的xtrabackup_checkpoints文件,從該文件的last_lsn開始備份.
於是我手動創建一個目錄incr3,並創建一個文件xtrabackup_checkpoints,以此作為base也是可以備份的.
將該xtrabackup_checkpoints文件的last_lsn改為0也可以,但仍不是全備
手動指定lsn,也可以成功創建增量備份.這種方法適用於當base不可用時
innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup'--incremental /data/mysqldata/backup/incr1 --incremental-lsn=143680036
–incremental-lsn=name
This option specifies the log sequence number (LSN) to
use for the incremental backup. The option accepts a
string argument. It is used with the –incremental
option. It is used instead of specifying
–incremental-basedir. For databases created by MySQL and
Percona Server 5.0-series versions, specify the LSN as
two 32-bit integers in high:low format. For databases
created in 5.1 and later, specify the LSN as a single
64-bit integer.
prepare增量備份與prepare全備稍有不同
• 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.
1.首先,在所有備份重演提交的事務,這會合並增量備份和全備
2.所有未提交事務回滾
恢復基礎備份(全備)這裡一定要加–redo-only參數,該參數意思是只應用xtrabackup日志中已經提交的事務數據,不回滾還未提交的數據
innobackupex --apply-log --redo-only /data/mysqldata/backup/base/
將增量備份incr應用到基礎備份base
innobackupex --apply-log --redo-only /data/mysqldata/backup/base/--incremental-dir=/data/mysqldata/backup/incr1
將增量備份incr2應用到基礎備份base,注意恢復最後一個增量備份時需要去掉–redo-only參數,回滾xtrabackup日志中那些還未提交的數據
innobackupex --apply-log /data/mysqldata/backup/base/--incremental-dir=/data/mysqldata/backup/incr2
Note: –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只在最後一個增備時不使用.如果在最後一個增倍時使用了–redo-only,備份仍是一致的,但在這種情況下,服務器將執行回滾階段
要注意的是,prepare應該按照備份的時間順序,即 全備-增備1-增備2-增備3…
如果順序錯誤,備份將不可用.如果你不知道正確的熟順序,可以查看xtrabackup_checkpoints文件
把所有合在一起的基礎備份整體進行一次apply操作,回滾未提交的數據
innobackupex --apply-log /data/mysqldata/backup/base/
這一步是可選的,如果你不做,MySQL會去回滾未提交的數據.同時,因為iblog文件不會被備份,這一步也會創建它們,如果不做這一步,MySQL啟動時也會創建它們.
innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --copy-back --rsync /data/mysqldata/backup/base
–rsync Uses the rsync utility to optimize local file transfers.
When this option is specified, innobackupex uses rsync to
copy all non-InnoDB files instead of spawning a separate
cp for each file, which can be much faster for servers
with a large number of databases or tables. This option
cannot be used together with –stream.