Use Xtrabackup to back up the mysql database, xtrabackupmysql

Source: Internet
Author: User
Tags percona

Use Xtrabackup to back up the mysql database, xtrabackupmysql

Use Xtrabackup to back up databases

Xtrabackup is an open source backup tool developed by percona, which is different from ibbackup. Ibbackup is a paid backup tool. In terms of its backup performance, ibbackup is inferior to Xtrabackup. Both ibbackup and Xtrabackup support both online physical full backup and online physical Incremental backup for the Innodb Storage engine. For MyISAM storage engine, only warm backup is supported. For more information, see http://www.percona.com/software/percona-xtrabackup.

 

Follow these steps to back up a mysql database using Xtrabackup:

I. Installation

1. Introduction
Xtrabackup is a mysql database backup tool provided by percona. According to the official introduction, Xtrabackup is the only open-source tool in the world that can hot backup innodb and xtradb storage engine databases. Xtradb is an enhanced version of Innodb, which is superior to Innodb in performance and has the following features:
(1) The backup process is fast and reliable;
(2) The backup process will not interrupt ongoing transactions;
(3) Saving disk space and traffic based on compression and other functions;
(4) automatic backup check;
(5) Fast Restoration;

 

2. Installation
Its latest software is available from the http://www.percona.com/software/percona-xtrabackup. This article is based on the RHEL5.8 system. Therefore, you can directly download the rpm package of the corresponding version to install it. here we will not describe the process.

After the installation is complete, A/usr/bin/innobackupex script file is generated, which encapsulates xtrabackup and can process Innodb and Myisam simultaneously. However, a read lock is required when processing Myisam.

 

3. About innobackupex

Innobackupex
Innobackupex is more powerful. It integrates xtrabackup and other functions. It not only supports full backup/recovery, but also supports Incremental backup and recovery based on time.

Innobackupex backup Principle
Innobackupex first calls xtrabackup to back up innodb data files. After xtrabackup is complete, innobackupex will view the xtrabackup_susponded file. Then, execute "flush tables with read lock" to back up other files.

Innobackupex recovery Principle
Innobackupex first reads my. cnf: Check that the directory corresponding to the variable (datadir, innodb_data_home_dir, innodb_data_file_path, innodb_log_group_home_dir) exists. After confirming that the directory exists, copy the myisam table and index first, then copy innodb tables, indexes, and logs

 

 

II. Implementation of backup

1. Full backup

# Innobackupex -- user = DBUSER -- password = DBUSERPASS/path/to/BACKUP-DIR/

If you want to use a user with the minimum permission for backup, you can create such users based on the following command:
Mysql> create user 'bkpuser' @ 'localhost' identified by 's3cret ';
Mysql> revoke all privileges, grant option from 'bkpuser'; (if an error occurs, do not add it)
Mysql> grant reload, lock tables, replication client on *. * TO 'bkpuser' @ 'localhost ';
Mysql> flush privileges;

 

When innobakupex is used for backup, it will call xtrabackup to back up all InnoDB tables and copy all the files related to table structure definition (. frm), and files related to MyISAM, MERGE, CSV, and ARCHIVE tables. Files related to triggers and database configuration information are also backed up. These files will be saved to a time-based directory.

 

During backup, innobackupex creates the following files in the backup directory:
(1) xtrabackup_checkpoints -- Backup Type (such as full or incremental), backup status (such as whether it is already in prepared status), And LSN (log serial number) range information;

Each InnoDB page (usually 16 KB) contains a log serial number, that is, the LSN. The LSN is the system version number of the entire database system. The LSN related to each page can indicate how the page has changed recently.

(2) xtrabackup_binlog_info -- the binary log file currently in use by the mysql server and the location of the binary log event until the moment of backup.

(3) xtrabackup_binlog_pos_innodb -- the current position of the binary log file used for InnoDB or XtraDB tables.

(4) xtrabackup_binary -- The xtrabackup executable file used in backup;

(5) backup-my.cnf-the configuration option information used by the BACKUP command;

When innobackupex is used for backup, you can also use the -- no-timestamp Option to prevent the command from automatically creating a directory named after time. As a result, the innobackupex command creates a BACKUP-DIR directory to store backup data.

 

 

2. prepare (prepare) a full backup

Generally, after the backup is complete, the data cannot be used for restoration, because the backup data may contain uncommitted transactions or transactions that have been committed but not yet synchronized to the data file. Therefore, the data files are still inconsistent. The main function of "Preparation" is to roll back uncommitted transactions and synchronize committed transactions to data files, so that the data files are in a consistent state.

The -- apply-log option of the innobakupex command can be used to implement the above functions. Run the following command:
# Innobackupex -- apply-log/path/to/BACKUP-DIR

If the execution is correct, the following lines of information are usually output:

Xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1640470
020124 06:22:05 innobackupex: completed OK!

 

During the "Preparation" process, innobackupex can also use the -- use-memory option to specify the memory size that can be used. The default value is usually 100 MB. If enough memory is available, you can allocate more memory to the prepare process to speed up its completion.

 


 

3. Restore data from a full backup. When restoring data, the mysql server can be offline)

The -- copy-back option of the innobackupex command is used to perform the Restoration Operation. It copies all data-related files to the DATADIR directory of the mysql server to execute the restoration process. Innobackupex gets information about the DATADIR directory through a backup-my.cnf.

# Innobackupex -- copy-back/path/to/BACKUP-DIR

If the execution is correct, the last few lines of the output information are usually as follows:

Innobackupex: Starting to copy InnoDB log files
Innobackupex: in '/tmp/backup/2002-01-24_06-09-50'
Innobackupex: back to original InnoDB log directory '/mydata/data'
Innobackupex: Copying '/tmp/backup/2002-01-24_06-09-50/ib_logfile0' to '/mydata/data/ib_logfile0'
Innobackupex: Copying '/tmp/backup/2002-01-24_06-09-50/ib_logfile1' to '/mydata/data/ib_logfile1'
Innobackupex: Finished copying back files.

020124 06:29:22 innobackupex: completed OK!

Make sure that "innobackupex: completed OK!" appears in the row at the top of the preceding information !".

After the data is restored to the DATADIR directory, make sure that the owner and group of all data files are correct, such as mysql. Otherwise, before starting mysqld, you must modify the owner and group of the data file in advance. For example:

# Chown-R mysql: mysql/mydata/data/

If you do not modify the owner and group of the data directory and all the files in the directory, mysqld cannot be started. The following error message is displayed:

Starting MySQL... The server quit without updating PID file [FAILED]/data/localhost. pid ).

 

 

4. Use innobackupex for Incremental Backup

Each InnoDB page contains an LSN. When the related data changes, the LSN of the related page automatically increases. This is the foundation for InnoDB tables to perform Incremental backup, that is, innobackupex is implemented by backing up pages that have changed since the last full backup.

To implement the first Incremental backup, run the following command:

# Innobackupex -- incremental/backup -- incremental-basedir = BASEDIR


Here, BASEDIR refers to the directory where the full backup is located. After this command is executed, the innobackupex command creates a new directory named after time in the/backup Directory to store all Incremental backup data. In addition, when an incremental backup is performed again after the incremental backup is executed, its -- incremental-basedir should refer to the directory where the last incremental backup is located.

Note that Incremental Backup can only be applied to InnoDB or XtraDB tables. For MyISAM tables, full backup is actually performed during Incremental backup.


"Prepare" (prepare) Incremental backup is somewhat different from "prepare" Full backup, especially note the following:
(1) On each backup (including full and Incremental Backup), you must "replay" The committed transactions ". After "replay", all the backup data will be merged to the full backup.
(2) Roll Back uncommitted transactions based on all backups ".

 

Therefore, the operation becomes:
# Innobackupex -- apply-log -- redo-only BASE-DIR

Run the following command:
# Innobackupex -- apply-log -- redo-only BASE-DIR -- incremental-dir = INCREMENTAL-DIR-1

Next is the second increment:
# Innobackupex -- apply-log -- redo-only BASE-DIR -- incremental-dir = INCREMENTAL-DIR-2

 

Where the BASE-DIR refers to the directory where the full backup is located, while the INCREMENTAL-DIR-1 refers to the directory of the first Incremental backup, The INCREMENTAL-DIR-2 refers to the directory of the second Incremental backup, and so on, that is, if multiple incremental backups exist, the above operations must be performed each time;

 

Finally, execute the following command during all (full backup and Incremental Backup) recovery:

# Innobackupex -- copy-back/path/to/BACKUP-DIR

/Path/to/The BACKUP-DIR is the directory where the full backup is located

 

 

5. "stream" and "backup compression" functions of Xtrabackup

Xtrabackup supports the "stream" function for the backup data files, that is, the backup data can be transmitted to the tar program through STDOUT for archiving, rather than being saved to a backup directory by default. To use this function, you only need to use the -- stream option. For example:

# Innobackupex -- stream = tar/backup | gzip>/backup/'date between 0000f_0000h-0000m-0000s'.tar.gz

You can even use the following command to back up data to other servers:
# Innobackupex -- stream = tar/backup | ssh user@www.magedu.com "cat->/backups/'date too f_0000h-0000m-0000s'.tar"

In addition, you can use the -- parallel option to copy multiple files in parallel when performing a local backup. This option is used to specify the number of threads started during replication. Of course, you need to enable the innodb_file_per_table option or the shared tablespace is stored in multiple ibdata files through the innodb_data_file_path option when backing up data. This function cannot be used to copy multiple files in a database. The method is as follows:
# Innobackupex -- parallel/path/to/backup

At the same time, innobackupex backup data files can also be stored to remote hosts, which can be achieved using the -- remote-host option:
# Innobackupex -- remote-host = root@www.magedu.com/path/IN/REMOTE/HOST/to/backup
 

6. Import or export a single table

By default, InnoDB tables cannot be transplanted between mysql servers by directly copying table files, even if the innodb_file_per_table option is used. The Xtrabackup tool can be used to implement this function. However, you need to enable the innodb_file_per_table option on the mysql server of the "Export" table (strictly speaking, the table to be "exported" is before it is created, the innodb_file_per_table option is enabled on the mysql server. The innodb_file_per_table and innodb_expand_import options are also enabled on the "import" Table server.

(1) "Export" table
The export table is in the backup prepare phase. Therefore, once the full backup is completed, you can use the -- export option to export a table during the prepare process:
# Innobackupex -- apply-log -- export/path/to/backup

This command creates a file ending with. exp for the tablespace of each innodb table. These files ending with. exp can be imported to other servers.

(2) "import" table
To import an innodb table from another server on the mysql server, you must first create a table with the same structure as the original table on the current server before importing the table:
Mysql> create table mytable (...) ENGINE = InnoDB;

Delete the tablespace of the table:
Mysql> alter table mydatabase. mytable discard tablespace;

Next, copy the mytable. ibd and mytable. exp files of the mytable table from the "Export" Table server to the data directory of the current server, and then run the following command to "import" it ":
Mysql> alter table mydatabase. mytable import tablespace;

 

7. Use Xtrabackup to partially back up the database

Xtrabackup can also be used for partial backup, that is, to back up only one or more specified databases or tables in a database. To use this function, you must enable the innodb_file_per_table option, that is, each table is saved as an independent file. At the same time, it does not support the -- stream option, that is, it does not support transmitting data to other programs through pipelines for processing.

In addition, the restoration of some backups is different from the restoration of all data backups, that is, you cannot simply use the -- copy-back option to copy some of the prepared backups back to the data directory directly, instead, we need to restore the data by importing the table. Of course, in some cases, some backups can be restored directly through -- copy-back, but most of the data restored in this way will cause data inconsistency, this method is not recommended in any case.

(1) Create partial backup

There are three ways to create partial backup: Regular Expressions (-- include), enumeration table files (-- tables-file), and list databases to be backed up (-- databases ).

(A) Use -- include
When using -- include, you must specify the full name of the table to be backed up, such as databasename. tablename, for example:
# Innobackupex -- include = '^ mageedu [.] tb1'/path/to/backup

(B) Use -- tables-file
The parameter of this option must be a file name. Each line in this file contains the complete name of the table to be backed up; for example:
# Echo-e 'mageedu. tb1 \ nmageedu. tb2 '>/tmp/tables.txt
# Innobackupex -- tables-file =/tmp/tables.txt/path/to/backup

(C) Use -- databases
The parameter accepted by this option is the data name. If you want to specify multiple databases, they must be separated by spaces. When you specify a database, you can also specify only one of the tables. In addition, this option can also take a file as the parameter, each row of the file is an object to be backed up. For example:
# Innobackupex -- databases = "mageedu testdb"/path/to/backup

(2) prepare partial backup
The prepare backup process is similar to the table export process. Use the -- export option:
# Innobackupex -- apply-log -- export/pat/to/partial/backup

During the execution of this command, innobackupex will call the xtrabackup command to remove the missing table from the data dictionary. Therefore, many warnings about the "table does not exist" class are displayed. At the same time, information about creating the. exp file for the table in the backup file is displayed.

(3) Restore partial backup
The process of restoring some backups is the same as that of importing tables. Of course, you can also directly copy the backup in the prepared state to the data directory for restoration. However, the data directory must be consistent at this time.

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.