Percona XtraBackup 核心文档

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

1. 介绍

1.1 MySQL 备份工具特性对比

Features Percona XtraBackup MySQL Enterprise backup
License GPL Proprietary
Price Free Included in subscription at $5000 per Server
Streaming and encryption formats Open source Proprietary
Supported MySQL flavors MySQL, Percona Server, MariaDB, Percona XtraDB Cluster, MariaDB Galera Cluster MySQL
Supported operating systems Linux Linux, Solaris, Windows, OSX, FreeBSD.
External graphical user interfaces to backup/recovery Zmanda Recovery Manager for MySQL MySQL Workbench, MySQL En- terprise Monitor
Non-blocking InnoDB backups Yes Yes
Blocking MyISAM backups Yes Yes
Full compressed backups Yes Yes
Encrypted backups Yes Yes
Streaming backups Yes Yes
Parallel local backups Yes Yes
Parallel compression Yes Yes
Parallel encryption Yes Yes
Partial backups Yes Yes
Throttling Yes Yes
Point-in-time recovery support Yes Yes
Backup history table Yes Yes
Individual tables export Yes Yes
Restoring tables to a different server Yes Yes
InnoDB secondary indexes defragmentation Yes -
rsync support to minimize lock time Yes -
Improved FTWRL handling Yes -
Incremental compressed backups Yes -
Fast incremental backups Yes -
Incremental backups with archived logs feature in Percona Server Yes -
Backup locks Yes -
Partial backups of individual partitions Yes -
Parallel apply-log Yes -
Safe slave backups Yes -
Compact backups Yes -
Data & index file statistics Yes -
Buffer pool state backups Yes -
Individual partitions export Yes -
Backup progress table - Yes
Offline backups - Yes
Parallel copy-back - Yes
Backup to tape media managers - Yes
Backup image validation - Yes
Incremental backups with REDO log only - Yes
Cloud backups support - Amazon S3

通过以上对比,你能够清楚的知道

  1. 哪些是两者共有的特点
  2. 哪些是只有percona xtrabackup 才有的特点
  3. 哪些是ibbackup的特点

1.2 Percona XtraBackup的特性

下面简单罗列下,如需知道更多特性,请看后面的详细内容

  • 热备,可以不需要中断DB服务器
  • 增量备份
  • 流式备份(Stream compressed)到其他服务器
  • 在线迁移表
  • 可以非常简单高效的创建一个新的slave
  • 备份任务不会给服务器带来很高的负载

2. 安装

下载一个linux-generic 版本的二进制包即可,方便简单。

3. 用户手册

3.1 The innobackupex Script

innobackupex 是一个用perl写的工具,主要是封装了xtrabackup【C写的】
innobackupex 整合了 xtrabackup & 其他功能如:文件copying and streaming,提供更多方便好用的功能
不管是innoDB,XtraDB,Myisam , 都提供point-in-time备份和恢复

3.1.1 前提

连接和权限

mysql> CREATE USER ’bkpuser’@’localhost’ IDENTIFIED BY ’xx‘;
mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO ’bkpuser’@’localhost’;
mysql> FLUSH PRIVILEGES;

3.1.2 备份方式 : 全量备份

3.1.2.1 Creating a Backup with innobackupex

innobackupex 是一个可以组合xbstream & xbcrypt 的全量备份MySQL数据库的工具

为了创建一个备份,除了基本的连接参数外,额外仅仅只需要一个备份路径参数即可,就这么简单

$ innobackupex --user=DBUSER --password=DBUSERPASS /path/to/BACKUP-DIR/

检查最后的输出来确保备份正确

innobackupex: Backup created in directory ’/path/to/BACKUP-DIR/2013-03-25_00-00-09'
 innobackupex: MySQL binlog position: filename ’mysql-bin.000003’, position 1946
 111225 00:00:53  innobackupex: completed OK!

在这个例子当中,默认备份文件名会以timestamp命名:/path/to/BACKUP-DIR/2013-03-25_00-00-09

  • 其他选项

--no-timestamp : 备份结果不会存储在一个 stamped directory 中

$ innobackupex --user=DBUSER --password=DBUSERPASS /path/to/BACKUP-DIR/ --no-timestamp

--defaults-file : 可以指定配置文件,唯一的限制就是这个参数必须是第一个option

$ innobackupex --defaults-file=/tmp/other-my.cnf --user=DBUSER --password=DBUSERPASS /path/to/BACKUP-DIR

3.1.2.2 Preparing a Full Backup with innobackupex

这个阶段叫:prepare stage 。
创建备份后,这时候数据还不能被立刻使用。因为这时候还有很多uncommitted 事务没有被回滚,同样还有很多事务没有被replayed。
通过--apply-log 后,这些数据文件才能算是一致的数据。

$ innobackupex --apply-log /path/to/BACKUP-DIR

  • 详解prepare stage

innobackupex 通过backup-my.cnf文件来开始prepare

然后,innobackupex 回放已经提交【committed】的事务,回滚未提交【uncommitted】的事务
当以上步骤完成后,所有信息都准备完毕,重做日志会被re-created

相当于调用了两次xtrabackup --prepare 两次。

  • 其他选项

--use-memory option: prepare stage的专用参数,默认100M,使用内存越多,恢复速度越快。

$ innobackupex --apply-log --use-memory=4G /path/to/BACKUP-DIR

3.1.2.3 Restoring a Full Backup with innobackupex

为了方便,innobackupex提供了 --copy-back选项,它可以自动帮助我们将备份文件copy到datadir

$ innobackupex --copy-back /path/to/BACKUP-DIR

它会根据服务器上的my.cnf 来将所有数据相关文件拷贝到指定的 datadir

你可以检查最后的输出

innobackupex : finished copying back files
111225 01:01:13 innobackupex: completed OK!

注意:一定要保证datadir为空目录,否则有可能报错,如果文件已经存在,就不会复制。 除非指定了 --force-non-empty-directories.
并且记住:restore的时候,必须保证mysql处于shutdown状态。
最后记住:重新给文件赋予正确的属组合权限

$ chown -R mysql:mysql /var/lib/mysql

3.1.3 其他备份方式

3.1.3.1 增量备份

恢复速度太慢,忽略

3.1.4 部分表备份

Percona xtrabackup 提供了部分表【partial】备份,顾名思义:只备份一部分表或库
前提是:你必须设置了 innodb_file_per_table

对于部分表备份,只有一个忠告:千万别将prepared backup copy back回家。
restoring partial backups,你应该importing 表,而不是 --copy-back选项。

3.1.4.1 Creating Partial Backups

有三种备份方式: regular expressions(--include), enumerating (--tables-file)以及提供databases (--databases)

  • regular expressions(--include)

格式:databasename.tablename.

例如:

$ innobackupex --include=’^mydatabase[.]mytable’ /path/to/backup

以上命令,只有被匹配的表才会被备份

  • Using the --tables-file option

可以使用文件列表,一行一个,格式: databasename.tablename

例如:

echo "mydatabase.mytable" > /tmp/tables.txt
innobackupex --tables-file=/tmp/tables.txt /path/to/backup

  • Using the --databases option

以空格为间隔符,指定表或者库

例如:

$ innobackupex --databases="mydatabase.mytable mysql" /path/to/backup

以上命令,创建了一个包含 mytable表 和 mysql 库的备份

3.1.4.2 Preparing Partial Backups

对于部分表备份的prepare,步骤类似 restoring individual tables一样 : 应用日志 并且 使用--export 选项:

innobackupex --apply-log --export /path/to/partial/backup

你会收到一部分warnings,因为这是部分表prepare和export,没有被prepare的表会从数据字典移除。

111225  0:54:06  InnoDB: Error: table ’mydatabase/mytablenotincludedinpartialb’
InnoDB: in InnoDB data dictionary has tablespace id 6,
InnoDB: but tablespace with that id or name does not exist. It will be removed from data dictionary.

3.1.4.3 Restoring Partial Backups

方法参考: restoring individual tables
当然你也可以将其copying back 到一个全新的server上,系统表空间可以被重建

例如:

$ sudo mysql_install_db --user=mysql

3.1.5 Compact Backups

compact备份指的是: 不备份二级索引,这样空间占用少。
缺点是: prepare & restore 的时候非常慢 , 因为需要rebuild 二级索引

例如:

backup size without --compact option
2.0G 2013-02-01_10-18-38

backup size taken with --compact option
1.4G 2013-02-01_10-29-48

Creating Compact Backups

$ innobackupex --compact /data/backups

检查 xtrabackup_checkpoints,当指定了--compact, compact 是1,否则是0

backup_type = full-backuped
from_lsn = 0
to_lsn = 2888984349
last_lsn = 2888984349
compact = 1

Preparing Compact Backups

prepare compact 备份,必须加上这个新参数:--rebuild-indexes

$ innobackupex --apply-log --rebuild-indexes /data/backups/2013-02-01_10-29-48

日志结果:

130201 10:40:20  InnoDB: Waiting for the background threads to start
Rebuilding indexes for table sbtest/sbtest1 (space id: 10)
  Found index k_1
  Dropping 1 index(es).
  Rebuilding 1 index(es).
Rebuilding indexes for table sbtest/sbtest2 (space id: 11)
  Found index k_1
  Found index c
  Found index k
  Found index c_2
  Dropping 4 index(es).
  Rebuilding 4 index(es).

Restoring Compact Backups

$ innobackupex --copy-back /path/to/BACKUP-DIR

3.1.6 Encrypted Backups

不常用

3.1.7 高级特性

3.1.7.1 Streaming and Compressing Backups

Streaming 模式,将备份以 tar 或者 xbstream 的格式发送,而不是传统的直接copying 文件
你可以写自己的过滤规则来或者你想要的数据,这样灵活性更大。比如,你还可以使用自己的指定的压缩。
另一个使用streaming备份的好处就是: 自动加密

想要使用streaming 特性,必须使用 --stream 选项,并且提供这两种format (tar 或者 xbstream)以及存储temporary files的目录

$ innobackupex --stream=tar /tmp

当压缩功能打开时,xtrabackup 会用quicklz算法压缩innodb表,*.qp结尾。

当使用xbstream的时候,备份在 copying 文件 以及 compress 文件上,使用并行【parallel】功能来提升速度

  • 使用xbstream的例子

将一个完整的全备直接备份成一个文件:

$ innobackupex --stream=xbstream /root/backup/ > /root/backup/backup.xbstream

stream & compress 模式开启:

$ innobackupex --stream=xbstream --compress /root/backup/ > /root/backup/backup.xbstream

解压到/root/backup中:

$ xbstream -x < backup.xbstream -C /root/backup/

将一个压缩的备份传送到其他服务器并解压:

$ innobackupex --compress --stream=xbstream /root/backup/ | ssh user@otherhost "xbstream -x -C /root/backup/

  • 使用tar的例子

$ innobackupex --stream=tar /root/backup/ > /root/backup/out.tar
$ innobackupex --stream=tar ./ | ssh user@destination "cat - > /data/backups/backup.tar"
$ innobackupex --stream=tar ./ | gzip - > backup.tar.gz
$ innobackupex --stream=tar ./ | bzip2 - > backup.tar.bz2



注意:以上streaming 模式并没有对备份进行prepare ,所以在restore 之前,必须prepare

3.1.8 Taking Backups in Replication Environments

跟复制相关的备份需要关注的option

  • The --slave-info option

当你在slave上备份的时候,这个参数特别有用。它会保留相关的复制信息如:对应的master binlog position 和 file。
也会将相关信息change master等信息写入 xtrabackup_slave_info

  • The --safe-slave-backup option

为了确保一致性复制状态,这个选项会将sql_thread stop掉,并且等待开启备份直到slave_open_temp_tables 为0
如果没有open的tempory 表,那么备份就会开始,否则sql_thread就会一直stop,直到slave_open_temp_tables 为0
如果--safe-slave-backup-timeout(默认300s)时间超过后,slave_open_temp_tables还不为0,那么备份就会失败,sql thread 会被restart
如果从slave上备份,这个参数推荐使用

Accelerating with --parallel copy and –compress-threads

--parallel 可以指定并行线程数来并发copy innodb数据文件

$ innobackupex --parallel=4 /path/to/backup

--compress-threads : 这个参数只和xbstream绑定使用,并发压缩

$ innobackupex --stream=xbstream --compress --compress-threads=4 ./ > backup.xbstream

在prepare之前,必须先解压xbstream

Accelerating with --rsync option

这个参数是用来提升非innoDB引擎的备份,意义不是很大
注意: --rsync 不能与 --remote-host ,--stream 一起使用

Throttling backups with innobackupex

如果压力特别大,这个参数可以限流,类似--sleep

Restoring Individual Tables (5.6+ 才支持)

  • exporting tables

$ innobackupex --apply-log --export /path/to/backup

这个命令会为每个ibd表都增加一个.exp 的文件,有这个文件,才能够正确的import到online server

  • importing tables

导入数据前,必须先在其他服务器上创建好同样的表结构,如果错误,会造成db server hang,小心

OTHERSERVER|mysql> CREATE TABLE mytable (...) ENGINE=InnoDB;

然后discard 表空间

OTHERSERVER|mysql> ALTER TABLE mydatabase.mytable DISCARD TABLESPACE;

最后,拷贝文件.mydatabase.mytable.ibd , mytale.exp 到数据目录,import到tablespace中

OTHERSERVER|mysql> ALTER TABLE mydatabase.mytable IMPORT TABLESPACE;

Point-In-Time recovery

基于binlog

Improved FLUSH TABLES WITH READ LOCK handling

percona server 才支持

Waiting for queries to finish

Killing the blocking queries

Store backup history on the server

3.1.8 innobackupex 实现

Making a Backup

  1. innobackupex 调用 xtrabackup --suspend-at-end
  2. xtrabackup 开始拷贝 innodb 数据文件
  3. 当xtrabackup拷贝完成innodb文件后,会创建 xtrabackup_suspended_2
  4. 当innobackupex看到xtrabackup_suspended_2文件被创建后,会调用命令flush table with read lock
  5. innobackupex开始检查db server支持的特性,如:gtid,backup locks,changed page bitmap等
  6. 然后开始拷贝非innodb文件
  7. 当所有的文件都备份完成后,结束redo 事务的拷贝。
  8. 接下来释放锁
  9. 删除xtrabackup_suspended_2文件 , 并且退出

再这些过程中,还创建了如下文件

xtrabackup_checkpoints : 记录了lsn和备份类型
xtrabackup_binlog_info : 记录了备份开始时的binary日志信息
xtrabackup_binlog_pos_innodb : 记录了备份innodb redo事务时刻的binary log信息
xtrabackup_slave_info : 记录了slave上show slave status相关信息
backup-my.cnf : 记录了部分需要备份的信息
xtrabackup_binary : 记录了这段备份期间执行过的事务

Restoring a backup

copy-back or move-back
它自己不会记录日志,需要我们指定管道记录日志

References

罗列了全的innobackupex相关的参数和选项

3.2 xtrabackup binary

我们只用innobackupex即可

3.3The xbstream Binary

3.4 The xbcrypt binary

用的少

3.5 The xbcloud Binary

用的少

3.6 How Percona XtraBackup Works

4.1 innobackupex how-tos

4.1.1 Make a Local Full Backup (Create, Prepare and Restore)

Create the backup阶段

下面是一个简单的案例

$ innobackupex /data/backups

Prepare the Backup阶段

--apply-log --use-memory 是常用的选项

$ innobackupex --use-memory=4G --apply-log /data/backups/2010-03-13_02-42-44/

Restore the Backup阶段

恢复一个已经prepared的备份,首先要停掉MySQL server,然后使用--copy-back

innobackupex --copy-back /data/backups/2010-03-13_02-42-44/
调整权限 chown -R mysql:mysql /data/mysql_data/mysql

4.1.2 Make a Streaming Backup

stream for tar
流的方式备份可以将备份用tar写到标准输出来取代之前直接copy文件,该参数必须是第一个option
然后可以通过管道的方式用gzip等压缩工具压缩,或者直接传送其他远端服务器上
解压的时候,必须使用-i参数,tar -ixvf backup.tar

例如:

  • Stream the backup into a tar archived named ‘backup.tar’
 $ innobackupex --stream=tar ./ > backup.tar
  • The same, but compress it
 $ innobackupex --stream=tar ./ | gzip - > backup.tar.gz
  • Send it to another server instead of storing it locally
 $ innobackupex --stream=tar ./ | ssh user@desthost "cat - > /data/backups/backup.tar"
  • The same thing with can be done with the ‘’netcat’‘.
## On the destination host:
$ nc -l 9999 | cat - > /data/backups/backup.tar
## On the source host:
$ innobackupex --stream=tar ./ | nc desthost 9999
  • The same thing, but done as a one-liner:

$ ssh user@desthost "( nc -l 9999 > /data/backups/backup.tar & )" && innobackupex --stream=tar ./ | nc desthos

  • Throttling the throughput to 10MB/sec. This requires the ‘pv’ tools; you can find them at the official site or install it from the distribution package (“apt-get install pv”)

$ innobackupex --stream=tar ./ | pv -q -L10m | ssh user@desthost "cat - > /data/backups/backup.tar"

  • Checksumming the backup during the streaming
## On the destination host:
$ nc -l 9999 | tee >(sha1sum > destination_checksum) > /data/backups/backup.tar
## On the source host:
$ innobackupex --stream=tar ./ | tee >(sha1sum > source_checksum) | nc desthost 999
## compare the checksums
## On the source host:
$ cat source_checksum
65e4f916a49c1f216e0887ce54cf59bf3934dbad  -
## On the destination host:
$ destination_checksum
65e4f916a49c1f216e0887ce54cf59bf3934dbad  -

stream for xbstream

举个例子:

  • Stream the backup into a tar archived named backup.xbstream
 innobackupex --stream=xbstream ./ > backup.xbstream
  • The same but with compression
 innobackupex --stream=xbstream --compress ./ > backup.xbstream
  • To unpack the backup to the current directory:
 xbstream -x <  backup.xbstream
  • Sending backup compressed directly to another host and unpacking it:
 innobackupex --compress --stream=xbstream ./ | ssh user@otherhost "xbstream -x"
  • Parallel compression with parallel copying backup
 innobackupex --compress --compress-threads=8 --stream=xbstream --parallel=4 ./ > backup.xbstream

4.1.3 Making an Incremental Backup

恢复慢,用的少

4.1.4 Making a Compressed Backup

--compress 可以帮你

$ innobackupex --compress /data/backup

如果想要多线程compress,可以使用 --compress-threads

$ innobackupex --compress --compress-threads=4 /data/backup

preparing compress backup时候,可以加上 --decompress

$ innobackupex --decompress /data/backup/2013-08-01_11-24-04/

4.1.5 Backing Up and Restoring Individual Partitions

用的少

4.2 xtrabackup how-tos

innobackupex 就够了

5. 已知问题和限制

5.1 ,5.5 对于innodb 压缩表的updates类型的redo事件存在bug,并没有修复,直到5.6.12 (bug #16267120)
5.6中innodb_log_compressed_pages这个参数,不推荐设置OFF,默认是ON
在optimize table(bug #1541763) 或者 alter table ... tablespace(bug #1532878) 的时候去备份,会导致备份的数据无法recover
compact 备份目前还存在bug,建议不要用 (bug #1192834)

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
10月前
|
存储 人工智能 缓存
ChatGPT会取代MySQL DBA吗?(译自Percona,文末投票)
ChatGPT是目前科技界最热门的话题,有人甚至说ChatGPT已经通过了谷歌的3级编程面试。我想知道,这是否意味着ChatGPT也准备好取代MySQL DBA?答案是否定的,让我来告诉你为什么。
|
关系型数据库 MySQL 数据库
【MySQL】Xtrabackup备份工具使用
Xtrabackup是由percona提供的mysql数据库备份工具,支持在线热备份(备份时不影响数据读写). Xtrabackup有两个主要的工具:xtrabackup、innobackupex 1、xtrabackup只能备份InnoDB和XtraDB两种数据表,而不能备份MyISAM数据表 2、innobackupex是参考了InnoDB Hotbackup的innoback脚本修改而来的.innobackupex是一个perl脚本封装,封装了xtrabackup。主要是为了方便的 同时备份InnoDB和MyISAM引擎的表,但在处理myisam时需要加一个读锁。并且加入了一些使用的选项
266 0
|
关系型数据库 MySQL Linux
Percona XtraBackup 8.0, 安装与测试
Percona XtraBackup 8.0, 安装与测试Percona XtraBackup 8.0 是Percona XtraBackup新推出了一个针对MySQL8.0的版本,主要是MySQL8.0在Redo 和 数据库字典方面有了新的改进。
4066 0
|
关系型数据库 MySQL 测试技术
Percona XtraBackup 关于 MySQL备份还原的详细测试
Percona XtraBackup 关于 MySQL备份还原的详细测试一. Percona XtraBackup 的优点。(1)无需停止数据库进行InnoDB热备; (2)增量备份MySQL; (3)流压缩传输到其它服务器; (4)在线移动表; (5)能够比较容易地创建主从同步; (6)备份MySQL时不会增大服务器负载。
1406 0
|
关系型数据库 MySQL Unix
MySQL中的Percona-toolkit工具由来漫谈
首先问一个问题,你听说过下面这两个工具吗? Maatkit 和Aspersa 如果听过,可能就暴露年龄了,你如果现在去查Aspersa相关的文章,会发现下载链接之类的都不可用了。
1680 0