Percona XtraBackup is an high performance, low-profile and non-blocking open-source backup utility for InnoDB and XtraDB databases.
Currently is the only open-source tool that performs backups on 'MySQL-based' servers (MySQL, MariaDB, Percona Server) that doesn't lock your database during it. Backups are online, and queries and transactions continue to run without interruption ('hot backups').
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 like partial, remote, compressed and incremental backups, multi-threaded file copying for performance and Point-in-time recovery.
- Install the xtrabackup package from AUR.
XtraBackup is really a set of three tools:
- xtrabackup - a compiled C binary, which copies only InnoDB and XtraDB data
- innobackupex - a wrapper script that provides functionality to backup a whole MySQL database instance with MyISAM, InnoDB, and XtraDB tables.
- tar4ibd - tars InnoDB data safely.
In all of the below examples, the following is assumed:
- You are backing up a server whose data is stored in /var/lib/mysql/, which is the standard location for Archlinux
- You are storing the backups in /data/backups/mysql
- You have a my.cnf file in a standard location, such as /etc/my.cnf, with at least the following contents:
[mysqld] datadir=/var/lib/mysql/ [xtrabackup] target_dir=/data/backups/mysql/
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.
- Make a backup:
# xtrabackup --backup
Prepare the backup:
# xtrabackup --prepare
Prepare again, to create fresh InnoDB log files:
# xtrabackup --prepare
The exit status of a successful xtrabackup is 0. In the second --prepare 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.
You might want to set the --use-memory option to something similar to the size of your buffer pool, if you are on a dedicated server that has enough free memory. The complete documentation of the tool is here.
- Copying all your MySQL data into the specified directory in your my.cnf. It will put the backup in a timestamped subdirectory of /data/backups/, in this case, /data/backups/2010-03-13_02-42-44.
# innobackupex /data/backups
# 100313 02:43:07 innobackupex: completed OK!
- If you take the backup from a replica (“slave”), then you might need to see the position relative to the primary (“master”) at the point of the backup:
# innobackupex --slave-info /data/backups
See the full documentation of this tool here.
The tar4ibd binary is a specially patched version of tar that understands how to handle InnoDB/XtraDB data files correctly.
The syntax is the same as tar. The only consideration you must have is including the -i option if you are extracting a file with GNU tar. If not, only a part of your data will be extracted.
Setting up a slave with Xtrabackup
Xtrabackup is also can be used to clone one slave to another, or just setup new slave from the master. And it is done in a non-blocking way (almost for MyISAM setups) for the cloned server.
Assuming the previous scenario, first execute
# innobackupex --stream=tar /tmp/ --slave-info | ssh user@DESTSERVER "tar xfi - -C /var/lib/mysql"
When it finishes, on your destination server run
# innobackupex --apply-log --use-memory=2G /var/lib/mysql
and you will have ready the database directory.
Copy my.cnf from the original server and start mysqld on the new slave.
Look at the content of the file xtrabackup_slave_info that will be created on the slave:
# cat /var/lib/mysql/xtrabackup_slave_info CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000834', MASTER_LOG_POS=50743116
and execute that statement on a mysql console and start the slave:
mysql> CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000834', MASTER_LOG_POS=50743116; mysql> START SLAVE;
You should check that everything went OK with:
mysql> SHOW SLAVE STATUS \G ... Slave_IO_Running: Yes Slave_SQL_Running: Yes ... Seconds_Behind_Master: 1643 ...
mysql> STOP SLAVE;