Percona最近发布了xtrabackup-2.1.0-alphal版本,可以支持基于AES256算法的数据加密,对于容灾存储不可信的场景来说,真是福音啊,可以不用自己写脚本实现了:)
可以从这里下载。下面是具体的使用过程:
1. 先进行备份
1 2 3 4 5 6 |
innobackupex --defaults-file=/home/mysql/mysql/etc/my.cnf \ --ibbackup=xtrabackup --user=mysqlbackup --password='********' \ --host=127.0.0.1 --tmpdir=/tmp --compact --compress \ --compress-threads=5 --encrypt=AES256 \ --encrypt-key=3c0efcea569021b49245e47b5d6a0e28 \ --encrypt-threads=5 --no-timestamp --parallel=2 backup_data |
看到下面的输出就说明OK了。
1 |
innobackupex: completed OK! |
几个关键参数说明如下:
1 2 3 4 5 6 7 8 9 10 11 |
--compact # 备份数据不保存二级索引页(secondary index),apply-log的时候需要对应加上rebuild-indexes进行重建 # 压缩 --compress # 开启压缩,目前只支持quicklz算法 --compress-threads=5 # 并发压缩线程,默认为1 --compress-chunk-size=64K # 每个压缩线程使用的buffer,默认64K # 加密 --encrypt=AES256 # 开启加密,目前支持的算法有AES128, AES192 和 AES256 --encrypt-key=3c0efcea569021b49245e47b5d6a0e28 # 32位密钥,不过不推荐这么使用,最好将密钥存放在文件中,用encrypt-key-file参数引用 --encrypt-threads=5 # 加密线程数,默认为1 |
2. 恢复。恢复过程相比直接备份会麻烦一点,需要先解密,再解压,才能进行apply-log
1) 解密,需要使用和加密时相同的key
1 2 3 4 |
for i in `find . -iname "*\.xbcrypt"`; do xbcrypt -d --encrypt-key=3c0efcea569021b49245e47b5d6a0e28 \ --encrypt-algo=AES256 < $i > $(dirname $i)/$(basename $i .xbcrypt) && rm $i done |
2) 解压缩。xtrabackup目前没有buildin的解压缩方式,需要使用第三方解压工具qpress
1 |
for i in `find . -iname "*\.qp"`; do qpress -d $i $(dirname $i) && rm $i; done |
qpress可以支持多核,可以有效的提升解压效率。解压时加上-Tn,默认为2.
3) 接下来就是正常的apply-log了,记得加上–rebuild-indexes
1 2 3 |
innobackupex --defaults-file=/home/mysql/mysql/etc/my.cnf \ --ibbackup=xtrabackup --apply-log \ --rebuild-indexes backup_data |
最后:压缩效率还是挺高的,实测结果使用压缩后的备份比正常压缩可以节省70%的空间,当然这个和具体的场景有关,各位可以试试自己的场景:)