Percona XtraBackup 8.0.26实战大全
Posted 老叶茶馆_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Percona XtraBackup 8.0.26实战大全相关的知识,希望对你有一定的参考价值。
本文首发于 GreatSQL社区 微信公众号。
1. Percona XtraBackup特性说明
2. Percona Xtrabackup 备份恢复权限限制
3. 创建备份用户、配置参数及数据准备
4. 全量备份与恢复
5. 增量备份
6. 压缩备份
7. 流备份
1. Percona XtraBackup特性说明
1) Percona Xtrabackup 8.0.26新增支持MyRocks存储引擎,不支持TokuDB引擎
2) Percona Xtrabackup 8.0.26 不支持低于mysql 8.0的备份(因为MySQL 8.0在数据字典、redo log中和之前版本不兼容)
3) Percona Xtrabackup 8.0.26 目前X86版本可以从官方下载,ARM版本需要手动编译
4) 备份文件必须是空的,没有任何文件
2. Percona Xtrabackup 备份恢复权限限制
1)备份路径需要有可读写权限
2)reload和Lock Tables(指定--no-locak选项除外),因为备份前需要执行FLUSH TABLES WITH READ LOCK和FLUSH ENGINE LOGS
3)Backup_admin权限,因为备份时需要查询performance_schema.log_status表并运行LOCK INSTANCE FOR BACKUP, LOCK BINLOG FOR BACKUP, or LOCK TABLES FOR BACKUP
4)Replication client权限。备份时为了读取二进制日志文件
5)Create tablespace权限。恢复表时需要创建表
6)Process权限。备份时需要运行show engine innodb status命令
7)Super权限。为了在复制环境中启动/停止复制线程
8)Create权限。为了创建percona_schema.xtrbackup_history表
9)Alter权限。为了更新percona_schema.xtrbackup_history表
10)Insert权限。为了将历史记录插入到percona_schema.xtrbackup_history表
11)Select权限。为了查询历史数据
3. 创建备份用户、配置参数及数据准备
//创建用户
mysql > CREATE USER 'bkpuser' @ 'localhost' IDENTIFIED BY 's3cr%T' ;
mysql > GRANT BACKUP_ADMIN,PROCESS,RELOAD,LOCK TABLES,REPLICATION CLIENT ON *.* TO 'bkpuser' @ 'localhost' ;
mysql > GRANT SELECT ON performance_schema.log_status TO 'bkpuser' @ 'localhost' ;
Mysql > GRANT SELECT ON performance_schema.keyring_component_status TO bkpuser @ 'localhost'
mysql > FLUSH PRIVILEGES ;
配置参数,Xtrbackup在备份时会读取MySQL的my.cnf配置文件中[mysqld]和[xtrabackup]部分,所以我们可以在配置文件中设置备份的目录[xtrabackup],target_dir = /data/backups/mysql
注意:如果my.cnf配置文件中有--set-variable=<variable>=<value>,xtrabackup
不识别,需要使用--veriable=value
代替 版本检测,Percona xtrabackup 8.0.21添加了--no-server-version-check参数,会将备份源版本和Percona Xtrabackup版本对比
源系统和PXB版本一样,备份继续 源系统低于PXB版本,备份继续 源系统大于PXB版本,且参数未被覆盖,停止备份并返回错误 源系统大于PXB版本,参数被覆盖,备份继续
Shell> xtrabackup --backup --no-server-version-check --target-dir = $mysql /backup1
覆盖参数时,可能会发生备份失败、创建损坏的备份、备份成功
3.1 Sysbench造数据
mysql> create database sbtest;
//使用sysbench造数
Shell> sysbench /usr/share/sysbench/oltp_read_write.lua --mysql-host=172.16.50.161 --mysql-port=3306 --mysql-user='root' --mysql-password='123456' --mysql-db='sbtest' --tables=1 --table-size=5000 --threads=20 prepare
//检查
MySQL [sbtest]> select count(*) from sbtest.sbtest1;
+----------+
| count(*) |
+----------+
| 5000 |
+----------+
1 row in set (0.00 sec)
4. 全量备份与恢复
4.1 全量备份
Shell> xtrabackup --defaults-file=/data1/mysqldata/my.cnf --socket=/data1/mysqldata/data/mysql.sock --user=root --password=123456 --port=3306 --backup --target-dir=/data1/xtrabackup/full/
输出信息:
210913 14:07:01 Finished backing up non-InnoDB tables and files
210913 14:07:01 Executing FLUSH NO_WRITE_TO_BINLOG BINARY LOGS
210913 14:07:01 Selecting LSN and binary log position from p_s.log_status
210913 14:07:01 [00] Copying /data1/mysqldata/log/binlog.000004 to /data1/xtrabackup/full/binlog.000004 up to position 196
210913 14:07:01 [00]...done
210913 14:07:01 [00] Writing /data1/xtrabackup/full/binlog.index
210913 14:07:01 [00]...done
210913 14:07:01 [00] Writing /data1/xtrabackup/full/xtrabackup_binlog_info
210913 14:07:01 [00]...done
210913 14:07:01 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): '19442549'
xtrabackup: Stopping log copying thread at LSN 19442549.
Starting to parse redo log at lsn = 19442226
210913 14:07:01 Executing UNLOCK INSTANCE
210913 14:07:01 All tables unlocked
210913 14:07:01 [00] Copying ib_buffer_pool to /data1/xtrabackup/full/ib_buffer_pool
210913 14:07:01 [00]...done
210913 14:07:01 Backup created in directory '/data1/xtrabackup/full/'
MySQL binlog position: filename 'binlog.000004', position '196', GTID of the last change '8950f324-1441-11ec-b9b5-080027193a00:1-15'
210913 14:07:01 [00] Writing /data1/xtrabackup/full/backup-my.cnf
210913 14:07:01 [00]...done
210913 14:07:01 [00] Writing /data1/xtrabackup/full/xtrabackup_info
210913 14:07:01 [00]...done
xtrabackup: Transaction log of lsn (19442549) to (19442559) was copied.
210913 14:07:02 completed OK!
4.2 全量恢复
准备工作,由于使用--backup备份数据文件时时间点时不一致的(备份增量redo日志线程会实时监听增量数据),因为程序在不同的时间点复制,并且再复制过程中可能会被更改,如果使用innodb直接尝试启动这些数据文件,innodb将检测损坏并停止运行,所以需要使用--prepare将将备份文件进行崩溃恢复,来使用这些复制文件
Shell>xtrabackup --prepare --target-dir=/data1/xtrabackup/full
// 拷贝数据 注意:需要将源目录进行备份,且恢复目录要为空
Shell> xtrabackup --copy-back --target-dir=/data1/xtrabackup/full
// 修改目录属性启动数据库
Shell> chown -R mysql:mysql /data/mysqldata/data
Shell> chmod -R 755 /data/mysqldata/data
// 启动数据库
Shell> /data1/mysql/bin/mysqld --defaults-file=/data1/mysqldata/my.cnf --user=mysql &
// 检测
MySQL> select count(*) from sbtest.sbtest1;
5. 增量备份
增量备份需要在全量备份的基础上进行,每个innodb页面都包含一个日志序列号LSN。该LSN是整个数据库系统的版本号。每个页面的LSN显示最近更改的时间。增量备份时会将上一次全量备份的LSN或上一次增量备份的LSN和当前页面的LSN进行对比,通过两种算法继续查找。
第一种就是读取所有的数据页面直接检查页面LSN(适用所有版本MySQL),第二种就是在服务器上启用”更改页面跟踪“功能,该功能会在页面更改时进行记录,将信息保存下来,xtrbackup做增量备份时回去读取该信息,从而节省资源(该查找方法适用于percona server for MySQL)
全量备份
Shell> xtrabackup --defaults-file=/data1/mysqldata/my.cnf --socket=/data1/mysqldata/data/mysql.sock --user=root --password=123456 --port=3306 --backup --target-dir=/data1/xtrabackup/full/
应用中继日志
Shell> xtrabackup --prepare --apply-log-only --target-dir=/data1/xtrabackup/full
输出:
InnoDB: Shutdown completed; log sequence number 1626007
161011 12:41:04 completed OK!
增量备份:查看全量备份目录的xtrbackup_checkpoints
Shell> cat xtrabackup_checkpoints
backup_type = full-prepared //备份类型
from_lsn = 0 //开始LSN
to_lsn = 22254648//备份结束时LSN
last_lsn = 22254648 //上一个LSN号
flushed_lsn = 22254648//当前flush的LSN号
增量备份命令
Shell> xtrabackup --defaults-file=/data1/mysqldata/my.cnf --socket=/data1/mysqldata/data/mysql.sock --user=root --password=123456 --port=3306 --backup --target-dir=/data1/xtrabackup/inc1 --incremental-basedir=/data1/xtrabackup/full
输出信息:
210913 14:18:07 Finished backing up non-InnoDB tables and files
210913 14:18:07 Executing FLUSH NO_WRITE_TO_BINLOG BINARY LOGS
210913 14:18:07 Selecting LSN and binary log position from p_s.log_status
210913 14:18:07 [00] Copying /data1/mysqldata/log/binlog.000005 to /data1/xtrabackup/inc1/binlog.000005 up to position 196
210913 14:18:07 [00]...done
210913 14:18:07 [00] Writing /data1/xtrabackup/inc1/binlog.index
210913 14:18:07 [00]...done
210913 14:18:07 [00] Writing /data1/xtrabackup/inc1/xtrabackup_binlog_info
210913 14:18:07 [00]...done
210913 14:18:07 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): '20799610'
xtrabackup: Stopping log copying thread at LSN 20804588.
Starting to parse redo log at lsn = 20799534
210913 14:18:07 >> log scanned up to (20804598)
210913 14:18:08 Executing UNLOCK INSTANCE
210913 14:18:08 All tables unlocked
210913 14:18:08 [00] Copying ib_buffer_pool to /data1/xtrabackup/inc1/ib_buffer_pool
210913 14:18:08 [00]...done
210913 14:18:08 Backup created in directory '/data1/xtrabackup/inc1/'
MySQL binlog position: filename 'binlog.000005', position '196', GTID of the last change '8950f324-1441-11ec-b9b5-080027193a00:1-20'
210913 14:18:08 [00] Writing /data1/xtrabackup/inc1/backup-my.cnf
210913 14:18:08 [00]...done
210913 14:18:08 [00] Writing /data1/xtrabackup/inc1/xtrabackup_info
210913 14:18:08 [00]...done
xtrabackup: Transaction log of lsn (20799610) to (20804624) was copied.
210913 14:18:09 completed OK!
//检查当前checkpoints点
Shell> cat xtrabackup_checkpoints
backup_type = incremental
from_lsn = 22254648
to_lsn = 33288485
last_lsn = 33299549
flushed_lsn = 33288485
From_lsn是备份的起始LSN,对于增量,他必须与前一个备份检查点to_lsn相同
应用中继日志
--prepare增量备份恢复时与全量备份不同,在全量备份时执行--prepare为了保证数据库一致,针对数据文件从日志文件重放已提交的事务,以及回滚未提交的事务。在增量时,必须跳过未提交事务的回滚,因为备份时未提交的事务可能正在进行中,很可能在下一次增量备份中提交,所以需要使用--apply-log-only选项来防止回滚阶段。
将第一个增量备份应用到全量备份中(增量文件将会应用到/data1/xtrbackup/full中)
hell> xtrabackup --prepare --apply-log-only --target-dir=/data1/xtrabackup/full --incremental-dir=/data1/xtrabackup/inc1
输出信息:
incremental backup from 1626007 is enabled.
xtrabackup: cd to /data/backups/base
xtrabackup: This target seems to be already prepared with --apply-log-only.
xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(4124244)
...
xtrabackup: page size for /tmp/backups/inc1/ibdata1.delta is 16384 bytes
Applying /tmp/backups/inc1/ibdata1.delta to ./ibdata1...
...
161011 12:45:56 completed OK!
注意:全量备份中的to_LSN号和增量备份中的checkpoints的to_lsn号相同 如果做多次增量备份时,需要在每次做完增量备份后使用--prepare --apply-log-only将增量部分合并到全量备份中,否则会报lsn冲突
xtrabackup: This target seems to be already prepared with --apply-log-only.
xtrabackup: error: This incremental backup seems not to be proper for the target.
xtrabackup: Check 'to_lsn' of the target and 'from_lsn' of the incremental.
5.1 增量恢复
Shell> xtrabackup --prepare --target-dir=/data1/xtrabackup/full
// 拷回数据:
Shell> xtrabackup --defaults-file=/data1/mysqldata/my.cnf --copy-back --target-dir=/data1/xtrabackup/full
// 修改数据目录的权限和属性:
Shell> chown -R mysql:mysql /data1/mysqldata1
Shell> chmod -R 755 /data1/mysqldata1 启动数据库
Shell>/data1/mysql/bin/mysqld --defaults-file=/data1/mysqldata/my.cnf --user=mysql &
// 检测
MySQL> select count(*) from sbtest.sbtest1;
全备+增备恢复完成
6. 压缩备份
我们在备份时可以对备份文件进行压缩备份,使用--compress选项
Shell> xtrabackup --defaults-file=/data1/mysqldata/my.cnf --socket=/data1/mysqldata/data/mysql.sock --user=root --password=123456 --port=3306 --backup --compress --target-dir=/data1/xtrabackup/full
由于--compress使用到了qpress,所以需要安装percona-release包
Shell> suudo percona-release enable tools
Shell> sudo apt-get update
Shell> sudo apt-get install qpress
增加备份并行压缩线程
Shell> xtrabackup --defaults-file=/data1/mysqldata/my.cnf --socket=/data1/mysqldata/data/mysql.sock --user=root --password=123456 --port=3306 --backup --compress --compress-threads=4 --target-dir=/data1/xtrabackup/full
输出如下信息:
...
170223 13:00:38 [01] Compressing ./test/sbtest1.frm to /tmp/compressed/test/sbtest1.frm.qp
170223 13:00:38 [01]...done
170223 13:00:38 [01] Compressing ./test/sbtest2.frm to /tmp/compressed/test/sbtest2.frm.qp
170223 13:00:38 [01]...done
...
170223 13:00:39 [00] Compressing xtrabackup_info
170223 13:00:39 [00]...done
xtrabackup: Transaction log of lsn (9291934) to (9291934) was copied.
170223 13:00:39 completed OK!
6.1 解压备份及恢复
解压文件:
Shell> xtrabackup --decompress --target-dir=/data/compressed/
Shell> xtrabackup --prepare --target-dir=/data/compressed/
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 9293846
170223 13:39:31 completed OK!
备份恢复:
Shell> xtrabackup --copy-back --target-dir=/data/backups/
7. 流备份
使用流备份传输到名为backup.xbstream
Shell> xtrabackup --backup --stream=xbstream --target-dir=./ > backup.xbstream
加密备份
Shell> xtrabackup –backup –stream=xbstream ./ > backup.xbstream gzip - | openssl des3 -salt -k “密码” > backup.xbstream.gz.des3
加压备份到当前目录
Shell> xbstream -x < backup.xbstream
使用流备份方式到其他服务器上
Shell> xtrbackup --backup --stream=xbstream --target-dir=./ | ssh user@otherhost "xbstream -x "
限制备份工具的吞吐量为10MB/秒
Shell> xtrabackup –backup –stream=xbstream ./ | pv -q -L10m ssh user@desthost “cat - > /data/backups/backup.xbstream”
Enjoy GreatSQL :)
接下来是带货时间,GreatSQL社区出品,我(老叶)主讲的免费课程,欢迎报名学习,认真学完即可快速上手MySQL MGR
文章推荐:
扫码加入GreatSQL/MGR交流QQ群
点击文末“阅读原文”直达老叶专栏
以上是关于Percona XtraBackup 8.0.26实战大全的主要内容,如果未能解决你的问题,请参考以下文章
Centos 6.8 安装Percona Xtrabackup
Percona-Xtrabackup 2.3.3 不再堵塞备份