暂无图片
暂无图片
5
暂无图片
暂无图片
暂无图片

xtrabackup压缩备份和恢复

原创 徐佩怡 2021-03-01
9452

xtrabackup压缩备份和恢复

xtrabackup是Percona公司的一款用于MySQL在线备份的工具,支持在线备份,全量备份和增量备份,单库单表备份。
本文侧重于介绍Xtrabackup用于MySQL5.6和MYSQl5.7版本的压缩备份和恢复。

压缩本分的好处使用xbstream方式压缩备份,节约空间,也无需再使用一次tar命令进行压缩。
基于实际的环境,我们测试讲100G数据备份并传到对应的备份服务器存储,采取以下方案:
1.不压缩的普通备份方式需要17空间,存储备份文件并传输。
2.用tar的方式压缩,发现并不能达到效果,压缩后的文件仍然是17G。
3.使用xtrabackup的compress选项进行压缩,压缩后的文件为4.9G。
4.使用trabackup的compress选项进行压缩并使用tar.gz进行压缩,压缩后文件为3.8G。
下方表格是测试的耗时和压缩文件大小的对比图:
截屏20210228 下午10.50.36.png

在开始介绍之前,先列出普通使用xtrabackup命令进行备份恢复方式:

Xtrabackup的(非压缩)备份和恢复的方式:

MySQL 5.6 & 5.7

**备份:**
innobackupex --defaults-file=/etc/my.cnf  --user=root --password=123456  /path/to/BACKUP-DIR/
**恢复:**
innobackupex --apply-log /backups/2020-02-27_11-04-55/
innobackupex --defaults-file=/etc/my.cnf --copy-back   /backups/2020-02-27_11-04-55/
$ chown -R mysql:mysql /path/the/datadir

MySQL8.0

**备份**
xtrabackup --backup --target-dir=/data/backups/ --user=root --password=123456 
**恢复**
xtrabackup --prepare --target-dir=/data/backups/
xtrabackup --copy-back --target-dir = /data/backups /
$ chown -R mysql:mysql /path/the/datadir

压缩备份的备份和恢复:

MySQL 5.6和MySQL 5.7压缩备份:
innobackupex --defaults-file=/etc/my5.7-6670.cnf --compress --compress-threads=4 --user=root /tmp/2020-02-28-11-05
–compress: 参数表示使用压缩
–compress-threads:表示使用几个线程压缩

MySQL 5.6和MySQL 5.7解压恢复:

mkdir /tmp/backup_2020-02-28
innobackupex  --defaults-file=/etc/my.cnf  --decompress --user=root /tmp/2020-02-28-11-05/2021-02-28_11-05-01/
innobackupex  --apply-log /tmp/2020-02-28-11-05/2021-02-28_11-05-01/
innobackupex --defaults-file=/etc/my.cnf --copy-back   /tmp/2020-02-28-11-05/2021-02-28_11-05-01/
chown -R mysql:mysql /path/the/datadir

压缩备份的目录变化:

1.目录文件对比,不同的目录如下:

-rw-r----- 1 root root     479 2月  28 22:30 backup-my.cnf.qp
-rw-r----- 1 root root     472 2月  28 22:30 ib_buffer_pool.qp
-rw-r----- 1 root root 1155974 2月  28 22:30 ibdata1.qp
-rw-r----- 1 root root     153 2月  28 22:30 xtrabackup_binlog_info.qp
-rw-r----- 1 root root     135 2月  28 22:30 xtrabackup_checkpoints
-rw-r----- 1 root root     563 2月  28 22:30 xtrabackup_info.qp
-rw-r----- 1 root root     491 2月  28 22:30 xtrabackup_logfile.qp

这些文件在压缩备份的时候都会以qp作为压缩文件的后缀,表示是经过压缩的文件。
xtrabackup在压缩过程中,将使用指定的压缩算法,压缩所有输出数据,除了元数据和非InnoDB的文件。目前仅仅支持quicklz算法。结果文件是qp压缩格式,即每个 .qp的文件。使用xbstream作为流选项,可以并行复制和压缩备份,这可以显着加快备份过程。

2.在执行innobackupex --defaults-file=/etc/my5.7-6670.cnf --decompress --user=root /tmp/2020-02-28-11-05/2021-02-28_11-05-01/时候的输出部分截图,表示正在对压缩内容进行解压:
截屏20210228 下午11.56.22.png

3.解压(innobackupex —defaults-file=/etc/my5.7-6670.cnf —decompress —user=root /tmp/2020-02-28-11-05/2021-02-28_11-05-01/)之后,文件中包含.qp和解压后的文件:

-rw-r--r-- 1 root root      490 2月  28 23:47 backup-my.cnf
-rw-r----- 1 root root      479 2月  28 22:30 backup-my.cnf.qp
-rw-r--r-- 1 root root      546 2月  28 23:47 ib_buffer_pool
-rw-r----- 1 root root      472 2月  28 22:30 ib_buffer_pool.qp
-rw-r--r-- 1 root root 79691776 2月  28 23:47 ibdata1
-rw-r----- 1 root root  1155974 2月  28 22:30 ibdata1.qp
-rw-r--r-- 1 root root       63 2月  28 23:47 xtrabackup_binlog_info
-rw-r----- 1 root root      153 2月  28 22:30 xtrabackup_binlog_info.qp
-rw-r----- 1 root root      135 2月  28 22:30 xtrabackup_checkpoints
-rw-r--r-- 1 root root      589 2月  28 23:47 xtrabackup_info
-rw-r----- 1 root root      563 2月  28 22:30 xtrabackup_info.qp
-rw-r--r-- 1 root root     2560 2月  28 23:47 xtrabackup_logfile
-rw-r----- 1 root root      491 2月  28 22:30 xtrabackup_logfile.qp

可能的报错

1.安装xtrabackup时缺失依赖包:

error: Failed dependencies:
        perl(DBD::mysql) is needed by percona-xtrabackup-2.4.19-1.el7.x86_64
        perl(Digest::MD5) is needed by percona-xtrabackup-2.4.19-1.el7.x86_64

解决方案:
yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL
yum -y install rsync perl l perl-Digest-MD5

2.安装toolkit时缺失依赖包:

perl(IO::Socket::SSL) is needed by percona-toolkit-3.1.0-2.el7.x86_64 
perl(Term::ReadKey) is neede by percona-toolkit-3.1.0-2.el7.x86_64 

解决方案:
yum install perl-IO-Socket-SSL.noarch
yum install perl-TermReadKey.x86_64

3.使用–stream=tar选项备份时收到如下提示,即只支持xbstream:

Using server version 5.7.31-log
innobackupex version 2.4.19 based on MySQL server 5.7.26 Linux (x86_64) (revision id: c2d69da)
xtrabackup: error: compressed and encrypted backups are incompatible with the
'tar' streaming format. Use --stream=xbstream instead.

解决方案:
使用–stream=xbstream取代–stream=tar,在测试版本innobackupex version 2.4.19 中,默认也是xbstream,也可不用特意写出来。

4.decompress报错:

210228 23:35:27 [01] decompressing ./2021-02-28_22-30-00/xtrabackup_logfile.qp
sh: qpress: command not found
cat: write error: Broken pipe
Error: decrypt and decompress thread 0 failed.

我参考了
Compressed Backup
Percona-Toolkit系列(1)—安装_Expec-乐的博客-CSDN博客

解决方案:
配置Percona YUM源
yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
执行安装命令:

percona-release enable tools
yum install update
yum install qpress

percona-release enable tools的命令:
enable表示打开Percona某个产品的存储库
存储库分为:
original:存储库是获取Percona以前版本的产品的地方,这些产品还没有到达生命的尽头,但是仍然受到支持,比如MySQL 5.6和5.7的Percona Server、Percona XtraDB Cluster 5.6和5.7、MongoDB 3.4和3.6的Percona服务器等等。
ps-80:MySQL 8.0的Percona Server
pxc-80:Percona XtraDB Cluster 8.0
psmdb-40:MongoDB 4.0的Percona Server
tools:托管所有其他产品和工具:Percona XtraBackup 8.0、Percona Toolkit、PMM客户端、ProxySQL等。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论