日期:2014-05-16  浏览次数:20816 次

使用Percona XtraBackup热备和恢复MySQL(一)

?

1.在http://www.percona.com/downloads/XtraBackup/LATEST/? 下载对应平台的XtraBackup,这里使用的是 http://www.percona.com/redir/downloads/XtraBackup/XtraBackup-2.0.0/binary/Linux/x86_64/percona-xtrabackup-2.0.0.tar.gz

?

2.解压tar -zvxf percona-xtrabackup-2.0.0.tar.gz -C /usr/local/

?

3.因为MySQL我安装的是Percona-Server-5.5.21,而且安装目录为:/usr/local/Percona-Server-5.5.21-rel25.0-227.Linux.x86_64/,注:同样可以用于其他MySQL版本

cd /usr/local/percona-xtrabackup-2.0.0/bin
cp * /usr/local/Percona-Server-5.5.21-rel25.0-227.Linux.x86_64/bin/

?因为系统Path里面已经加入

export PATH=$JAVA_HOME/bin:/usr/local/Percona-Server-5.5.21-rel25.0-227.Linux.x86_64/bin:$PATH

?

4.我的mysql的配置文件是/etc/my.cnf,如果不指定,XtraBackup默认使用此文件识别mysql安装目录,数据文件目录等信息

?

5.全量备份:innobackupex --user=DBUSER --password=DBUSERPASS /path/to/BACKUP-DIR/,我使用/data/backup/mysql/作为mysql备份文件存放目录

innobackupex --user=YOUS --password=YOUS /data/backup/mysql

看到类似输出说明备份成功,如出错,根据提示解决

innobackupex: Backup created in directory '/data/backup/mysql/2012-05-28_19-01-32'
innobackupex: MySQL binlog position: filename 'mysql-bin.000063', position 44718229367
120528 19:07:53  innobackupex: completed OK!

可以在/data/backup/mysql/2012-05-28_19-01-32看到备份的文件

此时,cat xtrabackup_checkpoints会看到

backup_type = full-backuped
from_lsn = 0
to_lsn = 44718229367
last_lsn = 44718229367
?

?

6.全量Preparing:innobackupex --apply-log /path/to/BACKUP-DIR

innobackupex --user=YOUS --password=YOUS --apply-log /data/backup/mysql/2012-05-28_19-01-32/

?

可以看到如下生成文件:

-rw-r--r--. 1 root root          13 May 28 19:07 xtrabackup_binary
-rw-r--r--. 1 root root          26 May 29 15:07 xtrabackup_binlog_info
-rw-r--r--. 1 root root          43 May 29 15:07 xtrabackup_binlog_pos_innodb
-rw-r-----. 1 root root          85 May 29 15:07 xtrabackup_checkpoints
-rw-r-----. 1 root root     2097152 May 29 14:03 xtrabackup_logfile

cat xtrabackup_checkpoints,可以看出是全量备份并且做了prepare的

backup_type = full-prepared
from_lsn = 0
to_lsn = 49556823920
last_lsn = 49556823920

?

7.增量备份的前提是必须已经做过全量备份。

增量备份:innobackupex --incremental /path/to/BACKUP-DIR/--incremental-basedir=BASEDIR,当有了INCREMENTAL-DIR-1之后,下一次增量备份的需要基于INCREMENTAL-DIR-1,变成innobackupex --incremental /path/to/BACKUP-DIR/ --incremental-basedir=INCREMENTAL-DIR-1

全量备份的目录是:/data/backup/mysql/2012-05-28_19-01-32

innobackupex --incremental /data/backup/mysql --incremental-basedir=/data/backup/mysql/2012-05-28_19-01-32/ --user=YOUS --password=YOUS

?

增量备份成功会生成目录/data/backup/mysql/2012-05-29_14-25-03

cat xtrabackup_checkpoints

backup_type = incremental
from_lsn = 44718229367
to_lsn = 49556823920
last_lsn = 49556823920
?

8.增量Preparing,对每一个增量备份目录:

innobackupex --apply-log --redo-only BASE-DIR --incremental-dir=INCREMENTAL-DIR-1
innobackupex --apply-log --redo-only BASE-DIR --incremental-dir=INCREMENTAL-DIR-2

innobackupex --apply-log --redo-only /data/backup/mysql/2012-05-28_19-01-32/ --incremental-dir=/data/backup/mysql/2012-05-29_14-25-03 --user=YOUS--password=YOUS

看到如下输出:

120529 14:29:43 InnoDB: Shutdown completed; log sequence number 49556823920
120529 14:29:43 innobackupex: completed OK!

cd BASE-DIR,cat xtrabackup_checkpoints

backup_type = full-prepared
from_lsn = 0
to_lsn = 49556823920
last_lsn = 49556823920

?当把所有的增量备份都执行Preparing后,还可以全量备份和全部的增量备份做一次Preparing,

innobackupex --apply-log BASE-DIR

?

9.恢