在前篇看了Xtrabackup命令的使用。
MariaDB 使用 xtrabackup 工具进行备份与恢复
http://www.cndba.cn/dave/article/2652
这里篇看一下xtrabackup的其他功能和选项。更多详细信息请直接参考命令的帮助:
[root@www.cndba.cn/dave backup]# innobackupex --help
1 并行备份
xtrabackup支持并行备份,在备份时可以同时开启多个线程,并行的进行备份操作,默认情况下只开启一个进程进行备份。
--parallel=# 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.
使用—parallel选项指定并行备份的线程数量,示例如下:
[root@www.cndba.cn/dave backup]# innobackupex --user=root --password='' --parallel=8 /tmp/backup
2 节流备份
xtrabackup支持节流备份,就是节省IO操作的备份,当数据库服务器上已经没有过多的空闲IO时,我们可以使用节流备份。
--throttle=# This option specifies a number of I/O operations (pairs
of read+write) per second. It accepts an integer
argument. It is passed directly to xtrabackup's
--throttle option.
使用—throttle选项限制每秒钟操作IO的次数,示例如下:
[root@www.cndba.cn/dave backup]# innobackupex --user=root --password='' --throttle=50 /tmp/backup
注意:
—throttle选项只适用于备份阶段,不能用于prepare阶段与copy-back阶段。
3 压缩备份
xtrabackup支持压缩功能,在备份时可以直接生成经过压缩的备份。有两种实现方法:
- 1) 使用—compress选项进行压缩。
- 2) 使用流的方式进行备份,将备份的tar格式的流重定向到其他压缩软件进行压缩。
在使用xtrabackup进行备份时,使用—compress选项可以对备份数据进行压缩,所有备份数据都会被压缩,包括事务日志和元数据文件。
--compress[=name] 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.
--compress-chunk-size=#
Size of working buffer(s) for compression threads in
bytes. The default value is 64K.
使用—compress选项对备份进行压缩时,使用的压缩算法为”quicklz”,压缩出的文件的后缀名为”.qp”。”quicklz”是一个压缩库,官网如下:http://www.quicklz.com/
[root@www.cndba.cn/dave backup]# innobackupex --user=root --password='' --compress /tmp/backup
[root@www.cndba.cn/dave backup]# cd 2018-02-09_02-07-14/
[root@www.cndba.cn/dave 2018-02-09_02-07-14]# ll
total 452
-rw-r----- 1 root root 414 Feb 9 02:07 backup-my.cnf.qp
drwxr-x--- 2 root root 4096 Feb 9 02:07 cndba
-rw-r----- 1 root root 1324 Feb 9 02:07 ib_buffer_pool.qp
-rw-r----- 1 root root 420467 Feb 9 02:07 ibdata1.qp
drwxr-x--- 2 root root 4096 Feb 9 02:07 mysql
drwxr-x--- 2 root root 4096 Feb 9 02:07 performance_schema
drwxr-x--- 2 root root 4096 Feb 9 02:07 world
-rw-r----- 1 root root 109 Feb 9 02:07 xtrabackup_binlog_info.qp
-rw-r----- 1 root root 113 Feb 9 02:07 xtrabackup_checkpoints
-rw-r----- 1 root root 511 Feb 9 02:07 xtrabackup_info.qp
-rw-r----- 1 root root 297 Feb 9 02:07 xtrabackup_logfile.qp
[root@www.cndba.cn/dave 2018-02-09_02-07-14]#
[root@www.cndba.cn/dave 2018-02-09_02-07-14]# cd cndba
[root@www.cndba.cn/dave cndba]# ll
total 28
-rw-r----- 1 root root 254 Feb 9 02:07 dave.frm.qp
-rw-r----- 1 root root 1801 Feb 9 02:07 dave.ibd.qp
-rw-r----- 1 root root 127 Feb 9 02:07 db.opt.qp
-rw-r----- 1 root root 256 Feb 9 02:07 oracle.frm.qp
-rw-r----- 1 root root 1811 Feb 9 02:07 oracle.ibd.qp
-rw-r----- 1 root root 256 Feb 9 02:07 zhixin.frm.qp
-rw-r----- 1 root root 1806 Feb 9 02:07 zhixin.ibd.qp
[root@www.cndba.cn/dave cndba]#
在对数据进行压缩的同时,还可以使用—compress-threads=#选项可以指定压缩线程的数量,加快压缩的速度,示例如下
[root@www.cndba.cn/dave backup]# innobackupex --user=root --password='' –parallel=8 --compress --compress-threads=8 /tmp/backup
备份经过压缩以后,在还原备份数据之前,则需要先进行解压操作。可以使用—decompress选项对压缩备份的目录进行解压操作,不过,即使是使用—decompress选项进行解压操作,也仍然需要安装qpress,所以,如果在压缩备份时使用了—compress选项,那么解压备份的前提就是安装了qpress。使用—decompress选项时,可以配合—parallel选项,加速解压操作的进度。
使用示例如下
[root@www.cndba.cn/dave backup]# innobackupex --user=root --password='' --parallel=8 --decompress /tmp/backup/2018-02-09_02-18-30
180209 02:21:46 innobackupex: Starting the decrypt and decompress operation
IMPORTANT: Please check that the decrypt and decompress run completes successfully.
At the end of a successful decrypt and decompress run innobackupex
prints "completed OK!".
innobackupex version 2.4.9 based on MySQL server 5.7.13 Linux (x86_64) (revision id: a467167cdd4)
180209 02:21:46 [01] decompressing ./performance_schema/db.opt.qp
180209 02:21:46 [07] decompressing ./mysql/func.MYD.qp
180209 02:21:46 [03] decompressing ./ibdata1.qp
180209 02:21:46 [08] decompressing ./mysql/host.MYI.qp
180209 02:21:46 [02] decompressing ./mysql/proxies_priv.frm.qp
sh: qpress: command not found
sh: qpress: command not found
cat: write error: Broken pipe
cat: write error: Broken pipe
总之,如果在备份时使用了—compress选项,那么需要先解压才能进行apply-log与copy-back操作,解压时必须安装了qpress。
4 流备份
xtrabackup支持流式备份,即将备份数据以数据流的方式输出。使用—stream选项则可以实现流备份,xtrabackup支持两种格式的流,tar格式的流与xbstream格式的流,也就是说,—stream选项有两个可用值,tar或xbstream
4.1 tar格式流备份
使用如下命令,会将tar格式的流当做标准输出输出到屏幕上,虽然我们指定了/backup目录,但是并不能在对应目录下生成备份文件。
innobackupex --user=root --password='' --stream=tar /tmp/backup
所以,需要将上述tar格式的流进行重定向,使用如下命令进行tar格式的流备份。
innobackupex --user=root --password='' --stream=tar /tmp/backup > /tmp/backup/alldb.tar
上述命令表示将tar格式的流数据备份到/tmp/backup/alldb.tar中,上述命令中—stream=tar后面的路径不可省,但是其路径可以是任意路径,与备份的tar包所在的路径没有任何关系,但是此路径不能省略,省略就会报错。
注意:官方文档中声明,在解压tar格式的流备份时,需要使用tar命令的-i选项,示例如下
[root@www.cndba.cn/dave backup]# tar -ixvf /tmp/backup/alldb.tar
我们也可以在生成流备份文件时,在进行压缩,示例如下
innobackupex --user=root --password='' --stream=tar /tmp/backup | gzip > /tmp/backup/alldb.tar.gz
上例表示将tar格式的留备份进行gzip压缩,可以将gzip换成其他常用的压缩软件进行压缩。使用上述命令已经体现出了流备份的优点,不用先生成备份,再通特定的压缩软件对备份进行压缩,一步就完成了备份归档压缩的步骤。
4.2 xbstream格式流备份
只要将—stream选项的流格式设置为xbstream即可,示例如下
innobackupex --user=root --password='' --stream= xbstream /tmp/backup > /tmp/backup/alldb.xbstream
我们将xbstream格式的数据流重定向到了/tmp/backup/alldb.xbstream文件中。
xbstream流备份也可以在备份时直接进行压缩,只不过,xbstream格式的流是使用—compress选项进行压缩,—compress选项上文已经总结过,此处不再赘述。
innobackupex --user=root --password='' --stream=xbstream --compress /tmp/ > /tmp/backup/alldb.xbstream
在使用这些备份文件恢复数据时,需要使用xbstream命令释放xbstream格式的流备份文件,在安装xtrabackup时就已经安装了xbstream命令,使用示例如下。
[root@www.cndba.cn/dave backup]# xbstream -x < alldb.xbstream
上述命令表示将当前目录中的bakup.xbstream备份文件通过xbstream命令进行释放,-x选项表示释放xbstream流备份文件到当前目录,如果想要释放xbstream流备份文件到指定的目录,可以使用-C选项,示例如下
[root@www.cndba.cn/dave backup]# mkdir /tmp/dave
[root@www.cndba.cn/dave backup]# xbstream -x -C /tmp/dave < alldb.xbstream
[root@www.cndba.cn/dave ~]# cd /tmp/dave
[root@www.cndba.cn/dave dave]# ll
total 452
-rw-r----- 1 root root 414 Feb 9 03:19 backup-my.cnf.qp
drwxr-x--- 2 root root 4096 Feb 9 03:19 cndba
-rw-r----- 1 root root 1324 Feb 9 03:19 ib_buffer_pool.qp
-rw-r----- 1 root root 420467 Feb 9 03:19 ibdata1.qp
drwxr-x--- 2 root root 4096 Feb 9 03:19 mysql
drwxr-x--- 2 root root 4096 Feb 9 03:19 performance_schema
drwxr-x--- 2 root root 4096 Feb 9 03:19 world
-rw-r----- 1 root root 109 Feb 9 03:19 xtrabackup_binlog_info.qp
-rw-r----- 1 root root 113 Feb 9 03:19 xtrabackup_checkpoints
-rw-r----- 1 root root 527 Feb 9 03:19 xtrabackup_info.qp
-rw-r----- 1 root root 297 Feb 9 03:19 xtrabackup_logfile.qp
[root@www.cndba.cn/dave dave]#
如果在生成xbstream格式的流备份时使用了—compress选项进行了压缩,那么释放后的xbstream流备份为qpress格式的压缩文件,还需要使用qpress进行解压,具体方法在上文中已经描述过,此处不再赘述。
版权声明:本文为博主原创文章,未经博主允许不得转载。