创建备份
# 使用 `xtrabackup --backup` 执行备份
./xtrabackup --backup --host=127.0.0.1 --user=root --password=123456 --target-dir=/root/xtrabackup/bin/bk_20220418
如果不指定备份目录[--target-dir],则备份目录默认为
xtrabackup
命令所在目录下的xtrabackup_backupfiles文件夹中。
如果指定备份目录(绝对路径和相对路径都可以),则其父目录必须存在--因为内部使用mkdir命令创建,备份目录下可以包含其它的文件等内容(建议不包含内容),但不能包含与备份相关的内容--覆盖已存在的文件时将抛出系统错误。
从如下日志可以看出很多信息,如数据库存储数据的目录/usr/local/mysql/data/
;备份目录/root/xtrabackup/bin/bk_20220418/
;完成标志completed OK!
等
...
Using server version 5.7.33
./xtrabackup version 2.4.21 based on MySQL server 5.7.32 Linux (x86_64) (revision id: 5988af5)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /usr/local/mysql/data/
...
220419 00:12:31 Backup created in directory '/root/xtrabackup/bin/bk_20220418/'
...
xtrabackup: Transaction log of lsn (2752428) to (2752437) was copied.
220419 00:12:31 completed OK!
准备备份
作用:使文件在同一时刻完全一致。
Data files are not point-in-time consistent until they’ve been prepared, because they were copied at different times as the program ran, and they might have been changed while this was happening.
注意:如果目标目录[--target-dir]已经做过准备备份,可以忽略本步骤。
# `xtrabackup --prepare` 执行准备。
# 如果要将本次备份的数据作为后续增量备份的基础,应该使用 `xtrabackup –apply-log-only`(见增量备份步骤)
./xtrabackup --prepare --target-dir=/root/xtrabackup/bin/bk_20220418/
执行日志
...
xtrabackup: recognized client arguments: --prepare=1 --target-dir=/root/xtrabackup/bin/bk_20220418/
./xtrabackup version 2.4.21 based on MySQL server 5.7.32 Linux (x86_64) (revision id: 5988af5)
xtrabackup: cd to /root/xtrabackup/bin/bk_20220418/
...
InnoDB: Shutdown completed; log sequence number 2752552
220419 00:54:49 completed OK!
恢复备份
警告
1、在恢复备份时需要先执行准备备份
2、在执行备份恢复时,需要先停止数据库服务,在执行完备份恢复并进行授权后再启动数据库服务
# xtrabackup --copy-back 恢复备份 --datadir目录需为空
./xtrabackup --copy-back --target-dir=/root/xtrabackup/bin/bk_20220418 --datadir=/usr/local/mysql/data
# 还需要对数据目录进行授权
cd /usr/local/mysql/data
chown -R mysql:mysql data
执行日志
xtrabackup: recognized client arguments: --copy-back=1 --target-dir=/root/xtrabackup/bin/bk_20220418
./xtrabackup version 2.4.21 based on MySQL server 5.7.32 Linux (x86_64) (revision id: 5988af5)
220419 01:10:30 [01] Copying ib_logfile0 to /usr/local/mysql/data/ib_logfile0
220419 01:10:30 [01] ...done
220419 01:10:30 [01] Copying ib_logfile1 to /usr/local/mysql/data/ib_logfile1
...
220419 01:10:31 [01] Copying ./ibtmp1 to /usr/local/mysql/data/ibtmp1
220419 01:10:31 [01] ...done
220419 01:10:31 completed OK!
参考: https://www.percona.com/doc/percona-xtrabackup/2.4/backup_scenarios/full_backup.html