MySQL备份工具Xtrabackup

时间:2022-08-31 17:13:04
Xtrabackup:MySQL DBA的必备工具   Xtrabackuppercona开发的产品,可以看做是InnoDB Hotbackup的免费替代品。

先看看如何安装Xtrabackup,
下载最新的版本,最简单的安装方式无疑是使用RPM包,不过如果你想使用源代码方式安装的话,则会发现其安装方式有点古怪,这是因为它采用的在MySQL源代码上打补丁构建的方式。

wget http://www.percona.com/mysql/xtrabackup/xtrabackup-0.8.tar.gz
tar zxf xtrabackup-0.8.tar.gz
cd xtrabackup-0.8
./configure
make

进行到这里时,千万别惯性使然接着make install,那样就会接着安装MySQL了,正确方法是接着:

cd innobase/xtrabackup/
make
make install

如此一来,就会在你的/usr/bin目录里安装上两个有用的工具:xtrabackup,innobackupex-1.5.1:

xtrabackup可以在不加锁的情况下备份innodb数据表,不过此工具不能操作myisam。
innobackupex-1.5.1是一个脚本封装,能同时处理innodb和myisam,但在处理myisam时需要加一个读锁。

按如上的介绍,由于操作myisam时需要加读锁,这会堵塞线上服务的写操作,而innodb没有这样的限制,所以数据库中innodb表类型所占的比例越大,则越有利。实际应用中一般是直接使用innobackupex-1.5.1方法,它主要有三种操作方式,按手册中的介绍:

Usage:
innobackup [--sleep=MS] [--compress[=LEVEL]] [--include=REGEXP] [--user=NAME]
           [--password=WORD] [--port=PORT] [--socket=SOCKET] [--no-timestamp]
           [--ibbackup=IBBACKUP-BINARY] [--slave-info] [--stream=tar]
           [--defaults-file=MY.CNF]
           [--databases=LIST] [--remote-host=HOSTNAME] BACKUP-ROOT-DIR
innobackup --apply-log [--use-memory=MB] [--uncompress] [--defaults-file=MY.CNF]
           [--ibbackup=IBBACKUP-BINARY] BACKUP-DIR
innobackup --copy-back [--defaults-file=MY.CNF] BACKUP-DIR

第一个命令行是热备份mysql数据库。

The first command line above makes a hot backup of a MySQL database.
By default it creates a backup directory (named by the current date
and time) in the given backup root directory. With the --no-timestamp
option it does not create a time-stamped backup directory, but it puts
the backup in the given directory (which must not exist). This
command makes a complete backup of all MyISAM and InnoDB tables and
indexes in all databases or in all of the databases specified with the
--databases option. The created backup contains .frm, .MRG, .MYD,
.MYI., .TRG, .TRN, .opt, and InnoDB data and log files. The MY.CNF
options file defines the location of the database. This command
connects to the MySQL server using mysql client program, and runs
ibbackup (InnoDB Hot Backup program) as a child process.

带有--apply-log选项的命令是准备在一个备份上启动mysql服务。

The command with --apply-log option prepares a backup for starting a MySQL
server on the backup. This command expands InnoDB data files as specified
in BACKUP-DIR/backup-my.cnf using BACKUP-DIR/ibbackup_logfile,
and creates new InnoDB log files as specified in BACKUP-DIR/backup-my.cnf.
The BACKUP-DIR should be a path name of a backup directory created by
innobackup. This command runs ibbackup as a child process, but it does not
connect to the database server.

带有--copy-back选项的命令从备份目录拷贝数据,索引,日志到my.cnf文件里规定的初始位置。

The command with --copy-back option copies data, index, and log files
from backup directory back to their original locations.
The MY.CNF options file defines the original location of the database.
The BACKUP-DIR is a path name of a backup directory created by innobackup.

Xtrabackup还可以用来
moving InnoDB tables between servers,更多的内容可以参考官方文档例子

参考链接:
Xtrabackup online backup for InnoDB/XTraDB(pdf)