1.生成密钥串
echo "123456" |md5sum
f447b20a7fcbf53a5d5be013ea0b15af - #注意后面的中划线不算
2.方式1-使用key
#备份
xtrabackup --user=mysqlbackup --password='Abc@12345678' --backup --compress --compress-threads=4 --encrypt=AES256 --encrypt-key="f447b20a7fcbf53a5d5be013ea0b15af" --target-dir=/data/backup/full > /data/backup/bak.log 2>&1
[root@servername backup]# ll /data/backup/full/
total 1524
-rw-r----- 1 root root 394 Jun 20 16:56 backup-my.cnf.zst.xbcrypt
-rw-r----- 1 root root 204 Jun 20 16:56 binlog.000003.zst.xbcrypt
-rw-r----- 1 root root 121 Jun 20 16:56 binlog.index.zst.xbcrypt
-rw-r----- 1 root root 730 Jun 20 16:56 ib_buffer_pool.zst.xbcrypt
-rw-r----- 1 root root 4052 Jun 20 16:56 ibdata1.zst.xbcrypt
drwxr-x--- 2 root root 4096 Jun 20 16:56 mysql
-rw-r----- 1 root root 1415399 Jun 20 16:56 mysql.ibd.zst.xbcrypt
drwxr-x--- 2 root root 8192 Jun 20 16:56 performance_schema
drwxr-x--- 2 root root 40 Jun 20 16:56 sys
-rw-r----- 1 root root 42823 Jun 20 16:56 undo_001.zst.xbcrypt
-rw-r----- 1 root root 40182 Jun 20 16:56 undo_002.zst.xbcrypt
drwxr-x--- 2 root root 34 Jun 20 16:56 wxbtest
-rw-r----- 1 root root 123 Jun 20 16:56 xtrabackup_binlog_info.zst.xbcrypt
-rw-r----- 1 root root 134 Jun 20 16:56 xtrabackup_checkpoints
-rw-r----- 1 root root 440 Jun 20 16:56 xtrabackup_info.zst.xbcrypt
-rw-r----- 1 root root 373 Jun 20 16:56 xtrabackup_logfile.zst.xbcrypt
-rw-r----- 1 root root 144 Jun 20 16:56 xtrabackup_tablespaces.zst.xbcrypt
#解密解压
xtrabackup --defaults-file=/etc/my.cnf --decompress --parallel=4 --decrypt=AES256 --encrypt-key="f447b20a7fcbf53a5d5be013ea0b15af" --target-dir=/data/backup/full --remove-original
注意:--remove-original选项在解密后删除加密文件
[root@servername backup]# ll /data/backup/full/
total 70708
-rw-r--r-- 1 root root 447 Jun 20 16:57 backup-my.cnf
-rw-r--r-- 1 root root 157 Jun 20 16:57 binlog.000003
-rw-r--r-- 1 root root 16 Jun 20 16:57 binlog.index
-rw-r--r-- 1 root root 6238 Jun 20 16:57 ib_buffer_pool
-rw-r--r-- 1 root root 12582912 Jun 20 16:57 ibdata1
drwxr-x--- 2 root root 143 Jun 20 16:57 mysql
-rw-r--r-- 1 root root 26214400 Jun 20 16:57 mysql.ibd
drwxr-x--- 2 root root 8192 Jun 20 16:57 performance_schema
drwxr-x--- 2 root root 28 Jun 20 16:57 sys
-rw-r--r-- 1 root root 16777216 Jun 20 16:57 undo_001
-rw-r--r-- 1 root root 16777216 Jun 20 16:57 undo_002
drwxr-x--- 2 root root 22 Jun 20 16:57 wxbtest
-rw-r--r-- 1 root root 18 Jun 20 16:57 xtrabackup_binlog_info
-rw-r----- 1 root root 134 Jun 20 16:56 xtrabackup_checkpoints
-rw-r--r-- 1 root root 566 Jun 20 16:57 xtrabackup_info
-rw-r--r-- 1 root root 2560 Jun 20 16:57 xtrabackup_logfile
-rw-r--r-- 1 root root 39 Jun 20 16:57 xtrabackup_tablespaces
#准备
xtrabackup --prepare --target-dir=/data/backup/full
[root@servername backup]# ll full/
total 115760
-rw-r--r-- 1 root root 447 Jun 20 16:57 backup-my.cnf
-rw-r--r-- 1 root root 157 Jun 20 16:57 binlog.000003
-rw-r--r-- 1 root root 16 Jun 20 16:57 binlog.index
-rw-r--r-- 1 root root 6238 Jun 20 16:57 ib_buffer_pool
-rw-r--r-- 1 root root 12582912 Jun 20 16:58 ibdata1
-rw-r----- 1 root root 12582912 Jun 20 16:58 ibtmp1
drwxr-x--- 2 root root 6 Jun 20 16:58 '#innodb_redo'
drwxr-x--- 2 root root 143 Jun 20 16:57 mysql
-rw-r--r-- 1 root root 26214400 Jun 20 16:57 mysql.ibd
drwxr-x--- 2 root root 8192 Jun 20 16:57 performance_schema
drwxr-x--- 2 root root 28 Jun 20 16:57 sys
-rw-r--r-- 1 root root 16777216 Jun 20 16:57 undo_001
-rw-r--r-- 1 root root 16777216 Jun 20 16:57 undo_002
drwxr-x--- 2 root root 22 Jun 20 16:57 wxbtest
-rw-r--r-- 1 root root 18 Jun 20 16:57 xtrabackup_binlog_info
-rw-r----- 1 root root 134 Jun 20 16:58 xtrabackup_checkpoints
-rw-r--r-- 1 root root 566 Jun 20 16:57 xtrabackup_info
-rw-r----- 1 root root 33554432 Jun 20 16:58 xtrabackup_logfile
-rw-r--r-- 1 root root 39 Jun 20 16:58 xtrabackup_tablespaces
#恢复
[root@servername backup]# systemctl stop mysqld
[root@servername backup]# rm -rf /var/lib/mysql/*
xtrabackup --host=127.0.0.1 --user=mysqlbackup --password='Abc@12345678' --port=3306 --datadir=/var/lib/mysql --copy-back --target-dir=/data/backup/full
[root@servername backup]# chown -R mysql.mysql /var/lib/mysql
[root@servername backup]# systemctl start mysqld