三、XtraBackup流备份及压缩

(1)、流备份使用tar.gz压缩

[root@localhost backup]# innobackupex-1.5.1 --user=root --password=xxxxx --stream=tar /mysql/ | gzip ->/backup/b.tar.gz

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona LLC and/or its affiliates 2009-2013.  All Rights Reserved.

This software is published under
the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.

Get the latest version of Percona XtraBackup, documentation, and help resources:
http://www.percona.com/xb/p

140227 16:31:07  innobackupex-1.5.1: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup' as 'root'  (using password: YES).
140227 16:31:07  innobackupex-1.5.1: Connected to MySQL server
140227 16:31:07  innobackupex-1.5.1: Executing a version check against the server...
140227 16:31:07  innobackupex-1.5.1: Done.
......
......
......
......
innobackupex-1.5.1: Backup created in directory '/mysql'
innobackupex-1.5.1: MySQL binlog position: filename 'mysql-bin.000033', position 120
140227 16:31:16  innobackupex-1.5.1: Connection to database server closed
innobackupex-1.5.1: You must use -i (--ignore-zeros) option for extraction of the tar stream.
140227 16:31:16  innobackupex-1.5.1: completed OK!

在/backup目录下会生成备份压缩文件
[root@localhost backup]# pwd
/backup
[root@localhost backup]# ls
b.tar.gz  

如果需要在备份完发送到远程服务器需要配置SSH信任关系,实现在备份完成自动传送到远程服务器,例子如下?

SSH配置信任关系省略

[root@localhost backup]# innobackupex-1.5.1 --user=root --password=xxxxx --stream=tar /mysql | gzip -> /backup/b.tar.gz | scp /backup/b.tar.gz 192.168.xx.xx:/home/mysql

如果使用bzip2压缩也很简单例子如下:

[root@localhost backup]# innobackupex-1.5.1 --user=root --password=xxxxx --stream=tar /mysql/ | bzip2 ->/backup/b.tar.bz2

解压缩备份文件

[root@localhost backup]# tar ixzvf b.tar.gz      --注意:解压时必须使用-i参数 
./backup-my.cnf
ibdata1
test/checksums.ibd
test/checksum.ibd
percona/checksums.ibd
percona/t.ibd
mysql/slave_worker_info.ibd
mysql/slave_master_info.ibd
........
........
........

(2)流备份的另一种格式xbstream

在使用流备份的时候支持两种格式一个是tar,另外一个就是xbstream.下面我们就开始实验一下吧.

[root@localhost backup]# innobackupex-1.5.1 --user=root --password=xxxxx --stream=xbstream /mysql> /backup/b.xbstream

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona LLC and/or its affiliates 2009-2013.  All Rights Reserved.

This software is published under
the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.

Get the latest version of Percona XtraBackup, documentation, and help resources:
http://www.percona.com/xb/p

140227 16:54:38  innobackupex: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup' as 'root'  (using password: YES).
140227 16:54:38  innobackupex: Connected to MySQL server
140227 16:54:38  innobackupex: Executing a version check against the server...
140227 16:54:38  innobackupex: Done.
.........
.........
.........
innobackupex: Backup created in directory '/mysql'
innobackupex: MySQL binlog position: filename 'mysql-bin.000033', position 120
140227 16:54:47  innobackupex: Connection to database server closed
140227 16:54:47  innobackupex: completed OK!

查看产生的备份文件,解压备份文件.
[root@localhost backup]# ls
b.xbstream
[root@localhost backup]# xbstream -x < b.xbstream 
[root@localhost backup]# ls
backup-my.cnf ibdata1  mysql  percona  performance_schema  test ...... ...... 

使用多线程备份并行压缩加快备份速度
[root@localhost backup]# innobackupex-1.5.1 --user=root --password=xxxxx --compress --compress-threads=2 --parallel=4 --stream=xbstream /mysql >/backup/b.xbstream

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona LLC and/or its affiliates 2009-2013.  All Rights Reserved.

This software is published under
the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.

Get the latest version of Percona XtraBackup, documentation, and help resources:
http://www.percona.com/xb/p

140227 17:05:50  innobackupex-1.5.1: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup' as 'root'  (using password: YES).
140227 17:05:50  innobackupex-1.5.1: Connected to MySQL server
140227 17:05:50  innobackupex-1.5.1: Executing a version check against the server...
140227 17:05:50  innobackupex-1.5.1: Done.
..........
..........
..........
..........
innobackupex-1.5.1: Backup created in directory '/mysql'
innobackupex-1.5.1: MySQL binlog position: filename 'mysql-bin.000033', position 120
140227 17:05:56  innobackupex-1.5.1: Connection to database server closed
140227 17:05:56  innobackupex-1.5.1: completed OK!

对比一下压缩与未压缩的文件对比(差距不小哦^_^)

[root@localhost backup]# du -sh *
79M     a.xbstream
17M     b.xbstream

^_^压缩比率还是不错的吧

如果需要在备份完发送到远程服务器参考tar.gz远程发送备份的例子.

(3)、流的增量备份

创建0级备份

innobackupex-1.5.1 --user=root --password=xxxxx --parallel=2 --stream=xbstream /mysql/ > /backup/zero.xbstream

基于0级备份,创建1级备份

innobackupex-1.5.1 --user=root --password=xxxxx --parallel=2 --incremental --incremental-lsn=183752829 --stream=xbstream /mysql/ > /backup/one.xbstream

注意参数--incremental-lsn需要查看0级备份的checkpoint信息

例子:
[root@localhost backup]# cat xtrabackup_checkpoints 
backup_type = full-backuped
from_lsn = 0
to_lsn = 183752829
last_lsn = 183752829
compact = 0

文档中用到的参数英文原文解释如下:

 --compress
        This option instructs xtrabackup to compress backup copies of InnoDB
        data files. It is passed directly to the xtrabackup child process.
        Try 'xtrabackup --help' for more details.

--compress-threads
        This option specifies the number of worker threads that will be used
        for parallel compression. It is passed directly to the xtrabackup
        child process. Try 'xtrabackup --help' for more details.

--incremental
        This option tells xtrabackup to create an incremental backup, rather
        than a full one. It is passed to the xtrabackup child process. When
        this option is specified, either --incremental-lsn or
        --incremental-basedir can also be given. If neither option is given,
        option --incremental-basedir is passed to xtrabackup by default, set
        to the first timestamped backup directory in the backup base
        directory.

--incremental-lsn
        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.

--parallel=NUMBER-OF-THREADS
        On backup, this option specifies the number of threads the
        xtrabackup child process should use to back up files concurrently.
        The option accepts an integer argument. It is passed directly to
        xtrabackup's --parallel option. See the xtrabackup documentation for
        details.
        On --decrypt or --decompress it specifies the number of parallel
        forks that should be used to process the backup files.

--stream=STREAMNAME
        This option specifies the format in which to do the streamed backup.
        The option accepts a string argument. The backup will be done to
        STDOUT in the specified format. Currently, the only supported
        formats are tar and xbstream. This option is passed directly to
        xtrabackup's --stream option.

关于如何从增量备份中恢复数据,请参考"详解Percona的XtraBackup备份工具(上篇)"

我在使用xtrabackup version 2.1.6的时候尝试使用流备份格式tar报错,看来tar格式还是不支持增量备份.希望在下个版本能够支持吧