Xtrabackup工具使用简介 - 20110427

Preview:

DESCRIPTION

percona Xtrabackup工具使用简介

Citation preview

XtraBackup 使用简介

XtraBackup 使用简介

作者: 叶金荣

编写日期: 2011-04-27

目录

1 测试说明..........................................................................................................................2

2 XtraBackup 简介..........................................................................................................2

3 XtraBackup 特点..........................................................................................................3

3.1 下载 XtraBackup.......................................................................................................3

3.2 XtraBackup 使用方法...............................................................................................4

3.3 XtraBackup 配置选项详解.......................................................................................6

3.4 XtraBackup 实际案例.............................................................................................10

3.4.1 做一次数据全备.....................................................................................................10

3.4.2 做一次数据增备.....................................................................................................12

3.4.3 做一次数据恢复.....................................................................................................15

XtraBackup 使用简介

3.4.3.1 只有一次全备.....................................................................................................15

3.4.3.2 同时应用增备.....................................................................................................16

3.5 innobackup 实际案例.............................................................................................19

3.5.1 准备配置文件.........................................................................................................19

3.5.2 做一次数据全备.....................................................................................................20

3.5.3 做一次数据增备.....................................................................................................24

3.5.4 做一次数据恢复.....................................................................................................25

3.5.4.1 只有一次全备.....................................................................................................25

3.5.4.2 同时应用增备.....................................................................................................29

4 结语................................................................................................................................31

1测试说明

本次测试了 XtraBackup 这个在线热备工具的使用方法,考察其在实际生

产环境中的应用前景、可行性及方案。

XtraBackup 使用简介

2 XtraBackup简介

简言之,XtraBackup 是 Percona 推出的一个基于 MySQL XtraDB 引擎

的在线热备工具,是一个开源项目。XtraDB 引擎其实是 InnoDB 引擎的变种,

因此这个工具也是支持原生态 InnoDB 引擎的。它同时也能支持对 MyISAM 引

擎的备份,目前可支持 MySQL 5.0、5.1、5.5 等多个版本。不过目前还不支

持 InnoDB Plugin 新版本引擎。XtraBackup 项目基于 GPL V2 协议分发。

3 XtraBackup特点

XtraBackup 工具的主要特点有:

支持在线(无需加锁)备份 InnoDB/XtraDB 引擎;对于 MyISAM 引擎,

则需要加一个只读锁,当然了,也可以不加

支持数据流压缩备份,支持磁带、异地备份

支持增备

XtraBackup 软件主要有 2 个工具:

xtrabackup – 二进制文件,用于拷贝 InnoDB/XtraDB 表空间文件

XtraBackup 使用简介

innobackupex – 封装后的脚本,基于 innobackup Perl 脚本,由

Oracle/InnoDB 基于 GPL 协议分发。该脚本用于接收各种参数,然后

调用 xtrabackup 进行实际工作。该脚本提供了各种函数,用于备份

MySQL 下的 InnoDB、XtraDB、MyISAM 等多种引擎。

3.1下载XtraBackup

可以在:http://www.percona.com/downloads/XtraBackup/ 上下载

到该工具。当前最新稳定版本是 1.5,同时提供了多种格式的安装包,支持多

种操作系统,包括:Linux、Debian、FreeBSD、Solaris、Windows 等。同

时还提供源码,可自行编译,因此实际可以支持更多平台。

3.2XtraBackup使用方法

XtraBackup 一般的用法为:

备份:xtrabackup [DEFAULTS OPTION] –backup

恢复:xtrabackup [DEFAULTS OPTION] –prepare] [OPTIONS]

XtraBackup 使用简介

上面提到的 DEFAULTS OPTION 选项,默认从以下顺序的配置文件中读取:

/etc/my.cnf

/opt/mysql-5.0.xx/etc/my.cnf

~/.my.cnf

只读取配置文件的“mysqld”和“xtrabackup”部分。

另外,下面的选项列表作为 XtraBackup 的第一参数:

(The following 4 options may be given as the first argument)

--print-defaults Print the program argument list and exit

--no-defaults Don't read default options from any options file

--defaults-file=# Only read default options from the given file #

--defaults-extra-file=# Read this file after the global files are read

--target-dir=name destination directory

--backup take backup to target-dir

--stats calc statistic of datadir (offline mysqld is recommended)

--prepare prepare a backup for starting mysql server on the backup.

--export create files to import to another database when prepare.

--print-param print parameter of mysqld needed for copyback.

--use-memory=# The value is used instead of buffer_pool_size

--suspend-at-end creates a file 'xtrabackup_suspended' and waits until the

user deletes that file at the end of '--backup'

--throttle=# limit count of IO operations (pairs of read&write) per

second to IOS values (for '--backup')

--log-stream outputs the contents of 'xtrabackup_logfile' to stdout

only until the file 'xtrabackup_suspended' deleted (for

'--backup').

--incremental-lsn=name

(for --backup): copy only .ibd pages newer than specified

LSN 'high:low'. ##ATTENTION##: checkpoint lsn must be

used. anyone can detect your mistake. be carefully!

--incremental-basedir=name

(for --backup): copy only .ibd pages newer than backup at

specified directory.

--incremental-dir=name

(for --prepare): apply .delta files and logfile in the

XtraBackup 使用简介

specified directory.

--tables=name filtering by regexp for table names.

--create-ib-logfile ** not work for now** creates ib_logfile* also after

'--prepare'. ### If you want create ib_logfile*, only

re-execute this command in same options. ###

-h, --datadir=name Path to the database root.

-t, --tmpdir=name Path for temporary files. Several paths may be specified,

separated by a colon (:), in this case they are used in a

round-robin fashion.

throttle – 限制每秒的 IOS 数量,以避免瞬间对服务器性能产生较大影响。

不过,设置太小的话,可能会导致备份进度太慢(比如跟不上事务日志的更新

速度)。

target-dir – 数据备份目标目录

Datadir 选项必须在命令行或 my.cnf 配置文件的[mysqld] 中体现

可以设置 my.cnf 文件中[xtrabackup]这个分区的全部选项(如果配置

文件中,[xtrabackup]放在[mysqld]后面,则这些选项会覆盖前面的

值)。

3.3XtraBackup配置选项详解

详细的选项不再逐个解释,可自行查看。

Options for xtrabackup. The All options are read from [mysqld]

and [xtrabackup] section of my.cnf.

XtraBackup 使用简介

--datadir

[required]: This option is same for mysqld. And this shuld be

same to mysqld. So, you should set datadir at [mysqld] section of

my.cnf .

--target-dir

[required]: This option specifies the target directory to put the

backup files. If not exist, ”–backup” option creates the directory.

attention: We must use fullpath expression.

--backup

stores the backup data files (*.ibd) and a backup log file (named

'xtrabackup_logfile') in the directory specified in target-dir

--prepare

prepares a backup for starting mysqld from the backup files

stored by ”–backup” option. It applies the log records in

'xtrabackup_logfile' to the data files.

attention: In current version, you should re-execute ”–prepare”,

if you want ib_logfile* here. (This is restriction of the alpha version)

--print-param

This option is used by innobackup script. It generates target

mysqld information for “innobackup –copy-back” like this.

# This MySQL options file was generated by XtraBackup.

[mysqld]

datadir = /opt/mysql-5.0.67/data

XtraBackup 使用简介

innodb_data_home_dir = /opt/mysql-5.0.67/data

innodb_data_file_path = ibdata1:10M:autoextend

innodb_log_group_home_dir = /opt/mysql-5.0.67/data

--defaults-file=#

This option is same to the other MySQL tools. It can specify the

my.cnf only read.

--defaults-extra-file=#

This option is also same to the other MySQL tools. It can specify

the my.cnf additionally read.

--use-memory=#

This option is for ”–prepare”. This is not same to the same

named option of ibbackup. It is used as innodb_buffer_pool_size

of the InnoDB instance of ”–prepare”. The default value is 100M

bytes. But 1G or 2G bytes are recommended if you can.

--export

This option is for ”–prepare”. It makes to output “clean” .ibd files

and .exp files for 'ALTER TABLE … IMPORT TABLESPACE' command

innodb_expand_import option enabled XtraDB. .exp file is generated

at the same place of the each .ibd file

--suspend-at-end

This option is for ”–backup”. creates a file

'xtrabackup_suspended' and waits until the user deletes that file at

XtraBackup 使用简介

the end. It is used in backup scripts for synchronizing the end

position of the backup transaction log file to the another backup.

--log-stream

This option is for ”–backup”. outputs the contents of

'xtrabackup_logfile' to stdout only, –suspend-at-end is added

automatically. It may be useful when you use your own backup tool.

(innobackupex –stream mode also uses this option.)

--incremental-lsn=LSN_HIGH:LSN_LOW

This option is for ”–backup”. Copy only .ibd pages newer than

specified LSN 'high:low'.

The checkpoint LSN must be used. (The checkpoint LSN of the

previous backup is printed out when ”–backup”, and stored at

'xtrabackup_checkpoints' file)

--incremental-basedir=name

This option is for ”–backup”. Copy only .ibd pages newer than

backup at specified directory. ('xtrabackup_checkpoints' of the

directory is read and the LSN is used automatically.)

--incremental-dir=name

This option is for ”–prepare”. Apply the incremental backup files

(*.delta & xtrabackup_logfile) in the specified directory.

--tables=REGEXP

This option only backup the file-per-table datafiles whose table

XtraBackup 使用简介

name “db_name.table_name” is matching to the regular expression

REGEXP.

--throttle=#

This option is for ”–backup”. limits count of IO operations (pairs

of read&write) per second to IOS values. (At ”–incremental” mode, it

is based on rather read IO)

--create-ib-logfile

This option is for ”–prepare”. no effect for now

attention: In current version, you should re-execute ”–prepare”,

if you want ib_logfile* here.

--tmpdir

It is read from my.cnf. But it is not used by xtrabackup for now.

--innodb_*

InnoDB options for the target mysqld. Ordinarily, they are read

from my.cnf and we don't care about their settings.

XtraBackup 使用简介

3.4XtraBackup实际案例

3.4.1 做一次数据全备

[@test 20110427]# xtrabackup --target-dir=/path/20110427/ --backup --throttle=100

xtrabackup Ver 1.5 Rev undefined for 5.1.53 unknown-linux-gnu (x86_64)

xtrabackup: uses posix_fadvise().

xtrabackup: cd to /home/mysql

xtrabackup: Target instance is assumed as followings.

xtrabackup: innodb_data_home_dir = ./

xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend

xtrabackup: innodb_log_group_home_dir = ./

xtrabackup: innodb_log_files_in_group = 2

xtrabackup: innodb_log_file_size = 268435456

xtrabackup: use O_DIRECT

110427 11:43:04 InnoDB: Warning: allocated tablespace 27, old maximum was 9

>> log scanned up to (13670891)

[01] Copying ./ibdata1

to /home/databak/20110427//ibdata1

>> log scanned up to (13670891)

>> log scanned up to (13670891)

>> log scanned up to (13670891)

>> log scanned up to (13670891)

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_money_etc.ibd

to /home/databak/20110427//cyou_monitor_ldj/tb_money_etc.ibd

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_service_metric.ibd

to /home/databak/20110427//cyou_monitor_ldj/tb_service_metric.ibd

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_host_status.ibd

XtraBackup 使用简介

to /home/databak/20110427//cyou_monitor_ldj/tb_host_status.ibd

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_money_work.ibd

to /home/databak/20110427//cyou_monitor_ldj/tb_money_work.ibd

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_exp_profession.ibd

to /home/databak/20110427//cyou_monitor_ldj/tb_exp_profession.ibd

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_money_life.ibd

to /home/databak/20110427//cyou_monitor_ldj/tb_money_life.ibd

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_service_status.ibd

to /home/databak/20110427//cyou_monitor_ldj/tb_service_status.ibd

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_service_metric_data.ibd

to /home/databak/20110427//cyou_monitor_ldj/tb_service_metric_data.ibd

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_host.ibd

to /home/databak/20110427//cyou_monitor_ldj/tb_host.ibd

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_exp_pet.ibd

to /home/databak/20110427//cyou_monitor_ldj/tb_exp_pet.ibd

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_service.ibd

to /home/databak/20110427//cyou_monitor_ldj/tb_service.ibd

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_exp_player.ibd

to /home/databak/20110427//cyou_monitor_ldj/tb_exp_player.ibd

[01] ...done

xtrabackup: The latest check point (for incremental): '6742998'

>> log scanned up to (13670891)

xtrabackup: Stopping log copying thread.

xtrabackup: Transaction log of lsn (13670891) to (13670891) was copied.

可以看到,默认地,把所有的 InnoDB 引擎表备份到指定目录下,同时记

XtraBackup 使用简介

录了最后的事务编号,当前是:13670891。便于下一次做增备,以及后续的

数据恢复。

3.4.2 做一次数据增备

[@test 20110427]# xtrabackup --backup --throttle=100 \

--target-dir=/home/databak/20110427/2011042712/ \

--incremental-basedir=/home/databak/20110427 \

xtrabackup Ver 1.5 Rev undefined for 5.1.53 unknown-linux-gnu (x86_64)

incremental backup from 13670891 is enabled.

xtrabackup: uses posix_fadvise().

xtrabackup: cd to /home/mysql

xtrabackup: Target instance is assumed as followings.

xtrabackup: innodb_data_home_dir = ./

xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend

xtrabackup: innodb_log_group_home_dir = ./

xtrabackup: innodb_log_files_in_group = 2

xtrabackup: innodb_log_file_size = 268435456

xtrabackup: use O_DIRECT

110427 11:52:13 InnoDB: Warning: allocated tablespace 27, old maximum was 9

>> log scanned up to (13670891)

[01] Copying ./ibdata1

to /home/databak/20110427/2011042712//ibdata1.delta

>> log scanned up to (13670891)

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_money_etc.ibd

to

/home/databak/20110427/2011042712//cyou_monitor_ldj/tb_money_etc.ibd.delta

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_service_metric.ibd

to

/home/databak/20110427/2011042712//cyou_monitor_ldj/tb_service_metric.ibd.delta

XtraBackup 使用简介

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_host_status.ibd

to

/home/databak/20110427/2011042712//cyou_monitor_ldj/tb_host_status.ibd.delta

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_money_work.ibd

to

/home/databak/20110427/2011042712//cyou_monitor_ldj/tb_money_work.ibd.delta

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_exp_profession.ibd

to

/home/databak/20110427/2011042712//cyou_monitor_ldj/tb_exp_profession.ibd.delta

>> log scanned up to (13719595)

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_money_life.ibd

to

/home/databak/20110427/2011042712//cyou_monitor_ldj/tb_money_life.ibd.delta

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_service_status.ibd

to

/home/databak/20110427/2011042712//cyou_monitor_ldj/tb_service_status.ibd.delta

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_service_metric_data.ibd

to

/home/databak/20110427/2011042712//cyou_monitor_ldj/tb_service_metric_data.ibd.delta

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_host.ibd

to /home/databak/20110427/2011042712//cyou_monitor_ldj/tb_host.ibd.delta

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_exp_pet.ibd

to /home/databak/20110427/2011042712//cyou_monitor_ldj/tb_exp_pet.ibd.delta

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_service.ibd

to /home/databak/20110427/2011042712//cyou_monitor_ldj/tb_service.ibd.delta

[01] ...done

[01] Copying ./cyou_monitor_ldj/tb_exp_player.ibd

to /home/databak/20110427/2011042712//cyou_monitor_ldj/tb_exp_player.ibd.delta

XtraBackup 使用简介

[01] ...done

xtrabackup: The latest check point (for incremental): '13724040'

>> log scanned up to (13724040)

xtrabackup: Stopping log copying thread..

xtrabackup: Transaction log of lsn (13670891) to (13724040) was copied.

可以看到,增备数据的事务编号更新到了:13724040。

3.4.3 做一次数据恢复

3.4.3.1 只有一次全备

[@test 20110427]# xtrabackup --prepare --target-dir=/home/databak/20110427

xtrabackup Ver 1.5 Rev undefined for 5.1.53 unknown-linux-gnu (x86_64)

xtrabackup: cd to /home/databak/20110427

xtrabackup: This target seems to be already prepared.

xtrabackup: notice: xtrabackup_logfile was already used to '--prepare'.

xtrabackup: Temporary instance for recovery is set as followings.

xtrabackup: innodb_data_home_dir = ./

xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend

xtrabackup: innodb_log_group_home_dir = ./

xtrabackup: innodb_log_files_in_group = 2

xtrabackup: innodb_log_file_size = 268435456

xtrabackup: Starting InnoDB instance for recovery.

xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)

InnoDB: The InnoDB memory heap is disabled

InnoDB: Mutexes and rw_locks use GCC atomic builtins

InnoDB: Compressed tables use zlib 1.2.3

InnoDB: Warning: innodb_file_io_threads is deprecated. Please use

innodb_read_io_threads and innodb_write_io_threads instead

mysqladmin pr110428 15:45:26 InnoDB: Log file ./ib_logfile0 did not exist: new to be

created

XtraBackup 使用简介

InnoDB: Setting log file ./ib_logfile0 size to 256 MB

InnoDB: Database physically writes the file full: wait...

InnoDB: Progress in MB: 100 200

110428 15:45:28 InnoDB: Log file ./ib_logfile1 did not exist: new to be created

InnoDB: Setting log file ./ib_logfile1 size to 256 MB

InnoDB: Database physically writes the file full: wait...

InnoDB: Progress in MB: 100 200

110428 15:45:29 InnoDB: highest supported file format is Barracuda.

InnoDB: The log sequence number in ibdata files does not match

InnoDB: the log sequence number in the ib_logfiles!

110428 15:45:29 InnoDB: Database was not shut down normally!

InnoDB: Starting crash recovery.

InnoDB: Reading tablespace information from the .ibd files...

InnoDB: Last MySQL binlog file position 0 6639763, file name

/home/binlog/binlog.000009

110428 15:45:30 Percona XtraDB (http://www.percona.com) 1.0.13-12.4 started; log

sequence number 13670891

[notice (again)]

If you use binary log and don't use any hack of group commit,

the binary log position seems to be:

InnoDB: Last MySQL binlog file position 0 6639763, file name

/home/binlog/binlog.000009

xtrabackup: starting shutdown with innodb_fast_shutdown = 1

110428 15:45:30 InnoDB: Starting shutdown...

110428 15:45:31 InnoDB: Shutdown completed; log sequence number 13670891

可 以 看 到 , 如 果 只 应 用 全 备 数 据 的 话 ,那么事 务 序 号 只 恢 复 到 了 :

13670891,而不是最新的:13724040。

XtraBackup 使用简介

3.4.3.2 同时应用增备

恢复增备数据时,要先对全备数据做一次应用,再做一次增备数据应用。

#先应用全备数据

[@test 20110427]# cd /home/databak/20110427

[@test 20110427]# xtrabackup --prepare --target-dir=./

xtrabackup Ver 1.5 Rev undefined for 5.1.53 unknown-linux-gnu (x86_64)

xtrabackup: cd to ./

xtrabackup: This target seems to be not prepared yet.

xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(6742998)

xtrabackup: Temporary instance for recovery is set as followings.

xtrabackup: innodb_data_home_dir = ./

xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend

xtrabackup: innodb_log_group_home_dir = ./

xtrabackup: innodb_log_files_in_group = 1

xtrabackup: innodb_log_file_size = 2097152

xtrabackup: Starting InnoDB instance for recovery.

xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)

InnoDB: The InnoDB memory heap is disabled

InnoDB: Mutexes and rw_locks use GCC atomic builtins

InnoDB: Compressed tables use zlib 1.2.3

InnoDB: Warning: innodb_file_io_threads is deprecated. Please use

innodb_read_io_threads and innodb_write_io_threads instead

110427 13:50:05 InnoDB: highest supported file format is Barracuda.

InnoDB: The log sequence number in ibdata files does not match

InnoDB: the log sequence number in the ib_logfiles!

110427 13:50:05 InnoDB: Database was not shut down normally!

InnoDB: Starting crash recovery.

InnoDB: Reading tablespace information from the .ibd files...

InnoDB: Last MySQL binlog file position 0 6591484, file name

/home/binlog/binlog.000009

110427 13:50:05 Percona XtraDB (http://www.percona.com) 1.0.13-12.4 started; log

sequence number 13670891

XtraBackup 使用简介

[notice (again)]

If you use binary log and don't use any hack of group commit,

the binary log position seems to be:

InnoDB: Last MySQL binlog file position 0 6591484, file name

/home/binlog/binlog.000009

xtrabackup: starting shutdown with innodb_fast_shutdown = 1

110427 13:50:05 InnoDB: Starting shutdown...

110427 13:50:06 InnoDB: Shutdown completed; log sequence number 13670891

#再次应用增备数据

[@test 20110427]# xtrabackup --prepare --target-dir=./

--incremental-dir=./2011042712/

xtrabackup Ver 1.5 Rev undefined for 5.1.53 unknown-linux-gnu (x86_64)

incremental backup from 13670891 is enabled. #增备开始恢复的点

xtrabackup: cd to ./

xtrabackup: This target seems to be already prepared.

xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(13670891)

#应用增备数据

Applying ./2011042712//ibdata1.delta ...

Applying ./2011042712//cyou_monitor_ldj/tb_exp_pet.ibd.delta ...

Applying ./2011042712//cyou_monitor_ldj/tb_exp_player.ibd.delta ...

Applying ./2011042712//cyou_monitor_ldj/tb_exp_profession.ibd.delta ...

Applying ./2011042712//cyou_monitor_ldj/tb_host.ibd.delta ...

Applying ./2011042712//cyou_monitor_ldj/tb_money_work.ibd.delta ...

Applying ./2011042712//cyou_monitor_ldj/tb_service_metric.ibd.delta ...

Applying ./2011042712//cyou_monitor_ldj/tb_money_life.ibd.delta ...

Applying ./2011042712//cyou_monitor_ldj/tb_money_etc.ibd.delta ...

Applying ./2011042712//cyou_monitor_ldj/tb_service.ibd.delta ...

Applying ./2011042712//cyou_monitor_ldj/tb_service_metric_data.ibd.delta ...

Applying ./2011042712//cyou_monitor_ldj/tb_host_status.ibd.delta ...

Applying ./2011042712//cyou_monitor_ldj/tb_service_status.ibd.delta ...

xtrabackup: Temporary instance for recovery is set as followings.

xtrabackup: innodb_data_home_dir = ./

XtraBackup 使用简介

xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend

xtrabackup: innodb_log_group_home_dir = ./2011042712/

xtrabackup: innodb_log_files_in_group = 1

xtrabackup: innodb_log_file_size = 2097152

xtrabackup: Starting InnoDB instance for recovery.

xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)

InnoDB: The InnoDB memory heap is disabled

InnoDB: Mutexes and rw_locks use GCC atomic builtins

InnoDB: Compressed tables use zlib 1.2.3

InnoDB: Warning: innodb_file_io_threads is deprecated. Please use

innodb_read_io_threads and innodb_write_io_threads instead

110427 13:50:12 InnoDB: highest supported file format is Barracuda.

InnoDB: The log sequence number in ibdata files does not match

InnoDB: the log sequence number in the ib_logfiles!

110427 13:50:12 InnoDB: Database was not shut down normally!

InnoDB: Starting crash recovery.

InnoDB: Reading tablespace information from the .ibd files...

InnoDB: Last MySQL binlog file position 0 6639763, file name

/home/binlog/binlog.000009

110427 13:50:12 Percona XtraDB (http://www.percona.com) 1.0.13-12.4 started; log

sequence number 13724040 #应用到最新的日志点

[notice (again)]

If you use binary log and don't use any hack of group commit,

the binary log position seems to be:

InnoDB: Last MySQL binlog file position 0 6639763, file name

/home/binlog/binlog.000009

xtrabackup: starting shutdown with innodb_fast_shutdown = 1

110427 13:50:12 InnoDB: Starting shutdown...

110427 13:50:13 InnoDB: Shutdown completed; log sequence number 13724040

可以看到,这时候事务日志点更新到了:13724040,已经是最新的了。

XtraBackup 使用简介

3.5innobackup实际案例

3.5.1 准备配置文件

Xtrabackup 可以预先读取配置文件,根据配置文件定义的规则执行备份,

下面是一个配置文件的案例:

#

#xtrabackup / innobackupex

#

[client]

socket = /home/mysql_DBPORT/mysql.sock

[xtrabackup]

#设定数据源

datadir = /home/mysql_DBPORT

user = mysql

socket = /home/mysql_DBPORT/mysql.sock

innodb_data_file_path = ibdata1:1G:autoextend

transaction_isolation = READ-COMMITTED

innodb_log_file_size = 256M

innodb_log_files_in_group = 2

#设定要备份的数据表,正则表达,其余的忽略,不做备份

tables="cy_.*|c_char_cyou|c_gm_list"

XtraBackup 使用简介

3.5.2 做一次数据全备

innobackupex \

#指定读取的配置文件路径

--defaults-file=/home/databak/3001/xtrabackup.cnf \

#创建的目录不带时间戳

--no-timestamp \

#并发 1 个线程运行

--parallel=1 \

#设备备份时,每秒 IO 上限是 100,防止系统 IO负载过高

--throttle=100 \

#同时备份 slave信息

--slave-info \

#备份时,会先执行 stop slave,确保能获取最准确的 slave信息

--safe-slave-backup \

#不锁表,针对 myisam 表引擎

--no-lock \

#指定备份文件存储目标路径

/home/databak/3001/20110715

#开始发起备份

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy

and Percona Inc 2009-2011. All Rights Reserved.

This software is published under

the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.

XtraBackup 使用简介

#开始准备,连接到 mysql 中,查看 binlog,slave 等信息

110715 19:34:10 innobackupex: Starting mysql with options:

--defaults-file='/home/databak/bm_3307/xtrabackup.cnf' --unbuffered --

110715 19:34:10 innobackupex: Connected to database with mysql child process

(pid=27240)

110715 19:34:16 innobackupex: Connection to database server closed

IMPORTANT: Please check that the backup run completes successfully.

At the end of a successful backup run innobackupex

prints "completed OK!".

innobackupex: Using Logging to file '/home/mysql/query.log'

innobackupex: Using mysql server version Copyright 2000-2008 MySQL AB, 2008 Sun

Microsystems, Inc.

#确定要备份的数据表清单

xtrabackup: tables regcomp(ip_blacklist|ip_limitdef|ip_whitelist|ip_logs|ip_entity_task|

ip_entity_login_7): Success

innobackupex: Created backup directory /home/databak/test/20110715

110715 19:34:16 innobackupex: Starting mysql with options:

--defaults-file='/home/databak/bm_3307/xtrabackup.cnf' --unbuffered --

110715 19:34:16 innobackupex: Connected to database with mysql child process

(pid=27263)

110715 19:34:20 innobackupex: Connection to database server closed

110715 19:34:20 innobackupex: Starting ibbackup with command: xtrabackup --defaults-

file="/home/databak/bm_3307/xtrabackup.cnf" --backup --suspend-at-end

--target-dir=/home/databak/test/20110715 --throttle=100

innobackupex: Waiting for ibbackup (pid=27279) to suspend

innobackupex: Suspend file '/home/databak/test/20110715/xtrabackup_suspended'

#开始备份共享表空间,备份各 InnoDB 表,第一次记录 redo log信息

xtrabackup: tables regcomp(ip_blacklist|ip_limitdef|ip_whitelist|ip_logs|ip_entity_task|

ip_entity_login_7): Success

xtrabackup Ver 1.6 Rev 245 for 5.1.55 unknown-linux-gnu (x86_64)

xtrabackup: uses posix_fadvise().

xtrabackup: cd to /home/mysql_3307

xtrabackup: Target instance is assumed as followings.

XtraBackup 使用简介

xtrabackup: innodb_data_home_dir = ./

xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend

xtrabackup: innodb_log_group_home_dir = ./

xtrabackup: innodb_log_files_in_group = 2

xtrabackup: innodb_log_file_size = 268435456

110715 19:34:20 InnoDB: Warning: allocated tablespace 10, old maximum was 9

>> log scanned up to (13919335100)

[01] Copying ./ibdata1

to /home/databak/test/20110715/ibdata1

>> log scanned up to (13919335366)

>> log scanned up to (13919335366)

[01] ...done

[01] Copying ./billing_mother/ip_blacklist.ibd

to /home/databak/test/20110715/billing_mother/ip_blacklist.ibd

[01] ...done

[01] ...done

[01] Copying ./bm_tlbb/ip_whitelist.ibd

to /home/databak/test/20110715/bm_tlbb/ip_whitelist.ibd

[01] ...done

#开始备份其他非 InnoDB 表

110715 19:34:58 innobackupex: Continuing after ibbackup has suspended

110715 19:34:58 innobackupex: Starting mysql with options:

--defaults-file='/home/databak/bm_3307/xtrabackup.cnf' --unbuffered --

110715 19:34:58 innobackupex: Connected to database with mysql child process

(pid=27303)

>> log scanned up to (13919335641)

>> log scanned up to (13919335641)

innobackupex:: Slave open temp tables: 0

110715 19:35:08 innobackupex: Starting to backup .frm, .MRG, .MYD, .MYI,

innobackupex: .TRG, .TRN, .ARM, .ARZ, .CSM, .CSV and .opt files in

innobackupex: subdirectories of '/home/mysql_3307'

innobackupex: Backing up files '/home/mysql_3307/mysql/*.

{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (69 files)

innobackupex: Backing up files '/home/mysql_3307/billing_mother/*.

{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (18 files)

XtraBackup 使用简介

innobackupex: Backing up files '/home/mysql_3307/bm_dj/*.

{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (18 files)

innobackupex: Backing up files '/home/mysql_3307/bm_dj1_xyj/*.

{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (18 files)

innobackupex: Backing up files '/home/mysql_3307/bm_dj1_zwj/*.

{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (18 files)

innobackupex: Backing up files '/home/mysql_3307/bm_tlbb/*.

{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (18 files)

110715 19:35:09 innobackupex: Finished backing

up .frm, .MRG, .MYD, .MYI, .TRG, .TRN, .ARM, .ARZ, .CSV, .CSM and .opt files

innobackupex: Resuming ibbackup

#第二次记录 redo log信息,记录 binlog信息

xtrabackup: The latest check point (for incremental): '13919335641'

>> log scanned up to (13919335641)

xtrabackup: Stopping log copying thread.

xtrabackup: Transaction log of lsn (13919335100) to (13919335641) was copied.

innobackupex:: Starting slave SQL thread

110715 19:35:11 innobackupex: Connection to database server closed

innobackupex: Backup created in directory '/home/databak/test/20110715'

110715 19:35:11 innobackupex: completed OK!

至此,innobackupex 做数据库全备完毕。

查看备份前后 redo log 的 checkpoint信息:

backup_type = full-backuped

from_lsn = 0

to_lsn = 13919335641

last_lsn = 13919335641

可以看到,备份期间 checkpoint 一直未发生变化。

XtraBackup 使用简介

3.5.3 做一次数据增备

执行增备和全备类似,只需要增加指定全备数据的路径即可。例如:

innobackupex \--defaults-file=/home/databak/bm_3307/xtrabackup.cnf \--no-timestamp \--parallel=1 \--throttle=100 \--slave-info \--safe-slave-backup \--no-lock \

#设定本次备份类型是增备,同时增备文件保存的路径

--incremental /home/databak/test/20110715/2011071518 \

#设定全备文件的路径

--incremental-basedir=/home/databak/test/20110715

3.5.4 做一次数据恢复

3.5.4.1 只有一次全备

做数据恢复时,和用 xtrabackup 备份的文件恢复相似,还更简单。

#1. 恢复全备

#直接进入备份文件所在目录,执行 –apply-log 操作即可

[@tc_19_66 20110714]# cd /home/databak/20110714

[@tc_19_66 20110714]# innobackupex --apply-log .

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy

and Percona Inc 2009-2011. All Rights Reserved.

XtraBackup 使用简介

This software is published under

the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.

IMPORTANT: Please check that the apply-log run completes successfully.

At the end of a successful apply-log run innobackupex

prints "completed OK!".

110714 17:46:48 innobackupex: Starting ibbackup with command: xtrabackup --prepare --

target-dir=/opt/home/databak/20110714

xtrabackup Ver 1.6 Rev undefined for 5.1.55 unknown-linux-gnu (x86_64)

xtrabackup: cd to /opt/home/databak/20110714

xtrabackup: This target seems to be not prepared yet.

xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(13916925521)

xtrabackup: Temporary instance for recovery is set as followings.

xtrabackup: innodb_data_home_dir = ./

xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend

xtrabackup: innodb_log_group_home_dir = ./

xtrabackup: innodb_log_files_in_group = 1

xtrabackup: innodb_log_file_size = 2097152

xtrabackup: Starting InnoDB instance for recovery.

xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)

InnoDB: The InnoDB memory heap is disabled

InnoDB: Mutexes and rw_locks use GCC atomic builtins

InnoDB: Compressed tables use zlib 1.2.3

InnoDB: Warning: innodb_file_io_threads is deprecated. Please use innodb_read_io_threads

and innodb_write_io_threads instead

110714 17:46:49 InnoDB: Initializing buffer pool, size = 100.0M

110714 17:46:49 InnoDB: Completed initialization of buffer pool

110714 17:46:49 InnoDB: highest supported file format is Barracuda.

InnoDB: Log scan progressed past the checkpoint lsn 13916925521

110714 17:46:49 InnoDB: Database was not shut down normally!

InnoDB: Starting crash recovery.

InnoDB: Reading tablespace information from the .ibd files...

InnoDB: Doing recovery: scanned up to log sequence number 13916926358 (0 %)

XtraBackup 使用简介

110714 17:46:49 InnoDB: Starting an apply batch of log records to the database...

InnoDB: Progress in percents: 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

90 91 92 93 94 95 96 97 98 99

InnoDB: Apply batch completed

InnoDB: In a MySQL replication slave the last master binlog file

InnoDB: position 0 480763006, file name bm153_binlog.000032

InnoDB: and relay log file

InnoDB: position 0 480763154, file name /home/binlog/bm_relay_3307.000046

InnoDB: Last MySQL binlog file position 0 279128696, file name

/home/binlog/bm_binlog_3307.000015

[notice (again)]

If you use binary log and don't use any hack of group commit,

the binary log position seems to be:

InnoDB: Last MySQL binlog file position 0 279128696, file name

/home/binlog/bm_binlog_3307.000015

xtrabackup: starting shutdown with innodb_fast_shutdown = 1

110714 17:46:50 InnoDB: Starting shutdown...

110714 17:46:50 InnoDB: Shutdown completed; log sequence number 13917110931

110714 17:46:50 innobackupex: Restarting xtrabackup with command: xtrabackup --

prepare --target-dir=/opt/home/databak/20110714

for creating ib_logfile*

xtrabackup Ver 1.6 Rev undefined for 5.1.55 unknown-linux-gnu (x86_64)

xtrabackup: cd to /opt/home/databak/20110714

xtrabackup: This target seems to be already prepared.

xtrabackup: notice: xtrabackup_logfile was already used to '--prepare'.

xtrabackup: Temporary instance for recovery is set as followings.

xtrabackup: innodb_data_home_dir = ./

xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend

xtrabackup: innodb_log_group_home_dir = ./

xtrabackup: innodb_log_files_in_group = 2

xtrabackup: innodb_log_file_size = 268435456

xtrabackup: Starting InnoDB instance for recovery.

xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)

XtraBackup 使用简介

InnoDB: The InnoDB memory heap is disabled

InnoDB: Mutexes and rw_locks use GCC atomic builtins

InnoDB: Compressed tables use zlib 1.2.3

InnoDB: Warning: innodb_file_io_threads is deprecated. Please use innodb_read_io_threads

and innodb_write_io_threads instead

110714 17:46:50 InnoDB: Initializing buffer pool, size = 100.0M

110714 17:46:50 InnoDB: Completed initialization of buffer pool

110714 17:46:50 InnoDB: Log file ./ib_logfile0 did not exist: new to be created

InnoDB: Setting log file ./ib_logfile0 size to 256 MB

InnoDB: Database physically writes the file full: wait...

InnoDB: Progress in MB: 100 200

110714 17:46:52 InnoDB: Log file ./ib_logfile1 did not exist: new to be created

InnoDB: Setting log file ./ib_logfile1 size to 256 MB

InnoDB: Database physically writes the file full: wait...

InnoDB: Progress in MB: 100 200

110714 17:46:54 InnoDB: highest supported file format is Barracuda.

InnoDB: The log sequence number in ibdata files does not match

InnoDB: the log sequence number in the ib_logfiles!

110714 17:46:54 InnoDB: Database was not shut down normally!

InnoDB: Starting crash recovery.

InnoDB: Reading tablespace information from the .ibd files...

InnoDB: In a MySQL replication slave the last master binlog file

InnoDB: position 0 480763006, file name bm153_binlog.000032

InnoDB: and relay log file

InnoDB: position 0 480763154, file name /home/binlog/bm_relay_3307.000046

InnoDB: Last MySQL binlog file position 0 279128696, file name

/home/binlog/bm_binlog_3307.000015

110714 17:46:55 Percona XtraDB (http://www.percona.com) 1.0.15-12.5 started; log

sequence number 13917111308

[notice (again)]

If you use binary log and don't use any hack of group commit,

the binary log position seems to be:

InnoDB: Last MySQL binlog file position 0 279128696, file name

/home/binlog/bm_binlog_3307.000015

xtrabackup: starting shutdown with innodb_fast_shutdown = 1

XtraBackup 使用简介

110714 17:46:55 InnoDB: Starting shutdown...

110714 17:46:55 InnoDB: Shutdown completed; log sequence number 13917111308

110714 17:46:55 innobackupex: completed OK!

3.5.4.2 同时应用增备

#2. 恢复增备

#进入全备文件所在目录,也是执行—apply-log,同时指定增备文件对应的路径即可

[@tc_19_66 20110714]# cd /home/databak/20110714

[@tc_19_66 20110714]# innobackupex --apply-log /home/databak/20110714 --

incremental-dir=/home/databak/20110714/2011071415/

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy

and Percona Inc 2009-2011. All Rights Reserved.

This software is published under

the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.

IMPORTANT: Please check that the apply-log run completes successfully.

At the end of a successful apply-log run innobackupex

prints "completed OK!".

110714 19:50:30 innobackupex: Starting ibbackup with command: xtrabackup --prepare --

target-dir=/home/databak/20110714

--incremental-dir=/home/databak/20110714/2011071415/

xtrabackup Ver 1.6 Rev undefined for 5.1.55 unknown-linux-gnu (x86_64)

incremental backup from 13916926358 is enabled.

xtrabackup: cd to /home/databak/20110714

xtrabackup: This target seems to be already prepared.

xtrabackup: xtrabackup_logfile detected: size=2359296, start_lsn=(13917151324)

xtrabackup: page size for /home/databak/20110714/2011071415//ibdata1.delta is 16384

bytes

XtraBackup 使用简介

Applying /home/databak/20110714/2011071415//ibdata1.delta ...

...

xtrabackup: page size for

/home/databak/20110714/2011071415//billing_mother/ip_entity_task.ibd.delta is 16384

bytes

Applying /home/databak/20110714/2011071415//billing_mother/ip_entity_task.ibd.delta ...

xtrabackup: Temporary instance for recovery is set as followings.

xtrabackup: innodb_data_home_dir = ./

xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend

xtrabackup: innodb_log_group_home_dir = /home/databak/20110714/2011071415/

xtrabackup: innodb_log_files_in_group = 1

xtrabackup: innodb_log_file_size = 2359296

xtrabackup: Starting InnoDB instance for recovery.

xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)

InnoDB: The InnoDB memory heap is disabled

InnoDB: Mutexes and rw_locks use GCC atomic builtins

InnoDB: Compressed tables use zlib 1.2.3

InnoDB: Warning: innodb_file_io_threads is deprecated. Please use innodb_read_io_threads

and innodb_write_io_threads instead

110714 19:50:31 InnoDB: Initializing buffer pool, size = 100.0M

110714 19:50:31 InnoDB: Completed initialization of buffer pool

110714 19:50:31 InnoDB: highest supported file format is Barracuda.

InnoDB: Log scan progressed past the checkpoint lsn 13917151324

110714 19:50:31 InnoDB: Database was not shut down normally!

InnoDB: Starting crash recovery.

InnoDB: Reading tablespace information from the .ibd files...

InnoDB: Doing recovery: scanned up to log sequence number 13917151604 (0 %)

110714 19:50:31 InnoDB: Starting an apply batch of log records to the database...

InnoDB: Progress in percents: 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

InnoDB: Apply batch completed

InnoDB: In a MySQL replication slave the last master binlog file

InnoDB: position 0 481059264, file name bm153_binlog.000032

InnoDB: and relay log file

InnoDB: position 0 481059412, file name /home/binlog/bm_relay_3307.000046

InnoDB: Last MySQL binlog file position 0 279409214, file name

/home/binlog/bm_binlog_3307.000015

XtraBackup 使用简介

110714 19:50:31 Percona XtraDB (http://www.percona.com) 1.0.15-12.5 started; log

sequence number 13917151604

[notice (again)]

If you use binary log and don't use any hack of group commit,

the binary log position seems to be:

InnoDB: Last MySQL binlog file position 0 279409214, file name

/home/binlog/bm_binlog_3307.000015

xtrabackup: starting shutdown with innodb_fast_shutdown = 1

110714 19:50:31 InnoDB: Starting shutdown...

110714 19:50:32 InnoDB: Shutdown completed; log sequence number 13917151604

110714 19:50:32 innobackupex: completed OK!

最后,将恢复出来的文件拷贝到 MySQL 指定的 datadir 下,即可启动

mysqld 服务了。

4结语

从目前来看,由于 XtraBackup 第一次做数据全备时,会读取全部在线数

据库表空间文件,会对性能影响较大,因此不适合在只有单机且业务量、数据

量都较大的情景中使用,较适合在部署双机热备环境的从库下使用。可以定期

做一次全备,再周期性做一次增备,同时还有 binlog 等辅助备份,基本上足以

保证数据的安全性了。

Recommended