MySQL物理备份-PXB原理及用法
关于PXB
PXB工作原理
还原备份
PXB安装
PXB支持多种操作系统,有多种安装方式,这里以CentOS 7环境下载安装为例:
wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.4/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm
yum localinstall percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm
使用PXB,用户需要的最小权限:
GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'bkpuser'@'localhost';
PXB使用场景
PXB支持全备、增量备份、压缩备份、加密备份等。
使用PXB备份恢复大概包含三个步骤,创建备份、准备备份、恢复备份。
以全备为例
创建备份,文件夹不为空会报错,没有会自动创建。
xtrabackup --backup --target-dir=/data/backups/
准备备份,版本大于等于创建时的版本,不要中断。
xtrabackup --prepare --target-dir=/data/backups/
恢复备份,复制回恢复目录,记得修改owner
xtrabackup --copy-back --target-dir=/data/backups/
备份流程
根据备份日志及general日志,可以看到详细的备份流程。
备份流程(不支持备份锁)
启动,进行版本检查,连接MySQL,读取目录
启动一个后台进程,扫描复制redo log,持续进行
开始复制innodb表和数据文件
完成innodb表和数据文件复制,加锁FTWRL,阻塞所有修改操作
FLUSH NO_WRITE_TO_BINLOG TABLES
FLUSH TABLES WITH READ LOCK
开始复制非innodb表和文件
完成上一步,获取点位show master status
释放锁
FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS
UNLOCK TABLES
复制ib_buffer_pool,打印点位,写backup-my.cnf及lsn
最后completed OK!
备份流程(支持备份锁,比如PerconaServer for MySQL5.6+)
总体步骤同上,不同之处在于4-7步
启动,进行版本检查,连接MySQL,读取目录
启动一个后台进程,扫描复制redo log,持续进行
开始复制innodb表和数据文件
完成innodb表和数据文件复制,加备份表锁,不阻塞InnoDB表DML
LOCK TABLES FOR BACKUP
开始复制非innodb表和文件
完成上一步,加binlog锁,LOCK BINLOG FOR BACKUP,获取点位show master status
释放锁
FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS
UNLOCK BINLOG
UNLOCK TABLES
复制ib_buffer_pool,打印点位,写backup-my.cnf及lsn
最后completed OK!
关于Percona的备份锁
LOCK TABLES FOR BACKUP
锁住非InnoDB表修改和所有DDL,InnoDB表可以DML。
LOCK BINLOG FOR BACKUP
锁住binlog日志写入,阻止show master/slave status时点位或gtid改变,事务不提交可以执行DML。
FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS
将缓存中的redo日志刷回磁盘,以保证能拷贝到。
UNLOCK BINLOG
释放binlog锁。
UNLOCK TABLES
释放表锁。
附:备份日志示例
[root@db-xxx ~]# innobackupex --defaults-file=/root/my3305.cnf --socket=/data/mysql_3305/mysql.sock /data/data_bak
xtrabackup: recognized server arguments: --datadir=/data/mysql_3305 --server-id=111148 --log_bin=mysql-bin --tmpdir=/data/mysqltmpdir --innodb_buffer_pool_size=8G --innodb_file_per_table=1 --innodb_flush_log_at_trx_commit=1 --innodb_log_buffer_size=128M --innodb_log_file_size=512M --innodb_log_files_in_group=3 --innodb_flush_method=O_DIRECT --innodb_open_files=32768 --innodb_read_io_threads=8 --innodb_write_io_threads=8 --innodb_io_capacity=2000
xtrabackup: recognized client arguments:
220407 17:00:28 innobackupex: Starting the backup operation
IMPORTANT: Please check that the backup run completes successfully.
At the end of a successful backup run innobackupex
prints "completed OK!".
220407 17:00:28 version_check Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup;mysql_socket=/data/mysql_3305/mysql.sock' (using password: NO).
220407 17:00:28 version_check Connected to MySQL server
220407 17:00:28 version_check Executing a version check against the server...
220407 17:00:28 version_check Done.
220407 17:00:28 Connecting to MySQL server host: localhost, user: not set, password: not set, port: not set, socket: /data/mysql_3305/mysql.sock
Using server version 5.7.28-31-log
innobackupex version 2.4.24 based on MySQL server 5.7.35 Linux (x86_64) (revision id: b4ee263)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /data/mysql_3305
xtrabackup: open files limit requested 0, set to 102400
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 3
xtrabackup: innodb_log_file_size = 536870912
xtrabackup: using O_DIRECT
InnoDB: Number of pools: 1
220407 17:00:28 >> log scanned up to (2749841)
xtrabackup: Generating a list of tablespaces
InnoDB: Allocated tablespace ID 2 for mysql/plugin, old maximum was 0
220407 17:00:29 >> log scanned up to (2749841)
220407 17:00:30 [01] Copying ./ibdata1 to /data/data_bak/2022-04-07_17-00-28/ibdata1
220407 17:00:30 [01] ...done
220407 17:00:30 >> log scanned up to (2749841)
220407 17:00:31 [01] Copying ./mysql/plugin.ibd to /data/data_bak/2022-04-07_17-00-28/mysql/plugin.ibd
220407 17:00:31 [01] ...done
...省略
220407 17:00:33 Executing LOCK TABLES FOR BACKUP...
220407 17:00:33 Starting to backup non-InnoDB tables and files
220407 17:00:33 [01] Copying ./mysql/db.opt to /data/data_bak/2022-04-07_17-00-28/mysql/db.opt
220407 17:00:33 [01] ...done
...省略
220407 17:00:37 Finished backing up non-InnoDB tables and files
220407 17:00:37 Executing LOCK BINLOG FOR BACKUP...
220407 17:00:37 [00] Writing /data/data_bak/2022-04-07_17-00-28/xtrabackup_binlog_info
220407 17:00:37 [00] ...done
220407 17:00:37 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): '2749873'
xtrabackup: Stopping log copying thread.
.220407 17:00:37 >> log scanned up to (2749882)
220407 17:00:37 Executing UNLOCK BINLOG
220407 17:00:37 Executing UNLOCK TABLES
220407 17:00:37 All tables unlocked
220407 17:00:37 [00] Copying ib_buffer_pool to /data/data_bak/2022-04-07_17-00-28/ib_buffer_pool
220407 17:00:37 [00] ...done
220407 17:00:37 Backup created in directory '/data/data_bak/2022-04-07_17-00-28/'
MySQL binlog position: filename 'mysql-bin.000010', position '534', GTID of the last change '21973746-b571-11ec-b85c-fefcfe57c7ab:1-2,
d35a24ba-b572-11ec-b3ba-fefcfe5f33ef:1-12'
220407 17:00:37 [00] Writing /data/data_bak/2022-04-07_17-00-28/backup-my.cnf
220407 17:00:37 [00] ...done
220407 17:00:37 [00] Writing /data/data_bak/2022-04-07_17-00-28/xtrabackup_info
220407 17:00:37 [00] ...done
xtrabackup: Transaction log of lsn (2749832) to (2749882) was copied.
220407 17:00:37 completed OK!
附general日志
2022-04-07T17:00:28.867291+08:00 43 Connect root@localhost on using Socket
2022-04-07T17:00:28.868227+08:00 43 Query SET SESSION wait_timeout=2147483
2022-04-07T17:00:28.869036+08:00 43 Query SELECT CONCAT(@@hostname, @@port)
2022-04-07T17:00:28.871236+08:00 43 Quit
2022-04-07T17:00:28.875219+08:00 44 Connect root@localhost on using Socket
2022-04-07T17:00:28.875460+08:00 44 Query SET SESSION wait_timeout=2147483
2022-04-07T17:00:28.875647+08:00 44 Query SET SESSION autocommit=1
2022-04-07T17:00:28.875823+08:00 44 Query SET NAMES utf8
2022-04-07T17:00:28.876023+08:00 44 Query SHOW VARIABLES
2022-04-07T17:00:28.880405+08:00 44 Query SHOW ENGINE INNODB STATUS
2022-04-07T17:00:28.907864+08:00 44 Query SELECT PLUGIN_NAME, PLUGIN_LIBRARY FROM information_schema.plugins WHERE PLUGIN_STATUS = 'ACTIVE' AND PLUGIN_TYPE = 'KEYRING'
2022-04-07T17:00:28.909332+08:00 44 Query SELECT
CONCAT(table_schema, '/', table_name), engine
FROM information_schema.tables
WHERE engine NOT IN (
'MyISAM', 'InnoDB', 'CSV', 'MRG_MYISAM'
)
AND table_schema NOT IN (
'performance_schema', 'information_schema', 'mysql'
)
2022-04-07T17:00:33.243415+08:00 44 Query SET SESSION lock_wait_timeout=31536000
2022-04-07T17:00:33.243856+08:00 44 Query LOCK TABLES FOR BACKUP
2022-04-07T17:00:37.481442+08:00 44 Query LOCK BINLOG FOR BACKUP
2022-04-07T17:00:37.481793+08:00 44 Query SHOW MASTER STATUS
2022-04-07T17:00:37.482095+08:00 44 Query SHOW VARIABLES
2022-04-07T17:00:37.500022+08:00 44 Query FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS
2022-04-07T17:00:37.707812+08:00 44 Query UNLOCK BINLOG
2022-04-07T17:00:37.708174+08:00 44 Query UNLOCK TABLES
2022-04-07T17:00:37.722529+08:00 44 Query SELECT UUID()
2022-04-07T17:00:37.722916+08:00 44 Query SELECT VERSION()
2022-04-07T17:00:37.953575+08:00 44 Quit
参考:
https://www.percona.com/doc/percona-xtrabackup/2.4/index.html