Professional Documents
Culture Documents
The XtraBackup tool is an open-source alternative to the venerable InnoDB Hot Backup tool that
is a program developed by Innobase Oy. InnoDB Hot Backup is a quality tool that allows an
administrator to create consistent backups of a running server without causing major system
slowdowns due to tables being locked. In addition to duplicating the functionality of InnoDB Hot
Backup, XtraBackup aims to bring additional features and functionality that will make the
database administrator’s life even easier.
There are two components of XtraBackup: xtrabackup and innobackupex. The xtrabackup
component is used to manage the actual backup of the InnoDB datafiles. The innobackupex
component is a Perl script that acts as a wrapper to xtrabackup and also manages MyISAM
tables, the InnoDB .frm files, triggers and views.
Hot backups with XtraBackup can be run with MySQL® Server versions 5.0 and 5.1 and any
version of InnoDB (5.0, 5.1, 1.0-plugin) or the XtraDB storage engine. MyISAM tables can also be
backed up although a read lock is placed on the MyISAM tables for the duration of the copy. This
means that users could be blocked from accessing these tables during the backup.
Limitations
The limitations of XtraBackup are very few and can be managed in most production setups. As
previously noted only InnoDB and XtraDB tables can be backed up without blocking modifications
to the tables being backed up.
New Features
In addition to implementing a non-blocking, consistent backup of InnoDB tables, XtraBackup
currently adds three major additional features over the InnoDB Hot Backup tool it is modeled
upon:
• Streaming backups
• I/O throttling
• Incremental backups
A streaming backup allows a server to push the backup files to another server for storage. This
can potentially solve two problems: the server being backed up does not have enough storage
space to save the backup and the need for remote storage to satisfy disaster recovery
requirements. Another use for streaming backups is the swift configuration of a server as a slave
or for development and testing.
While XtraBackup does not block connections from interacting with the database(s) being backed
up, the backup itself can cause problems because of its use of server I/O. To help resolve this
problem XtraBackup can throttle the amount of I/O used by the backup program.
The addition of incremental backups is a great feature to reduce your storage needs. For
example, many backup plans consist of a full backup one a week and then nightly incremental
backups for the other six days of the week. With a large database this can save a tremendous
amount of space and yet give you the security of knowing that backups are being performed
every night.
Installation of XtraBackup
XtraBackup is available as either source code or binaries. Binaries are available at
http://www.percona.com/mysql/xtrabackup. Currently the binaries cover Red Hat Enterprise
Linux versions four and five, a Debian package, FreeBSD, OS X and a generic GNU/Linux binary.
All are 64-bit versions. Installation is as simple as installing the appropriate package, or
unzipping the binary and copying the files to a good location.
Source compilation
Compiling the XtraBackup program is fairly easy, although a little different than a typical
compilation. To begin you will need the source code for both XtraBackup and MySQL® Server.
The source code for XtraBackup is available here at https://launchpad.net/percona-xtrabackup
while MySQL® Server source code is available at http://dev.mysql.com/downloads.
100%
[===============================================================================================================
=====>] 61,393 347K/s in 0.2s
You also need to copy the XtraBackup source directory to the innobase directory in the MySQL®
source code. Once this is done you can patch the innobase files with the included patch:
[root@mordor mysql_build]# cp -R xtrabackup-0.8-src/ mysql-5.0.83/innobase/
[root@mordor mysql_build]# cd mysql-5.0.83
[root@mordor mysql-5.0.83]# patch -p1 < fix_innodb_for_backup.patch
patching file innobase/buf/buf0buf.c
patching file innobase/buf/buf0rea.c
patching file innobase/fil/fil0fil.c
patching file innobase/include/mem0mem.h
patching file innobase/include/mem0mem.ic
patching file innobase/include/srv0start.h
patching file innobase/include/ut0byte.ic
patching file innobase/log/log0log.c
patching file innobase/log/log0recv.c
patching file innobase/mem/mem0mem.c
patching file innobase/os/os0file.c
patching file innobase/os/os0thread.c
patching file innobase/srv/srv0start.c
patching file innobase/trx/trx0trx.c
There are minimal prerequisites for the build. I needed the gcc-c++ and ncurses-devel rpm
packages on a stock Red Hat 5.3 64-bit server installation.
Once the compilation process is completed successfully, you will have a binary executable called
xtrabackup in this directory. In addition, there is a copy of the innobackupex script. Copy both
of these to the desired location on your system and you are ready.
Incorporating XtraBackup
There are two parts to any backup scenario: the backup and the restore. They are equally
important! Do not forget the restoration process and you should always be performing test
restores. This is especially true if you are just getting started with XtraBackup.
Backup
The primary function of XtraBackup is to perform a backup. Doing so proves to be fairly easy. In
the following example the innobackupex script is used in order to show how XtraBackup can
interact with MyISAM tables. A username/password for access to the MySQL® server must be
specified along with a directory in which to store the backups:
-bash-3.2$ /usr/bin/innobackupex-1.5.1 --user=root --password=pa$$W0rd /backup/full_backup
innobackupex: Using mysql Ver 14.12 Distrib 5.0.45, for redhat-linux-gnu (x86_64) using readline 5.0
innobackupex: Using mysql server version 5.0.45
090708 10:05:04 innobackupex: Starting ibbackup with command: xtrabackup --backup --suspend-at-end --target-
dir=/backup/full_backup/2009-07-08_10-05-00
innobackupex: Waiting for ibbackup (pid=4051) to suspend
innobackupex: Suspend file '/backup/full_backup/2009-07-08_10-05-00/xtrabackup_suspended'
Restore
You can create a backup by either running the innobackup script or the xtrabackup command.
Of course, you need to keep in mind that the xtrabackup command will ONLY backup InnoDB or
XtraDB tables. In either case, in order to perform a restore, you must run xtrabackup a second
time to “prepare” the backup files. Preparing the backup files is done by using the --prepare
option with xtrabackup. This step recreates the ib_logfile* files. Once this step is complete,
you are ready to move the backup files into place. The following shows a restore of the previous
full backup using the innobackupex script. Once the restore is done it is moved into place using
the copy-back option:
090714 19:16:38 innobackupex: Starting ibbackup with command: xtrabackup --prepare --target-
dir=/backups/full_backups/2009-07-14_19-15-03
[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 530, file name ./mysql-bin.000003
090714 19:16:41 innobackupex: Restarting xtrabackup with command: xtrabackup --prepare --target-
dir=/backups/full_backups/2009-07-14_19-15-03
for creating ib_logfile*
[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 530, file name ./mysql-bin.000003
When performing a restore, it will be necessary to remove any old data files. Of course the
MySQL® daemon must be shut down before this process begins. Once the restore is done you
should check the permissions of the restored directories, modify as necessary and then you
should be able to start up the MySQL® server.
-bash-3.2$ innobackupex-1.5.1 --copy-back /backups/full_backups/2009-07-14_19-15-03/
innobackupex: Warning: Your perl is too old! Innobackup requires
innobackupex: Warning: perl 5.0.5 or newer!
At this point you just start up the MySQL server and you are done with a full backup and restore.
In the example the database server contained the sakila and employees sample databases:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| employees |
| sakila |
+--------------------+
4 rows in set (0.00 sec)
Note
If you run the innobackupex and xtrabackup programs as the same user who runs the MySQL®
server daemon you should not have permissions issues. While this can take more planning up
front, it will create a smoother restore process.
Streaming backup
Being able to stream your backup gives you the option to perform a backup on one server and
store it on a second server. If you had a 500 gigabyte database and only have 50 gigabytes of
free space on the server you need to be able to stream the backup to a second server. The
option for this is --stream. Here is an example of a streamed backup:
For example, if Bob wants to access the server db1.company.com using the username bob from his
workstation ps12.company.com he would run ssh-keygen on ps12.company.com and then added
the public key to the authorized_keys file in the .ssh directory located in his home directory. If Bob
had not configured any other servers for password-less ssh access he would need to create the .ssh directory in
his home directory and copy the public key file to /home/bob/.ssh/authorized_keys.
I/O throttling
While the backup process does not lock the database if only InnoDB tables are used there is
another potential problem. The I/O activity of the backup itself can cause slowness of the server.
If this is a problem with your backup, you can use the I/O throttling option in order to control the
amount of I/O generated by the backup. The I/O throttling option is --throttle. You specify the
number of IOPS (Input/Output Operations a Second) based on the capabilities of your database
server. For example, you might want to limit the IOPS to 50 in order to keep the backup from
interfering with the normal server operation. To do this you could run something like this:
-bash-3.2$ innobackupex-1.5.1 --user=root --password='password' --throttle=50 /backup/
It is possible to throttle the backup so much that it actually can’t go any faster than the ongoing
updates to the system. In this case your backup would actually never finish. While this is a very
useful option for XtraBackup, use it with care and careful initial testing.
Incremental backups
An incremental backup is a backup of the data that has changed since a previous backup (either
a full backup or an incremental backup). Incremental backups can be used to reduce the
amount of time it takes a backup to be performed. It does this at the expense of requiring a
longer restoration time.
When XtraBackup is run it outputs the last LSN value used by InnoDB. An LSN is a Log Sequence
Number that is used to create checkpoints in the InnoDB log files. During a full backup of a
MySQL® server the LSN of the last checkpoint is recorded as part of the backup. When
performing an incremental backup this LSN is used so that only the changes that occur after this
LSN are recorded.
The next time a backup is run the LSN value, which is stored in the xtrabackup_checkpoints file
of the full backup, can be used to tell XtraBackup where to begin:
-bash-3.2$ cat xtrabackup_checkpoints
backup_type = fullprepared
from_lsn = 0:0
to_lsn = 0:469826382
As you can see the LSN value at the end of the full backup was 469909878. To record all
changes since the end of the full backup:
-bash-3.2$ xtrabackup --user=root --password='pa$$w0rd' --backup --incremenetal_ lsn=0:469826382
--targetdir=/backup/incremental_one
This must be done after your full backup is restored. If you have multiple incremental backups
they must be restored in order (Monday's backup first, then Tuesday etc). And do not forget to
test the restoration process! All the backups in the world will not do you any good if they are
corrupted by being incorrectly performed or the restoration fails because of a problem with
syntax. Take the time now to understand your system before it becomes necessary.
Summary
When first looking at XtraBackup, it can be quite intimidating. Like the late night commercials, it
seems like it is a continual “but wait . . . there’s more!!” The good news is that every feature is
designed to make backups better and more flexible. There is nothing extra here, everything is
about backups. Take the time to learn XtraBackup and the available options and it will serve you
well. There is nothing more important to a database administrator than performing backups (and
the attendant restores). XtraBackup is an open source tool that can make performing backups as
painless and powerful as possible.
Resources
XtraBackup wiki http://www.percona.com/docs/wiki/percona-xtrabackup:start
XtraBackup binary downloads http://www.percona.com/mysql/xtrabackup/
XtraBackup source code https://launchpad.net/percona-xtrabackup
XtraBackup newsgroup http://groups.google.com/group/percona-discussion