Xtrabackup full backup, incremental backup, restore MySQL database

Source: Internet
Author: User
Tags percona perl script

Xtrabackup User Guide InnoDB Data Backup tool

Xtrabackup is a innodb to do data backup tools, Support Online hot backup ( backup without affecting the data read and write, that is, no lock), is a commercial backup tool InnoDB hotbackup a good alternative

I. Introduction of Xtrabackup

A, what is Xtrabackup?

Xtrabackup is a innodb to do data backup tools, Support Online hot backup (backup without affecting the data read and write), is a commercial backup tool InnoDB hotbackup a good alternative.

Xtrabackup has two main tools: Xtrabackup, Innobackupex

1,Xtrabackup can only back up InnoDB and xtradb two kinds of data tables, but cannot back up MyISAM data table
2, Innobackupex is a reference to the InnoDB hotbackup innoback script modified. Innobackupex is a Perl script package that encapsulates xtrabackup. The main purpose is to back up the tables of the InnoDB and MyISAM engines at the same time, but Innobackupex needs to add a read lock when processing MyISAM . and added some options to use.

If Slave-info can record backup recovery, as slave need some information, according to this information, can easily use Backup to redo slave.

3. Official documents: Http://www.percona.com/docs/wiki/percona-xtrabackup:start

B, what can xtrabackup do

Online (Hot) backup InnoDB, xtradb tables for the entire library
Incremental backup based on Xtrabackup's last backup of the whole library (InnoDB only)
 generate backups as streams that can be saved directly to a remote machine (useful when the hard disk space is low)

The MySQL database itself provides tools that do not support true incremental backups, and binary log recovery is point-in-time (Point-in-time) recovery instead of incremental backups.

The Xtrabackup tool supports incremental backups of the InnoDB storage engine and works as follows:

(1) Complete a full backup first and record the LSN (log Sequence number) of the checkpoint at this point.
(2) When a process incremental backup, compares the LSN of each page in the tablespace to the LSN at the time of the last backup, and if so, backs up the page and logs the LSN of the current checkpoint.

First, the final checkpoint ("last checkpoint LSN") is found and recorded in logfile, and the logfile to Xtrabackup_logfile of InnoDB is started from the position of the LSN; Start copying all data files. ibd; Copy logfile is not stopped until the end of the copy of all data files.

Because the logfile inside records all the data modification situation, so, in the backup process, the data file has been modified, still can be restored by parsing xtrabackup_logfile to keep the data consistent.

C, Xtrabackup Backup principle

Xtrabackup is based on the InnoDB crash-recovery function. It replicates the data file of InnoDB, because it does not lock the table, the copied information is inconsistent, use crash-recovery when recovering, make the data recovery consistent.

InnoDB maintains a redo log, also known as transaction log, the transaction log, which contains all the changes to the InnoDB data. When InnoDB starts, it checks the data file and the transaction log, and it does a two-step operation:

Xtrabackup when backing up, a page by page copy of InnoDB data, and do not lock the table, at the same time, xtrabackup there is another thread monitoring transactions log, once the log changes, the changed log pages copied away. Why the rush to copy away? Because the transactions log file is limited in size, when it is full, it starts from scratch, so the new data may overwrite the old data.

During the prepare process, Xtrabackup uses the copied transactions log to crash recovery the backed up InnoDB data file.

D. Implementation Details

Xtrabackup Open the InnoDB data file in Read-write mode and then copy it. It does not actually modify this file. That is, the user running xtrabackup must have read and write access to the InnoDB data file. The reason for using the read-write mode is because Xtrabackup uses its built-in InnoDB library to open the file, while the InnoDB library opens the file with RW.

Xtrabackup to replicate large amounts of data from the file system, so it uses posix_fadvise () as much as possible to tell the OS not to cache the data it reads to improve performance. Because this data is not reused, the OS is not so smart. If you want to cache, a few g of data, the OS virtual memory will be a great pressure, other processes, such as mysqld is likely to be swap out, so the system will be greatly affected.

In the process of backing up InnoDB page, Xtrabackup reads and writes 1MB of data each time, 1mb/16kb=64 a page. This is not configurable. After reading the 1MB data, xtrabackup the 1MB data one page at a time, using InnoDB's buf_page_is_corrupted () function to check if the data on this page is normal, and if the data is not normal, reread the page and reread it up to 10 times. If it fails, the backup fails and exits. When copying transactions log, read and write 512KB data at a time. The same cannot be configured.

Second, xtrabackup installation

This article installs the Xtrabackup through the source way, the newest version is 1.6.

Xtrabackup is different from the general way of building Code, it adopts the method of building the patch on the MySQL source code. Download the source of the package is relatively large is because the package is packaged in the compilation required two MySQL Source, a 5.1.56, a 5.5.10.

Install dependent packages required for compilation


Yum Install Libaio Libaio-devel

Compiling and installing Xtrabackup


Copy the Code code as follows:

wget http://www.percona.com/redir/downloads/XtraBackup/XtraBackup-1.6/source/xtrabackup-1.6.tar.gz
Tar xvf xtrabackup-1.6.tar.gz
CD xtrabackup-1.6
Utils/build.sh innodb55

Note: build.sh is a compilation script provided in the source package, MySQL 5.1 can be used with parameters innodb51_builtin.

After the compilation is successful, a xtrabackup program is generated in the corresponding MySQL version directory, and the path I generate here is mysql-5.5.10/storage/innobase/xtrabackup/

LS mysql-5.5.10/storage/innobase/xtrabackup/
Makefile xtrabackup.c xtrabackup_innodb55 XTRABACKUP.O

Copy the file and create a soft link to/usr/bin, and this is done because the Innobackupex script needs to be met.


Copy the Code code as follows:

CP mysql-5.5.10/storage/innobase/xtrabackup/xtrabackup_innodb55/usr/bin/xtrabackup_55
CP Innobackupex/usr/bin/innobackupex
#备份时如果打包就需要这个命令, not required.

  Three Xtrabackup Backup and Recovery use

A, xtrabackup Common parameters options are as follows:



The path to the default profile, and if it does not, Xtrabackup will look for configuration files/etc/my.cnf,/etc/mysql/my.cnf,/USR/LOCAL/ETC/MY.CNF, ~/from the following location in turn. My.cnf and reads the [mysqld] and [xtrabackup] configuration segments in the configuration file.

[mysqld] only need to specify DataDir, Innodb_data_home_dir, Innodb_data_file_path, Innodb_log_group_home_dir, innodb_log_files_in _group, innodb_log_file_size6 parameters can let xtrabackup work normally.

If this parameter is used, the configuration file specified here is read again after the global profile is read

Storage directory path for backup files

Implement Backup to Target-dir

Implement preparation for backup files before recovery (generate InnoDB log file)

Parameters required to print a backup or restore

This parameter is used when prepare, controlling the amount of memory used by InnoDB instances when prepare

Generate a xtrabackup_suspended file in the Target-dir directory, suspend the xtrabackup process, and continuously synchronize changes to the data file to the backup file until the user manually deletes the xtrabackup_suspended file

Io times per second, limiting the amount of I/O operations used at backup, minimizing the impact of backups on the normal business of the database

This parameter is used at backup time to output the contents of the Xtrabackup_logfile to standard output, which is automatically used when using the Suspend-at-end parameter, which is used by the stream mode of the Innobackupex script.

An incremental backup only copies the LSN specified by this parameter to the new IBD pages, which LSN was last backed up to see the previous backup set of the Xtrabackup_checkpoints file

This parameter is used at backup time to back up the new IDB pages for the backup set specified in this parameter

This parameter is used when prepare, specifying prepare when the. delta file and log file are stored

Used when backing up data files of type file-per-table, using regular expressions to specify InnoDB tables that need to be backed up

The data file directory of the MySQL database.

B, normal backup mode

A) normal backup (full-scale backup)


Copy the Code code as follows:


Note: Xtrabackup only backs up data files and does not back up the data table structure (. frm), so this is a manual backup so that it can be used when the xtrabackup is restored.

Full-scale backup recovery

To prepare for a backup file before it is restored



Copy the corresponding database table structure from the backup directory to the default data directory



Delete the corresponding data files in the default data directory and copy the backed up data files to the default data directory



Modify Data Directory Permissions


Chown-r Mysql:mysql/data0/mysql/data

Restart MySQL


/data0/mysql/mysql restart

b) Incremental backup

Incremental backup Benefits:

1, the database is too large not enough space for full backup, make incremental backup effective space saving, and high efficiency.
2, support hot backup. The backup process does not lock the table and is not subject to time constraints and does not affect user use.
3, daily backup only produce a small amount of data, remote backup transfer more convenient. While saving space.
4, backup recovery based on file operations, reduce the risk of direct database operations.
5, the backup efficiency is higher, the recovery efficiency is higher.

This I study n long unsuccessful, the reason is not found yet. I test the environment of the MySQL version is 5.5.11, XTRABACKUP_55 is based on the MySQL5.5.9 source code compiled, do not know and this has a relationship? to be verified! Only the implementation steps are sorted out here.

Incremental backup and simulated recovery steps:

Do a full backup first, incremental backups need to be based on this


Copy the Code code as follows:

Ibdata1 xtrabackup_checkpoints Xtrabackup_logfile

Incremental backup based on this full-scale backup


Copy the Code code as follows:

xtrabackup_55--defaults-file=/data0/mysql/my.cnf--backup--target-dir=/data0/backup/mysql/delta-- Incremental-basedir=/data0/backup/mysql/base
Ibdata1.delta xtrabackup_checkpoints Xtrabackup_logfile

Note: In the incremental backup directory, the data files end with a. Delta. Incremental backups only back up the page that was modified after the last full backup, so incremental backups take up less space. Incremental backups can be incremental based on incremental backups. The incremental backup directory needs to be modified every time. For example, the second increment is changed to/data0/backup/mysql/delta2)  

Incremental backup recovery process:  

You first need to do a prepare operation for the full-volume, incremental backup separately. &NBSP


xtrabackup_55--defaults-file=/data0/mysql/my.cnf--prepare--target-dir=/data0/ backup/mysql/base 
xtrabackup_55--defaults-file=/data0/mysql/my.cnf--prepare--target-dir=/data0/backup /mysql/base--incremental-dir=/data0/backup/mysql/delta/ 

copies the corresponding database table structure from the backup directory to the default data directory  



Delete the corresponding data files in the default data directory and copy the backed up data files to the default data directory  

cp/data0/ backup/mysql/ib*/data0/mysql/data/ 

Modify Data directory permissions  

Chown-r mysql:mysql/data0/ mysql/data 

Restart mysql 

/data0/mysql/mysql restart 

Xtrabackup full backup, incremental backup, restore MySQL database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.