xtrabackup加密备份和还原
环境:
OS:Centos 7
mysql:5.7.30
IP:192.168.1.118
1.加密备份
/opt/xtrabackup247/bin/innobackupex --defaults-file=/opt/mysql5730/conf/my.cnf --user=root --socket=/tmp/mysql.sock --password=mysql -P3306 --encrypt=AES256 --encrypt-key=3c0efcea569021b49245e47b5d6a0e28 /opt/xtrbackup_file/
2.查看备份文件
[root@localhost 2022-04-14_17-10-11]# ls
backup-my.cnf.xbcrypt db_test mysql sys xtrabackup_checkpoints.xbcrypt
db_hxl ib_buffer_pool.xbcrypt performance_schema test xtrabackup_info.xbcrypt
db_hxl01 ibdata1.xbcrypt sbtest xtrabackup_binlog_info.xbcrypt xtrabackup_logfile.xbcrypt
加密方式备份的备份文件都是以xbcrypt结尾的
[root@localhost db_test]# ls
db.opt.xbcrypt tb_test_cp.frm.xbcrypt tb_test_cp.ibd.xbcrypt tb_test.frm.xbcrypt tb_test.ibd.xbcrypt
3.解压还原
首先需要进行解压,我这里是在备份的目录进行解压,也可以拷贝到新的目录解压
/opt/xtrabackup247/bin/xtrabackup --decompress --remove-original --decrypt=AES256 --encrypt-key=3c0efcea569021b49245e47b5d6a0e28 --target-dir=/opt/xtrbackup_file/2022-04-14_17-10-11
完成后可以看到相应的文件完成界面
[root@localhost 2022-04-14_17-10-11]# ls
backup-my.cnf db_hxl01 ib_buffer_pool mysql sbtest test xtrabackup_checkpoints xtrabackup_logfile
db_hxl db_test ibdata1 performance_schema sys xtrabackup_binlog_info xtrabackup_info
这里可以不用--decompress参数,因为我们备份的时候没有采用压缩的方式
--remove-original 目的是删除解密后加密的文件,不删除的话也可以进行下面的步骤恢复的
4.停掉数据库
[root@localhost]#/opt/mysql5730/bin/mysqladmin -h localhost -uroot -pmysql shutdown
创建空的数据目录
[root@localhost mysql5730]# cd /opt/mysql5730
[root@localhost mysql5730]# mv data bakdata
[root@localhost mysql5730]# mkdir data
5.应用
/opt/xtrabackup247/bin/innobackupex --defaults-file=/opt/mysql5730/conf/my.cnf --user=root --apply-log /opt/xtrbackup_file/2022-04-14_17-10-11
/opt/xtrabackup247/bin/innobackupex --defaults-file=/opt/mysql5730/conf/my.cnf --user=root --copy-back --rsync /opt/xtrbackup_file/2022-04-14_17-10-11
报错的话需要删除logfile
/opt/xtrabackup247/bin/innobackupex version 2.4.7 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 05f1fcf)
innobackupex: Can't create/write to file '/opt/mysql5730/redolog/ib_logfile0' (Errcode: 17 - File exists)
[01] error: cannot open the destination stream for ib_logfile0
[root@localhost ~]# cd /opt/mysql5730/redolog
[root@localhost redolog]# rm -rf *
6.修改权限
[root@dev-env23 mysql]#cd /opt
[root@dev-env23 mysql]#chown -R mysql:mysql ./mysql5730
7.启动数据库
/opt/mysql5730/bin/mysqld_safe --defaults-file=/opt/mysql5730/conf/my.cnf --user=mysql &
8.登陆数据库
/opt/mysql5730/bin/mysql -h localhost -uroot -p