1
.TH "XtraBackup" 1 "January 2011" "" "Percona Inc"
6
Percona XtraBackup is an open-source hot backup utility for MySQL that doesn't lock your database during the backup. It can back up data from InnoDB, XtraDB, and MyISAM tables on MySQL 5.0 and 5.1 servers, and it has many advanced features. Commercial support is available(http://www.percona.com/support/mysql-support-maintenance/).
8
Percona XtraBackup is a combination of the xtrabackup C program, and the innobackupex Perl script. The xtrabackup program copies and manipulates InnoDB and XtraDB data files, and the Perl script enables enhanced functionality, such as interacting with a running MySQL server and backing up MyISAM tables. XtraBackup works with unmodified MySQL servers, as well as Percona Server with XtraDB. It runs on Linux, Windows, and FreeBSD.
15
Backups are online, and queries and transactions continue to run without interruption.
19
MyISAM tables are read-only while they are being backed up.
23
You can select which tables and databases to back up.
27
XtraBackup is fast and low-impact. It takes special care not to disrupt your database server. It can copy files in parallel (in multiple threads) for even higher performance.
31
You can make local backups, you can stream the backups to other programs, or you can securely copy your backups to a remote host after completion.
35
You can stream a backup into a compressed format. There is a companion program tar4ibd that understands how to work with InnoDB's data format.
38
Incremental (delta) backups.
39
You can back up only the data pages that have changed since the last backup.
42
Point-in-time recovery.
43
You can recover a backup, and then use binary logs to roll forward to a point in time.
45
.SH " Percona XtraBackup User Manual "
48
This page explains how to use Percona XtraBackup. XtraBackup is really a set of three tools:
53
- a compiled C binary, which copies only InnoDB and XtraDB data
57
- a wrapper script that provides functionality to backup a whole MySQL database instance with MyISAM, InnoDB, and XtraDB tables.
61
- tars InnoDB data safely.
63
It is possible to use the
65
binary alone, or to use only the
67
wrapper script and let it execute
69
for you. It might be helpful to first learn how to use
71
, and then learn how to use
73
for convenience and added functionality.
75
.SS " How XtraBackup Works "
78
XtraBackup is based on InnoDB's crash-recovery functionality. It copies your InnoDB data files, which results in data that is internally inconsistent; but then it performs crash recovery on the files to make them a consistent, usable database again.
80
This works because InnoDB maintains a
82
, also called the transaction log. This contains a record of every change to InnoDB's data. When InnoDB starts, it inspects the data files and the transaction log, and performs two steps. It applies committed transaction log entries to the data files, and it performs an undo operation on any transactions that modified data but did not commit.
84
XtraBackup works by remembering the
85
.I "log sequence number"
86
(LSN) when it starts, and then copying away the data files. It takes some time to do this, so if the files are changing, then they reflect the state of the database at different points in time. At the same time, XtraBackup runs a background process that watches the transaction log files, and copies changes from it. XtraBackup needs to do this continually because the transaction logs are written in a round-robin fashion, and can be reused after a while. XtraBackup needs the transaction log records for every change to the data files since it began execution.
92
process. During this step, XtraBackup performs crash recovery against the copied data files, using the copied transaction log file. After this is done, the database is ready to restore and use.
94
The above process is implemented in the
96
compiled binary program. The
98
program adds more convenience and functionality by also permitting you to back up MyISAM tables and .frm files. It starts
100
, waits until it finishes copying files, and then issues
101
.B "FLUSH TABLES WITH READ LOCK"
102
to prevent further changes to MySQL's data and flush all MyISAM tables to disk. It holds this lock, copies the MyISAM files, and then releases the lock.
104
The backed-up MyISAM and InnoDB tables will eventually be consistent with each other, because after the prepare (recovery) process, InnoDB's data is rolled forward to the point at which the backup completed, not rolled back to the point at which it started. This point in time matches where the
105
.B "FLUSH TABLES WITH READ LOCK"
106
was taken, so the MyISAM data and the prepared InnoDB data are in sync.
112
tools both offer many features not mentioned in the preceding explanation. Each tool's functionality is explained in more detail on its manual page. In brief, though, the tools permit you to do operations such as streaming and incremental backups with various combinations of copying the data files, copying the log files, and applying the logs to the data.
114
.SH " The xtrabackup Binary "
119
binary is a compiled C program that is linked with the InnoDB libraries and the standard MySQL client libraries. The InnoDB libraries provide functionality necessary to apply a log to data files, and the MySQL client libraries provide command-line option parsing, configuration file parsing, and so on to give the binary a familiar look and feel.
123
your backup so it is ready to restore.
130
mode, corresponding to the two main functions it performs. There are several variations on these functions to accomplish different tasks, and there are two less commonly used modes,
134
. There is no need to use any wrapper script with
136
if you don't want to. It is quite easy to use by itself for most purposes.
138
This manual section explains how to use xtrabackup in detail.
139
.SH " Creating a Backup "
142
To create a backup, run
146
option. You also need to specify a
148
option, which is where the backup will be stored, and a
150
option, which is where the MySQL data is stored. If the InnoDB data or log files aren't stored in the same directory, you might need to specify the location of those, too. If the target directory does not exist,
152
creates it. If the directory does exist and is empty,
156
will not overwrite existing files, however; it will fail with operating system error 17, 'file exists.'
158
The tool changes its working directory to the data directory and performs two primary tasks to complete the backup:
160
- It starts a log-copying thread in the background. This thread watches the InnoDB log files, and when they change, it copies the changed blocks to a file called
161
.B "xtrabackup_logfile"
162
in the backup target directory. This is necessary because the backup might take a long time, and the recovery process needs all of the log file entries from the beginning to the end of the backup.
163
- It copies the InnoDB data files to the target directory. This is not a simple file copy; it opens and reads the files similarly to the way InnoDB does, by reading the data dictionary and copying them a page at a time.
165
When the data files are finished copying, xtrabackup stops the log-copying thread, and creates a files in the target directory called
166
.B "xtrabackup_checkpoints"
167
, which contains the type of backup performed, the log sequence number at the beginning, and the log sequence number at the end.
169
An example command to perform a backup follows:
174
xtrabackup --backup --datadir=/var/lib/mysql/ --target-dir=/data/backups/mysql/
178
This takes a backup of
181
.B "/data/backups/mysql/"
184
option deserves special explanation. Because the backup is performed from the data directory itself,
185
.B "the target directory is relative to the data directory"
186
unless you specify an absolute path. If you specify a relative path such as
187
.B "--target-dir=backups"
188
, for example, don't look for the backup in the directory from which you executed xtrabackup -- it will be a subdirectory of the
192
During the backup process, you should see a lot of output showing the data files being copied, as well as the log file thread repeatedly scanning the log files and copying from it. The last thing you should see is something like the following, where the value of the <LSN> will be a number that depends on your system:
197
xtrabackup: Transaction log of lsn (<SLN>) to (<LSN>) was copied.
201
After the backup is finished, the target directory will contain files such as the following, assuming you have a single InnoDB table
204
.B "innodb_file_per_table"
210
/data/backups/mysql/ibdata1
211
/data/backups/mysql/test
212
/data/backups/mysql/test/tbl1.ibd
213
/data/backups/mysql/xtrabackup_checkpoints
214
/data/backups/mysql/xtrabackup_logfile
218
The backup can take a long time, depending on how large the database is. It is safe to cancel at any time, because it does not modify the database.
220
.SH " Preparing the Backup "
223
After you make a backup with
225
, the next step is to
227
it. The data files are not point-in-time consistent until they've been prepared, because they were copied at different times as the program ran, and they might have been changed while this was happening. If you try to start InnoDB with these data files, it will detect corruption and crash itself to prevent you from running on damaged data. The prepare step makes the files perfectly consistent at a single instant in time, so you can run InnoDB on them.
229
During the prepare operation, xtrabackup boots up a kind of modified InnoDB that's embedded inside it (the libraries it was linked against). The modifications are necessary to disable InnoDB's standard safety checks, such as complaining that the log file isn't the right size, which aren't appropriate for working with backups. These modifications are only for the xtrabackup binary; you don't need a modified InnoDB to use xtrabackup for your backups.
231
The prepare step uses this "embedded InnoDB" to perform crash recovery on the copied datafiles, using the copied log file. The prepare step is very simple to use: you simply run xtrabackup with the
233
option and tell it which directory to prepare, for example, to prepare the backup previously taken,
238
xtrabackup --prepare --target-dir=/data/backups/mysql/
242
When this finishes, you should see an "InnoDB shutdown" with a message such as the following, where again the value of <LSN> will depend on your system:
247
101107 16:40:15 InnoDB: Shutdown completed; log sequence number <LSN>
251
Your backup is now clean and consistent, and ready to
253
. However, you might want to take an extra step to make restores as quick as possible. This is to prepare the backup a second time. The first time makes the data files perfectly self-consistent, but it doesn't create fresh InnoDB log files. If you restore the backup at this point and start MySQL, it will have to create new log files, which could take a little while, and you might not want to wait for that. If you run
255
a second time, xtrabackup will create the log files for you, and output status text such as the following, which is abbreviated for clarity. The value of <SIZE> will depend on your MySQL configuration.
260
$ xtrabackup --prepare --target-dir=/data/backups/mysql/
261
xtrabackup: This target seems to be already prepared.
262
xtrabackup: notice: xtrabackup_logfile was already used to '--prepare'.
263
101107 16:54:10 InnoDB: Log file ./ib_logfile0 did not exist: new to be created
264
InnoDB: Setting log file ./ib_logfile0 size to <SIZE> MB
265
InnoDB: Database physically writes the file full: wait...
266
101107 16:54:10 InnoDB: Log file ./ib_logfile1 did not exist: new to be created
267
InnoDB: Setting log file ./ib_logfile1 size to <SIZE> MB
268
InnoDB: Database physically writes the file full: wait...
269
101107 16:54:15 InnoDB: Shutdown completed; log sequence number 1284108
272
.SH " Restoring a Backup "
275
The xtrabackup binary does not have any functionality for restoring a backup. That is up to the user to do. You might use
279
to restore the files. You should check that the restored files have the correct ownership and permissions.
281
Note that xtrabackup backs up only the InnoDB data. You must separately restore the MySQL system database, MyISAM data, table definition files (.frm files), and everything else necessary to make your database functional.
283
.SH " How-To and Recipes "
286
This page is a quick-reference list of recipes for the
288
tool. It doesn't mention the
290
tool; that is documented separately.
292
In all of the below examples, we assume the following:
295
* You are backing up a server whose data is stored in
297
, which is the standard location for most distributions
299
* You are storing the backups in
300
.B "/data/backups/mysql"
305
file in a standard location, such as
307
, with at least the following contents:
310
datadir=/var/lib/mysql/
312
target_dir=/data/backups/mysql/
316
.SS " Making a Backup "
319
Making the backup copies the InnoDB data and log files to the destination. Preparing the backup makes the data files consistent and ready to use.
327
* Prepare the backup:
332
* Prepare again, to create fresh InnoDB log files:
338
.B "Success Criterion"
348
step, you should see InnoDB print messages similar to "Log file ./ib_logfile0 did not exist: new to be created", followed by a line indicating the log file was created.
351
.B "Relevant Configuration"
355
* You might want to set the
357
option to something similar to the size of your buffer pool, if you are on a dedicated server that has enough free memory.
359
.SS " Making an Incremental Backup "
362
Making an incremental backup requires a full backup as a base.
365
* Create a full backup as above, but do not run the
369
* Suppose the full backup is on Monday
371
* Create an incremental backup on Tuesday:
374
xtrabackup --backup --target-dir=/data/backups/inc/tue/ \
375
--incremental-basedir=/data/backups/mysql/
378
* Create an incremental backup on Wednesday:
381
xtrabackup --backup --target-dir=/data/backups/inc/wed/ \
382
--incremental-basedir=/data/backups/inc/tue/
385
* Prepare the base backup from Monday:
388
xtrabackup --prepare --apply-log-only --target-dir=/data/backups/mysql/
391
* Roll Monday's data forward to the state on Tuesday:
394
xtrabackup --prepare --apply-log-only --target-dir=/data/backups/mysql/ \
395
--incremental-dir=/data/backups/inc/tue/
398
* Roll forward again to the state on Wednesday:
401
xtrabackup --prepare --apply-log-only --target-dir=/data/backups/mysql/ \
402
--incremental-dir=/data/backups/inc/wed/
405
* Follow the steps for a normal backup to complete the preparation stage so the backup is ready to restore
407
.SS " Restoring the Backup "
412
doesn't copy MyISAM files, .frm files, and the rest of the database, you might need to back those up separately. To restore the InnoDB data, simply do something like the following after preparing:
417
cd /data/backups/mysql/
418
rsync -rvt --exclude 'xtrabackup_checkpoints' --exclude 'xtrabackup_logfile' \
420
chown -R mysql:mysql /var/lib/mysql/
423
.SH " Limitations of xtrabackup "
426
The xtrabackup binary has some limitations you should be aware of to ensure that your backups go smoothly and are recoverable.
429
* If the xtrabackup_logfile is larger than 4GB, the
431
step will fail on 32-bit versions of xtrabackup. This limitation also applied to some older 64-bit versions of xtrabackup (
435
* xtrabackup does not currently create new InnoDB log files (ib_logfile0, etc) during the initial
437
step. You must prepare the backup a second time to do this, if you wish.
439
* xtrabackup copies only the InnoDB data and logs. It does not copy table definition files (.frm files), MyISAM data, users, privileges, or any other portions of the overall database that lie outside of the InnoDB data. To back up this data, you need a wrapper script such as
445
doesn't understand the very old
449
syntax that MySQL uses. See
452
.SH " Configuring xtrabackup "
455
This page explains how to configure
457
and how to configure your system to support
461
.SS " Configuring xtrabackup "
466
configuration is through options, which behave exactly like standard MySQL program options: they can be specified either at the command-line, or through a file such as /etc/my.cnf.
468
The xtrabackup binary reads the [myslqd] and [xtrabackup] sections from any configuration files, in order. That is so that it can read its options from your existing MySQL installation, such as the
470
or some of the InnoDB options. If you want to override these, just specify them in the [xtrabackup] section, and because it is read later, it will take precedence.
472
You don't need to put any configuration in your my.cnf if you don't want to. You can simply specify the options on the command-line. Normally, the only thing you might find convenient to place in the [xtrabackup] section of your my.cnf file is the
480
target_dir = /data/backups/mysql/
484
This manual will assume that you
486
have any file-based configuration for
488
, so it will always show command-line options being used explicitly. Please see the
489
.B "option and variable reference"
490
for details on all of the configuration options.
494
binary does not accept exactly the same syntax in the
498
server binary does. For historical reasons, the
500
server binary accepts parameters with a
501
.B "--set-variable=<variable>=<value>"
504
does not understand. If your
506
file has such configuration directives, you should rewrite them in the
507
.B "--variable=value"
510
.SS " System Configuration and NFS Volumes "
515
tool requires no special configuration on most systems. However, the storage where the
517
is located must behave properly when
519
is called. In particular, we have noticed that NFS volumes not mounted with the
521
option might not really sync the data. As a result, if you back up to an NFS volume mounted with the
523
option, and then try to prepare the backup from a different server that also mounts that volume, the data might appear to be corrupt. You can use the
525
mount option to avoid this problem.
526
.SH " The xtrabackup Option Reference "
529
This page documents all of the command-line options for the
533
.SS " --print-defaults "
536
Print the program argument list and exit. Must be given as the first option on the command-line.
538
.SS " --no-defaults "
541
Don't read default options from any option file. Must be given as the first option on the command-line.
543
.SS " --defaults-file "
546
Only read default options from the given file. Must be given as the first option on the command-line. Must be a real file; cannot be a symbolic link.
548
.SS " --defaults-extra-file "
551
Read this file after the global files are read. Must be given as the first option on the command-line.
553
.SS " --apply-log-only "
556
This option causes only the redo stage to be performed when
557
.B "incremental backups"
563
Make a backup and place it in
566
.B "creating a backup"
569
.SS " --create-ib-logfile "
572
This option is not currently implemented. To create the InnoDB log files, you must
573
.B "prepare the backup"
579
The source directory for the backup. This should be the same as the datadir for your MySQL server, so it should be read from
581
if that exists; otherwise you must specify it on the command line.
586
Create files necessary for exporting tables. See [[export and import]].
588
.SS " --incremental-basedir "
592
.B "incremental backup"
593
, this is the directory containing the full backup that is the base dataset for the incremental backups.
595
.SS " --incremental-dir "
599
.B "incremental backup"
600
, this is the directory where the incremental backup is combined with the full backup to make a new full backup.
602
.SS " --incremental-lsn "
606
.B "incremental backup"
607
, you can specify the log sequence number (LSN) in high:low format as two 32-bit integers instead of specifying
608
.B "--incremental-basedir"
611
.SS " --innodb-miscellaneous "
614
There is a large group of InnoDB options that are normally read from the
616
configuration file, so that
618
boots up its embedded InnoDB in the same configuration as your current server. You normally do not need to specify these explicitly. These options have the same behavior that they have in InnoDB or XtraDB. They are as follows:
622
.B "--innodb-adaptive-hash-index"
626
.B "--innodb-additional-mem-pool-size"
630
.B "--innodb-autoextend-increment"
634
.B "--innodb-buffer-pool-size"
638
.B "--innodb-checksums"
642
.B "--innodb-data-file-path"
646
.B "--innodb-data-home-dir"
650
.B "--innodb-doublewrite-file"
654
.B "--innodb-doublewrite"
658
.B "--innodb-extra-undoslots"
662
.B "--innodb-fast-checksum"
666
.B "--innodb-file-io-threads"
670
.B "--innodb-file-per-table"
674
.B "--innodb-flush-log-at-trx-commit"
678
.B "--innodb-flush-method"
682
.B "--innodb-force-recovery"
686
.B "--innodb-io-capacity"
690
.B "--innodb-lock-wait-timeout"
694
.B "--innodb-log-buffer-size"
698
.B "--innodb-log-files-in-group"
702
.B "--innodb-log-file-size"
706
.B "--innodb-log-group-home-dir"
710
.B "--innodb-max-dirty-pages-pct"
714
.B "--innodb-open-files"
718
.B "--innodb-page-size"
722
.B "--innodb-read-io-threads"
726
.B "--innodb-write-io-threads"
734
not copy data files, and output the contents of the InnoDB log files to STDOUT until the
735
.B "--suspend-at-end"
743
perform recovery on a backup created with
745
, so that it is ready to use. See
746
.B "preparing a backup"
749
.SS " --print-param "
754
print out parameters that can be used for copying the data files back to their original locations to restore them. See
755
.B "scripting with xtrabackup"
763
to scan the specified data files and print out
764
.B "index statistics"
767
.SS " --suspend-at-end "
772
to create a file called
773
.B "xtrabackup_suspended"
775
.B "scripting with xtrabackup"
778
.SS " --tables-file "
781
A file containing one table name per line, in
782
.B "databasename.tablename"
783
format. The backup will be limited to the specified tables. See
790
A regular expression against which the full tablename, in
791
.B "databasename.tablename"
792
format, is matched. If the name matches, the table is backed up. See
799
This option specifies the destination directory for the backup. If the directory does not exist,
801
creates it. If the directory does exist and is empty,
805
will not overwrite existing files, however; it will fail with operating system error 17, 'file exists.'
809
, relative paths are interpreted as being relative to the current working directory.
815
.B "throttling a backup"
821
This option is currently not used for anything except printing out the correct
830
This option affects how much memory is allocated for
832
. Its purpose is similar to
833
.B "innodb_buffer_pool_size"
834
. It does not do the same thing as the similarly named option in Oracle's InnoDB Hot Backup tool. The default value is 100MB, and if you have enough available memory, 1GB to 2GB is a good recommended value.
839
This option specifies the number of threads to use to copy multiple data files concurrently when creating a backup. The default value is 1 (i.e., no concurrent transfer).
841
Currently, the option only works for local backups.
843
.SH " Exporting and Importing Tables "
846
In standard InnoDB, it is not normally possible to copy tables between servers by copying the files, even with
847
.B "innodb_file_per_table"
848
. However, with the xtrabackup binary, you can export individual tables from any InnoDB database, and import them into Percona Server with XtraDB. (The source doesn't have to be XtraDB, but the destination does.) This functionality requires
849
.B "innodb_file_per_table"
850
to be used on both servers, and requires
851
.B "innodb_expand_import"
852
to be enabled on the destination server. It only works on individual
854
files, and cannot export a table that is not contained in its own
858
Let's see how to export and import the following table:
863
CREATE TABLE export_test (
864
a int(11) DEFAULT NULL
865
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
869
.SS " Exporting the Table "
872
This table should have been created in innodb_file_per_table mode, so after taking a backup as usual with
876
file should exist in the target directory:
881
$ find /data/backups/mysql/ -name export_test.*
882
/data/backups/mysql/test/export_test.ibd
886
when you prepare the backup, add the extra parameter
888
to the command. If you do not have
889
.B "innodb_file_per_table"
890
in your my.cnf, you must add that to the command-line options. Here is an example:
895
xtrabackup --prepare --export --innodb-file-per-table --target-dir=/data/backups/mysql/
901
file in the target directory:
906
$ find /data/backups/mysql/ -name export_test.*
907
/data/backups/mysql/test/export_test.exp
908
/data/backups/mysql/test/export_test.ibd
912
These two files are all you need to import the table into a server running Percona Server with XtraDB.
914
.SS " Importing the Table "
917
On the destination server running Percona Server with XtraDB, with
918
.B "innodb_expand_import"
919
enabled, create a table with the same structure, and then perform the following steps:
922
.B "ALTER TABLE test.export_test DISCARD TABLESPACE;"
924
- If you see the following message, then you must enable innodb_file_per_table and create the table again: "ERROR 1030 (HY000): Got error -1 from storage engine"
925
- Copy the exported files to the
927
subdirectory of the destination server's data directory
929
.B "ALTER TABLE test.export_test IMPORT TABLESPACE;"
932
The table should now be imported, and you should be able to SELECT from it and see the imported data.
934
.SH " Incremental Backups "
939
tool supports incremental backups, which means that it can copy only the data that has changed since the last full backup. You can perform many incremental backups between each full backup, so you can set up a backup process such as a full backup once a week and an incremental backup every day, or full backups every day and incremental backups every hour.
941
Incremental backups work because each InnoDB page (usually 16kb in size) contains a
942
.I "log sequence number"
943
, or LSN. The LSN is the system version number for the entire database. Each page's LSN shows how recently it was changed. An incremental backup copies each page whose LSN is newer than the previous incremental or full backup's LSN.
945
Incremental backups do not actually compare the data files to the previous backup's data files. In fact, you can use
946
.B "--incremental-lsn"
947
to perform an incremental backup without even having the previous backup, if you know its LSN. Incremental backups simply read the pages and compare their LSN to the last backup's LSN. You still need a full backup to recover the incremental changes, however; without a full backup to act as a base, the incremental backups are useless.
949
.SS " Creating an Incremental Backup "
952
To make an incremental backup, begin with a full backup as usual. The xtrabackup binary writes a file called xtrabackup_checkpoints into the backup's target directory. This file contains a line showing the
954
, which is the database's LSN at the end of the backup. Create the full backup with a command such as the following:
959
xtrabackup --backup --target-dir=/data/backups/base --datadir=/var/lib/mysql/
967
full backup, use innobackupex since xtrabackup itself won't copy table definitions, triggers, or anything else that's not .ibd.
970
If you look at the xtrabackup_checkpoints file, you should see some contents similar to the following:
975
backup_type = full-backuped
981
Now that you have a full backup, you can make an incremental backup based on it. Use a command such as the following:
986
xtrabackup --backup --target-dir=/data/backups/inc1 \
987
--incremental-basedir=/data/backups/base --datadir=/var/lib/mysql/
992
.B "/data/backups/inc1/"
993
directory should now contain delta files, such as
996
.B "test/table1.ibd.delta"
997
. These represent the changes since the LSN 1291135. If you examine the
998
.B "xtrabackup_checkpoints"
999
file in this directory, you should see something similar to the following:
1004
backup_type = incremental
1010
The meaning should be self-evident. It's now possible to use this directory as the base for yet another incremental backup:
1015
xtrabackup --backup --target-dir=/data/backups/inc2 \
1016
--incremental-basedir=/data/backups/inc1 --datadir=/var/lib/mysql/
1020
.SS " Preparing the Backups "
1025
step for incremental backups is not the same as for normal backups. In normal backups, two types of operations are performed to make the database consistent: committed transactions are replayed from the log file against the data files, and uncommitted transactions are rolled back. For technical reasons, you must skip the rollback of uncommitted transactions when preparing a backup that will be used as the base for an incremental backup. You should use the
1026
.B "--apply-log-only"
1027
option to prevent the rollback phase.
1031
If you do not use the
1032
.B "--apply-log-only"
1033
option to prevent the rollback phase, then your incremental backups will be useless. After transactions have been rolled back, further incremental backups cannot be applied.
1036
Beginning with the full backup you created, you can prepare it, and then apply the incremental differences to it. Recall that you have the following backups:
1047
To prepare the base backup, you need to run
1049
as usual, but prevent the rollback phase:
1054
xtrabackup --prepare --apply-log-only --target-dir=/data/backups/base
1058
The output should end with some text such as the following:
1060
101107 20:49:43 InnoDB: Shutdown completed; log sequence number 1291135
1062
The log sequence number should match the to_lsn of the base backup, which you saw previously.
1064
This backup is actually safe to restore as-is now, even though the rollback phase has been skipped. If you restore it and start MySQL, InnoDB will detect that the rollback phase was not performed, and it will do that in the background, as it usually does for a crash recovery upon start. It will notify you that the database was not shut down normally, but the recovery phase should be nearly instantaneous, because the redo log has already been applied to the data files.
1066
To apply the first incremental backup to the full backup, you should use the following command:
1071
xtrabackup --prepare --apply-log-only --target-dir=/data/backups/base \
1072
--incremental-dir=/data/backups/inc1
1076
This applies the delta files to the files in /data/backups/base, which rolls them forward in time to the time of the incremental backup. It then applies the redo log as usual to the result. The final data is in /data/backups/base,
1078
in the incremental directory. You should see some output such as the following:
1083
incremental backup from 1291135 is enabled.
1084
xtrabackup: cd to /data/backups/base/
1085
xtrabackup: This target seems to be already prepared.
1086
xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(1291340)
1087
Applying /data/backups/inc1/ibdata1.delta ...
1088
Applying /data/backups/inc1/test/table1.ibd.delta ...
1090
101107 20:56:30 InnoDB: Shutdown completed; log sequence number 1291340
1094
Again, the LSN should match what you saw from your earlier inspection of the first incremental backup. If you restore the files from /data/backups/base, you should see the state of the database as of the first incremental backup.
1096
Preparing the second incremental backup is a similar process: apply the deltas to the (modified) base backup, and you will roll its data forward in time to the point of the second incremental backup:
1101
xtrabackup --prepare --apply-log-only --target-dir=/data/backups/base \
1102
--incremental-dir=/data/backups/inc2
1106
If you wish to avoid the notice that InnoDB was not shut down normally, when you have applied the desired deltas to the base backup, you can run
1108
again without disabling the rollback phase.
1109
.SH " Partial Backups "
1112
xtrabackup supports taking partial backups when
1113
.B "innodb_file_per_table"
1114
is enabled. There are two ways to create partial backups.
1116
For the purposes of this manual page, we will assume that there is a database named 'test' which contains tables named 't1' and 't2'.
1118
.SS " Using the --tables Option "
1121
The first method is with the
1123
option. The option's value is a regular expression that is matched against the fully qualified tablename, including the database name, in the form
1124
.B "databasename.tablename"
1127
To back up only tables in the 'test' database, you can use the following command:
1129
xtrabackup --backup --datadir=/var/lib/mysql --target-dir=/data/backups/ \
1130
--tables="^test[.].*"
1132
To back up only the table 'test.t1', you can use the following command:
1134
xtrabackup --backup --datadir=/var/lib/mysql --target-dir=/data/backups/ \
1135
--tables="^test[.]t1"
1137
.SS " Using the --tables-file Option "
1142
option specifies a file that can contain multiple table names, one table name per line in the file. Only the tables named in the file will be backed up. Names are matched exactly, case-sensitive, with no pattern or regular expression matching. The table names must be fully qualified, in
1143
.B "databasename.tablename"
1146
.SS " Preparing the Backup "
1151
option on a partial backup, you will see warnings about tables that don't exist. That is because these tables exist in the data dictionary inside InnoDB, but the corresponding
1153
files don't exist. They were not copied into the backup directory. These tables will be removed from the data dictionary, and when you restore the backup and start InnoDB, they will no longer exist and will not cause any errors or warnings to be printed to the log file.
1155
An example of the error message you will see during the prepare phase follows.
1160
InnoDB: Reading tablespace information from the .ibd files...
1161
101107 22:31:30 InnoDB: Error: table 'test1/t'
1162
InnoDB: in InnoDB data dictionary has tablespace id 6,
1163
InnoDB: but tablespace with that id or name does not exist. It will be removed from data dictionary.
1167
.SH " Analyzing Table Statistics "
1172
binary can analyze InnoDB data files in read-only mode to give statistics about them. To do this, you should use the
1174
option. You can combine this with the
1176
option to limit the files to examine. It also uses the
1180
You can perform the analysis on a running server, with some chance of errors due to the data being changed during analysis. Or, you can analyze a backup copy of the database. Either way, to use the statistics feature, you need a clean copy of the database including correctly sized log files, so you need to execute with
1182
twice to use this functionality on a backup.
1184
The result of running on a backup might look like the following:
1190
table: test/table1, index: PRIMARY, space id: 12, root page 3
1191
estimated statistics in dictionary:
1192
key vals: 25265338, leaf pages 497839, size pages 498304
1194
level 2 pages: pages=1, data=5395 bytes, data/pages=32%
1195
level 1 pages: pages=415, data=6471907 bytes, data/pages=95%
1196
leaf pages: recs=25958413, pages=497839, data=7492026403 bytes, data/pages=91%
1200
This can be interpreted as follows:
1203
* The first line simply shows the table and index name and its internal identifiers. If you see an index named
1204
.B "GEN_CLUST_INDEX"
1205
, that is the table's clustered index, automatically created because you did not explicitly create a PRIMARY KEY.
1208
.I "estimated statistics in dictionary"
1209
information is similar to the data that's gathered through
1211
inside of InnoDB to be stored as estimated cardinality statistics and passed to the query optimizer
1214
.I "real statistics"
1215
information is the result of scanning the data pages and computing exact information about the index.
1218
.B "level <X> pages:"
1219
output means that the line shows information about pages at that level in the index tree. The larger <X> is, the farther it is from the leaf pages, which are level 0. The first line is the root page.
1223
output shows the leaf pages, of course. This is where the table's data is stored.
1226
.B "external pages:"
1227
output (not shown) shows large external pages that hold values too long to fit in the row itself, such as long BLOB and TEXT values.
1231
is the real number of records (rows) in leaf pages.
1239
is the total size of the data in the pages, in bytes.
1243
is calculated as (data / (pages * PAGE_SIZE)) * 100%. It will never reach 100% because of space reserved for page headers and footers.
1246
.SH " Throttling Backups "
1251
does not block your database's operation, any backup can add load to the system being backed up. On systems that do not have much spare I/O capacity, it might be helpful to throttle the rate at which
1253
reads and writes data. You can do this with the
1259
mode, this option limits the number of pairs of read-and-write operations per second that
1261
will perform. If you are creating an
1262
.B "incremental backup"
1263
, then the limit is the number of read IO operations per second.
1265
By default, there is no throttling, and
1267
reads and writes data as quickly as it can. If you set too strict of a limit on the I/O operations, the backup might be so slow that it will never catch up with the transaction logs that InnoDB is writing, so the backup might never complete.
1269
.SH " Working with Binary Logs "
1274
binary integrates with information that InnoDB stores in its transaction log about the corresponding binary log position for committed transactions. This enables it to print out the binary log position to which a backup corresponds, so you can use it to set up new replication slaves or perform point-in-time recovery.
1276
.SS " Finding the Binary Log Position "
1279
You can find the binary log position corresponding to a backup performing the
1281
process. If your backup is from a server with binary logging enabled,
1283
will create a file named
1284
.B "xtrabackup_binlog_pos_innodb"
1285
in the target directory. This file contains the binary log file name and position of the exact point in the binary log to which the prepared backup corresponds.
1287
You will also see output similar to the following during the prepare stage:
1292
InnoDB: Last MySQL binlog file position 0 3252710, file name ./mysql-bin.000001
1295
If you use binary log and don't use any hack of group commit,
1296
the binary log position seems to be:
1297
InnoDB: Last MySQL binlog file position 0 3252710, file name ./mysql-bin.000001
1301
The output should contain the same file name and position as the
1302
.B "xtrabackup_binlog_pos_innodb"
1303
file. The message about hacking group commit refers to an early implementation of emulated group commit in Percona Server.
1305
is this still uncertain, or do we know that it works correctly now?
1307
.SS " Point-In-Time Recovery "
1310
To perform a point-in-time recovery from an
1312
backup, you should prepare and restore the backup, and then replay binary logs from the point shown in the
1313
.B "xtrabackup_binlog_pos_innodb"
1316
.SS " Setting Up a New Replication Slave "
1319
To set up a new replica, you should prepare the backup, and restore it to the data directory of your new replication slave. Then in your
1320
.B "CHANGE MASTER TO"
1321
command, use the binary log filename and position shown in the
1322
.B "xtrabackup_binlog_pos_innodb"
1323
file to start replication.
1325
.SH " Scripting Backups With xtrabackup "
1330
tool has several features to enable scripts to control it while they perform related tasks. The
1332
script is one example, but xtrabackup is easy to control with your own command-line scripts too.
1334
.SS " Suspending After Copying "
1339
mode, xtrabackup normally copies the log files in a background thread, copies the data files in a foreground thread, and then stops the log copying thread and finishes. If you use the
1340
.B "--suspend-at-end"
1341
option, instead of stopping the log thread and finishing, xtrabackup continues to copy the log files, and creates a file in the target directory called
1342
.B "xtrabackup_suspended"
1343
. As long as that file exists,
1345
will continue to watch the log files and copy them into the
1346
.B "xtrabackup_logfile"
1347
in the target directory. When the file is removed,
1349
will finish copying the log file and exit.
1351
This functionality is useful for coordinating the InnoDB data backups with other actions. Perhaps the most obvious is copying the table definitions (the .frm files) so that the backup can be restored. You can start
1353
in the background, wait for the
1354
.B "xtrabackup_suspended"
1355
file to be created, and then copy any other files you need to complete the backup. This is exactly what the
1357
tool does (it also copies MyISAM data and other files).
1359
.SS " Generating Meta-Data "
1362
It is a good idea for the backup to include all the information you need to restore the backup. The
1364
tool can print out the contents of a
1366
file that are needed to restore the data and log files. If you add the
1368
option, it will print out something like the following:
1373
# This MySQL options file was generated by XtraBackup.
1375
datadir = /data/mysql/
1376
innodb_data_home_dir = /data/innodb/
1377
innodb_data_file_path = ibdata1:10M:autoextend
1378
innodb_log_group_home_dir = /data/innodb-logs/
1382
You can redirect this output into a file in the target directory of the backup.
1384
.SS " Agreeing on the Source Directory "
1387
It's possible that the presence of a defaults file or other factors could cause
1389
to back up data from a different location than you expected. To prevent this, you can use
1391
to ask it where it will be copying data from. You can use the output to ensure that
1393
and your script are working on the same dataset.
1395
.SS " Log Streaming "
1400
to omit copying data files, and simply stream the log file to its standard output instead with
1402
. This automatically adds the
1403
.B "--suspend-at-end"
1404
option. Your script can then perform tasks such as streaming remote backups by piping the log files into an SSH connection and copying the data files to another server with a tool such as
1410
.SH " XtraBackup Exit Codes "
1415
binary exits with the traditional success value of 0 after a backup when no error occurs. If an error occurs during the backup, the exit value is 1.
1417
In certain cases, the exit value can be something other than 0 or 1, due to the command-line option code included from the MySQL libraries. An unknown command-line option, for example, will cause an exit code of 255.
1419
When an error happens in the
1425
is preparing to perform the backup, the exit code is -1. This is usually because of a missing or wrong command-line option, failure to open a file or directory that the user specified as a command-line option, or similar. This behavior is changed in xtrabackup 1.4 and later, so it always returns 0 or 1. (However, the MySQL libraries might still exit with a code of 255.)
1427
.SH " Implementation Details "
1430
This page contains notes on various internal aspects of the
1434
.SS " File Permissions "
1439
opens the source data files in read-write mode, although it does not modify the files. This means that you must run
1441
as a user who has permission to write the data files. The reason for opening the files in read-write mode is that
1443
uses the embedded InnoDB libraries to open and read the files, and InnoDB opens them in read-write mode because it normally assumes it is going to write to them.
1445
.SS " Tuning the OS Buffers "
1450
reads large amounts of data from the filesystem, it uses
1451
.B "posix_fadvise()"
1452
where possible, to instruct the operating system not to try to cache the blocks it reads from disk. Without this hint, the operating system would prefer to cache the blocks, assuming that
1454
is likely to need them again, which is not the case. Caching such large files can place pressure on the operating system's virtual memory and cause other processes, such as the database server, to be swapped out. The
1456
tool avoids this with the following hint on both the source and destination files:
1458
posix_fadvise(file, 0, 0, POSIX_FADV_DONTNEED)
1462
asks the operating system to perform more aggressive read-ahead optimizations on the source files:
1464
posix_fadvise(file, 0, 0, POSIX_FADV_SEQUENTIAL)
1466
.SS " Copying Data Files "
1469
When copying the data files to the target directory,
1471
reads and writes 1MB of data at a time. This is not configurable. When copying the log file,
1473
reads and writes 512 bytes at a time. This is also not possible to configure, and matches InnoDB's behavior.
1475
After reading from the files,
1477
iterates over the 1MB buffer a page at a time, and checks for page corruption on each page with InnoDB's
1478
.B "buf_page_is_corrupted()"
1479
function. If the page is corrupt, it re-reads and retries up to 10 times for each page. It skips this check on the doublewrite buffer.
1481
Percona Inc. http://www.percona.com/
1482
.SH "REPORTING BUGS"
1483
Report bugs to https://bugs.launchpad.net/percona-xtrabackup/+filebug