MySQL备份工具innobackupex

innobackupex工具的使用:

介绍

The innobackupex tool is a Perl script that acts as a wrapper for the xtrabackup C program. It is a patched version of the innobackup Perl script that Oracle distributes with the InnoDB Hot Backup tool. It enables more functionality by integrating xtrabackup and other functions such as file copying and streaming, and adds some convenience. It lets you perform point-in-time backups of InnoDB / XtraDB tables together with the schema definitions, MyISAM tables, and other portions of the server.
innodbackupex工具是一个Perl脚本,它对xtrabackup 程序的做了一个封装。这是一个修补程序版本的innodbackup perl脚本,innodb的热备份工具。它集成了xtrabackup和其他功能(如文件复制和流媒体),可以增加更多的功能性,并增加了一些便利性。它允许你将innodb表和MyISAM表和服务器的其他部分一起执行表的实时备份。

下载和安装

下载地址
https://www.percona.com/downloads/XtraBackup/LATEST/

备份

1,创建一个全备

The --defaults-file option You can provide other configuration file to innobackupex with this option. Theonly limitation is that it has to be the first option passed:
提供默认的配置,需要放在最前面指定
例如:

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

innobackupex --defaults-file=/data/3306/my.cnf --user=root --password=123456 /tmp/backups/

2,创建增量备份

innobackupex --defaults-file=/data/3306/my.cnf --incremental /tmp/backups/inc/ --incremental-basedir=/tmp/backups/2017-01-04_16-53-06 --user=root --password=123456

innobackupex --defaults-file=/data/3306/my.cnf --incremental /tmp/backups/inc/ --incremental-basedir=/tmp/backups/inc/2017-01-04_17-01-57 --user=root --password=123456

也可以通过LSN号来备份

innobackupex --incremental /data/backups --incremental-lsn=1291135

innobackupex --incremental /data/backups --incremental-lsn=1358967

可以从任意增量上的基础来进行备份
This is a very useful way of doing an incremental backup, since not always the base or the last incremental will beavailable in the system.
这是一种非常有用的增量备份方法,因为在系统中不一定总是有基础备份或最后的增量备份。

3,准备全备备份

**Preparing a Full Backup with innobackupex**
innobackupex --apply-log /path/to/BACKUP-DIR
--user-memory默认是100M,提高数值可以加快速度
innobackupex --apply-log --redo-only /tmp/backups/2017-01-04_16-53-06/ --use-memory=1G --user=USER --password=123456   

4,合并增量和全量备份

--redo-only

--redo-only should be used when merging all incrementals except the last one. That’s why the previous
line doesn’t contain the --redo-only option. Even if the --redo-only was used on the last step, backup wouldstill be consistent but in that case server would perform the rollback phase

在最后一次合并增量,全量备份的时候,不需要加上--redo-only,如果加上了,也不会导致数据不一致,但是会让mysql处于rollback的阶段。

innobackupex --apply-log --redo-only /tmp/backups/2017-01-04_16-53-06/ --incremental-dir=/tmp/backups/inc/2017-01-04_17-01-57 --use-memory=1G --user=root --password=123456

innobackupex --apply-log /tmp/backups/2017-01-04_16-53-06/ --incremental-dir=/tmp/backups/inc/2017-01-05_10-19-23 --use-memory=1G --user=root --password=123456

5,测试

关闭数据库

[root@localhost /]# mysqladmin --defaults-file=/data/3306/my.cnf shutdown -uroot –p

删除数据文件

rm /data/3306/data –rf

恢复

innobackupex --defaults-file=/data/3306/my.cnf --copy-back /tmp/backups/2017-01-04_16-53-06

授权

chown -R mysql:mysql /data/3306/data

启动数据库
OK完成
检查数据

压缩backups

Compact Backups
创建compact
Creating Compact Backups To make a compact backup innobackupex needs to be started with the --compact option:
$ innobackupex --compact /data/backups
准备
Preparing Compact Backups Preparing the compact require rebuilding the indexes as well. In order to prepare the backup a new option --rebuild-indexes should be used with --apply-logs:

–apply-log –rebuild-indexes. 可以使用
$ innobackupex --apply-log --rebuild-indexes /data/backups/2013-02-01_10-29-4

恢复

Restoring Compact Backups innobackupex has a --copy-back option, which performs the restoration of abackup to the server’s datadir

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

加密备份

Encrypted Backups

Creating Encrypted Backups To make an encrypted backup following options need to be specified (options--encrypt-key and --encrypt-key-file are mutually exclusive, i.e. just one of them needs to be provided):

创建加密备份的时候的时候需要加入选项--encrypt-key --encrypt-key-file其中一个

• --encryption=ALGORITHM - currently supported algorithms are: AES128, AES192 and AES256

• --encrypt-key=ENCRYPTION_KEY - proper length encryption key to use. It is not recommended to use this option where there is uncontrolled access to the machine as the command line and thus the key can be viewed as part of the process info.

• --encrypt-key-file=KEYFILE - the name of a file where the raw key of the appropriate length can be

read from. The file must be a simple binary (or text) file that contains exactly the key to be used.

Both --encrypt-key option and --encrypt-key-file option can be used to specify the encryption key.

Encryption key can be generated with command like:

加密秘钥可以通过下面的命令来生成

$ openssl rand -base64 24

Example output of that command should look like this:

GCHFLrDFVx6UAsRb88uLVbAVWbK+Yzfs

使用例子

Using the --encrypt-key option Example of the innobackupex command using the --encrypt-key should look like this

$ innobackupex --encrypt=AES256 --encrypt-key="GCHFLrDFVx6UAsRb88uLVbAVWbK+Yzfs" /data/backups

Using the --encrypt-key-file option Example of the innobackupex command using the --encrypt-key-file should look like this

$ innobackupex --encrypt=AES256 --encrypt-key-file=/data/backups/keyfile /data/backups

加密备份使用优化

Optimizing the encryption process

Two new options have been introduced with the encrypted backups that can be used to speed up the encryption process. These are --encrypt-threads and --encrypt-chunk-size.
使用--encrypt-threads 和--encrypt-chunk-size.这两个选项加速加密过程
By using the --encrypt-threads option multiple threads can be specified to be used for encryption in parallel.
使用--encrypt-threads可以多线程加密

Option --encrypt-chunk-size can be used to specify the size (in bytes) of the working encryption buffer for each encryption thread (default is 64K).
使用--encrypt-chunk-size可以增加每个线程的buffer。

解密备份
In Percona XtraBackup 2.1.4 new innobackupex --decrypt option has been implemented that can be used to decrypt the backups:
在2.14及其新版中可以使用下面的版本解密

$ innobackupex --decrypt=AES256 --encrypt-key="GCHFLrDFVx6UAsRb88uLVbAVWbK+Yzfs" /data/backups/2015-03-

准备备份

Preparing Encrypted Backups After the backups have been decrypted, they can be prepared the same way as the standard full backups with the --apply-logs option:
准备备份和正常备份准备备份一样

$ innobackupex --apply-log /data/backups/2015-03-18_08-31-35/

恢复备份
Restoring Encrypted Backups innobackupex has a --copy-back option, which performs the restoration of a backup to the server’s datadir

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

高级特性

流和压缩备份

Advanced Features

xbstream
使用例子
Examples using xbstream Store the complete backup directly to a single file:

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

To stream and compress the backup:

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

To unpack the backup to the /root/backup/ directory:

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

To send the compressed backup to another host and unpack it:
发送至另外一台机器上进行远程备份

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

tar备份使用例子
Examples using tar Store the complete backup directly to a tar archive:

$ innobackupex --stream=tar /root/backup/ > /root/backup/out.tar

To send the tar archive to another host:
远程备份使用例子

$ innobackupex --stream=tar ./ | ssh user@destination \ "cat - > /data/backups/backup.tar"

Warning: To extract Percona XtraBackup‘s archive you must use tar with -i option:

解压时候需要加入-i选项

$ tar -xizf backup.tar.gz

Compress with your preferred compression tool:

$ innobackupex --stream=tar ./ | gzip - > backup.tar.gz
$ innobackupex --stream=tar ./ | bzip2 - > backup.tar.bz2

Taking Backups in Replication Environments

加速备份进程

Accelerating the backup process

Accelerating with --parallel copy and –compress-threads

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

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

备份局部的数据

Partial Backups
Percona XtraBackup features partial backups, which means that you may backup only some specific tables ordatabases. The tables you back up must be in separate tablespaces, as a result of being created or altered after youenabled the innodb_file_per_table option on the server.

table 的物理文件必须是分开的

备份使用选项

Using the --include option The regular expression provided to this will be matched against the fully qualified table name, including the database name, in the form databasename.tablename.

For example,

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

可以指定正则表达式

Using the --tables-file option The text file provided (the path) to this option can contain multiple table

names, one per line, in the databasename.tablename format.

For example,

$ echo "mydatabase.mytable" > /tmp/tables.txt

$ innobackupex --tables-file=/tmp/tables.txt /path/to/backup

使用包含table表格的文件

Using the --databases option This option accepts either a space-separated list of the databases and tables to backup - in the databasename[.tablename] form - or a file containing the list at one element per line.

For example,

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

准备时候需要加入--export选项

Preparing Partial Backups For preparing partial backups, the procedure is analogous to restoring individual tables

: apply the logs and use the --export option:

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

恢复单独的表格

Restoring Individual Tables

Exporting tables Exporting is done in the preparation stage, not at the moment of creating the backup. Once a full

backup is created, prepare it with the --export option:

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

This will create for each InnoDB with its own tablespace a file with .exp extension. An output of this procedure would contain:

..

xtrabackup: export option is specified.

xtrabackup: export metadata of table 'mydatabase/mytable' to file

./mydatabase/mytable.exp (1 indexes)

Each .exp (or .cfg) file will be used for importing that table.

Note: InnoDB does a slow shutdown (i.e. full purge + change buffer merge) on –export, otherwise the tablespaces wouldn’t be consistent and thus couldn’t be imported. All the usual performance considerations apply: sufficient buffer pool (i.e. –use-memory, 100MB by default) and fast enough

storage, otherwise it can take a prohibitive amount of time for export to complete.

Importing tables To import a table to other server, first create a new table with the same structure as the one that

will be imported at that server:

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

then discard its tablespace:

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

After this, copy mytable.ibd and mytable.exp ( or mytable.cfg if importing to MySQL 5.6) files to

database’s home, and import its tablespace:

cp mytable.cfg mytable.ibd /path/to/data

chown -R mysql:mysql /path/to/data

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

Once this is executed, data in the imported table will be available.

定点恢复

Point-In-Time recovery

首先创建一个完整的备份

For taking the snapshot, we will use innobackupex for a full backup:

$ innobackupex /path/to/backup --no-timestamp

(the --no-timestamp option is for convenience in this example) and we will prepare it to be ready for restoration:

准备备份

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

查看bin log的记录点

To find out the position of the snapshot taken, see the xtrabackup_binlog_info at the backup’s directory:

$ cat /path/to/backup/xtrabackup_binlog_info

mysql-bin.000003 57

恢复完整的备份

This will tell you which file was used at moment of the backup for the binary log and its position. That position will

be the effective one when you restore the backup:

$ innobackupex --copy-back /path/to/backup

找到bin log 使用命令产生sql语句,恢复至指定位置备份

$ mysqlbinlog /path/to/datadir/mysql-bin.000003 /path/to/datadir/mysql-bin.000004 \
--start-position=57 --stop-datetime="11-12-25 01:00:00" | mysql -u root –p

参考文件:
官方文档PerconaXtraBackup-2-2-13.pdf

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355

推荐阅读更多精彩内容