Table of Contents
安装
全备及其恢复
增量备份及其恢复
常见错误及解决方法
Home Database Mysql Tutorial [MySQL] innobackupex在线备份及恢复(全量和增量)

[MySQL] innobackupex在线备份及恢复(全量和增量)

Jun 07, 2016 pm 03:39 PM
mysql online backup recover

Xtrabackup是由percona开发的一个开源软件,它是innodb热备工具ibbackup(收费的商业软件)的一个开源替代品。Xtrabackup由个部分组成:xtrabackup和innobackupex,其中xtrabackup工具用于备份innodb和 xtraDB引擎的表;而innobackupex工具用于备份myisam和in

Xtrabackup是由percona开发的一个开源软件,它是innodb热备工具ibbackup(收费的商业软件)的一个开源替代品。Xtrabackup由个部分组成:xtrabackup和innobackupex,其中xtrabackup工具用于备份innodb和 xtraDB引擎的表;而innobackupex工具用于备份myisam和innodb引擎的表,本文将介绍如何用innobackupex工具做全量和增量备份。

官网:http://www.percona.com/docs/wiki/percona-xtrabackup:start

安装

声明:以下操作最好以mysql用户执行。

首先,通过wget下载源码tar包:

wget http://www.percona.com/redir/downloads/XtraBackup/LATEST/source/percona-xtrabackup-2.1.5.tar.gz
Copy after login
安装依赖包:
yum install cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr bison libtool ncurses-devel zlib-devel
Copy after login
解压缩tar:
tar -zxvf percona-xtrabackup-2.1.5.tar.gz
cd percona-xtrabackup-2.1.5
Copy after login

utils/build.sh脚本会根据指定的引擎版本,自动解压缩适当的MySQL源码包并进行编译,这是最简单的安装方式。当你在命令行下不带任何参数执行该脚本时,出现如下提示:

[mysql@epay100 ~/software/percona-xtrabackup-2.1.5 ]$ ./utils/build.sh
Build an xtrabackup binary against the specified InnoDB flavor.

Usage: build.sh CODEBASE
where CODEBASE can be one of the following values or aliases:
  innodb51         | plugin                build against InnoDB plugin in MySQL 5.1
  innodb55         | 5.5                   build against InnoDB in MySQL 5.5
  innodb56         | 5.6,xtradb56,         build against InnoDB in MySQL 5.6
                   | mariadb100
  xtradb51         | xtradb,mariadb51      build against Percona Server with XtraDB 5.1
                   | mariadb52,mariadb53
  xtradb55         | galera55,mariadb55    build against Percona Server with XtraDB 5.5
Copy after login
根据上面提示和你使用的存储引擎及版本,选择相应的参数即可。因为我用的是MySQL 5.6,所以执行如下语句安装:
./utils/build.sh innodb56
Copy after login
以上语句执行成功后,表示安装完成。最后,把生成的二进制文件拷贝到一个自定义目录下(本例中为/home/mysql/admin/bin/percona-xtrabackup-2.1.5),并把该目录放到环境变量PATH中。
cp ./innobackupex /home/mysql/admin/bin/percona-xtrabackup-2.1.5
cp ./src/xtrabackup_56 ./src/xbstream /home/mysql/admin/bin/percona-xtrabackup-2.1.5  
Copy after login

全备及其恢复

全备:

执行如下语句进行全备:

innobackupex --defaults-file=/opt/mysql/my.cnf  --user=root --password=*** /backup/mysql/data
Copy after login
该语句将拷贝数据文件(由my.cnf里的变量datadir指定)至备份目录下(/backup/mysql/data),注意:如果不指定--defaults-file,默认值为/etc/my.cnf。

备份成功后,将在备份目录下创建一个时间戳目录(本例创建的目录为/backup/mysql/data/2013-10-29_09-05-25),在该目录下存放备份文件。


恢复:

恢复之前,要先关闭数据库,并且删除数据文件和日志文件。

innobackupex --defaults-file=/opt/mysql/my.cnf --user=root --password=*** --use-memory=4G --apply-log /backup/mysql/data/2013-10-29_09-05-25
innobackupex --defaults-file=/opt/mysql/my.cnf --user=root --password=***  --copy-back /backup/mysql/data/2013-10-29_09-05-25
Copy after login

从什么可以看出,恢复分为两个步骤,第1步是apply-log,为了加快速度,一般建议设置--use-memory,这个步骤完成之后,目录/backup/mysql/data/2013-10-29_09-05-25下的备份文件已经准备就绪。

第2步是copy-back,即把备份文件拷贝至原数据目录下。

恢复完成之后,一定要记得检查数据目录的所有者和权限是否正确。


增量备份及其恢复

注意:innobackupex 增量备份仅针对InnoDB这类支持事务的引擎,对于MyISAM等引擎,则仍然是全备。


增量备份:

增量备份需要基于全备,先假设我们已经有了一个全备(/backup/mysql/data/2013-10-29_09-05-25),我们需要在该全备的基础上做增量备份。

innobackupex --defaults-file=/opt/mysql/my.cnf  --user=root --password=*** --incremental-basedir=/backup/mysql/data/2013-10-29_09-05-25 --incremental /backup/mysql/data
Copy after login
其中--incremental-basedir指向全备目录,--incremental指向增量备份的目录。

上面语句执行成功之后,会在--incremental执行的目录下创建一个时间戳子目录(本例中为:/backup/mysql/data/2013-10-29_09-52-37),在该目录下存放着增量备份的所有文件。

在备份目录下,有一个文件xtrabackup_checkpoints记录着备份信息,全备的信息如下:

backup_type = full-backuped
from_lsn = 0
to_lsn = 563759005914
last_lsn = 563759005914
Copy after login
基于该全备的增量备份的信息如下:

backup_type = incremental
from_lsn = 563759005914
to_lsn = 574765133284
last_lsn = 574765133284
Copy after login
从上面可以看出,增量备份的from_lsn正好等于全备的to_lsn。
那么,我们是否可以在增量备份的基础上再做增量备份呢?答案是肯定的,只要把--incremental-basedir执行上一次增量备份的目录即可,如下所示:

innobackupex --defaults-file=/opt/mysql/my.cnf  --user=root --password=*** --incremental-basedir=/backup/mysql/data/2013-10-29_09-52-37 --incremental /backup/mysql/data
Copy after login
它的xtrabackup_checkpoints记录着备份信息如下:

backup_type = incremental
from_lsn = 574765133284
to_lsn = 574770200380
last_lsn = 574770200950
Copy after login
可以看到,该增量备份的from_lsn是从上一次增量备份的to_lsn开始的。


恢复:


增量备份的恢复比全备要复杂很多,第一步是在所有备份目录下重做已提交的日志,如:

innobackupex --apply-log --redo-only BASE-DIR
innobackupex --apply-log --redo-only BASE-DIR --incremental-dir=INCREMENTAL-DIR-1
innobackupex --apply-log BASE-DIR --incremental-dir=INCREMENTAL-DIR-2
Copy after login
其中BASE-DIR是指全备目录,INCREMENTAL-DIR-1是指第一次的增量备份,INCREMENTAL-DIR-2是指第二次的增量备份,以此类推。

这里要注意的是:最后一步的增量备份并没有--redo-only选项!还有,可以使用--use_memory提高性能。

以上语句执行成功之后,最终数据在BASE-DIR(即全备目录)下。

第一步完成之后,我们开始第二步:回滚未完成的日志:

innobackupex --apply-log BASE-DIR
Copy after login
上面执行完之后,BASE-DIR里的备份文件已完全准备就绪,最后一步是拷贝:

innobackupex --copy-back BASE-DIR
Copy after login
同样地,拷贝结束之后,记得检查下数据目录的权限是否正确。

常见错误及解决方法

错误:

131028 17:45:57  innobackupex: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_group=xtrabackup' (using password: NO).
innobackupex: Error: Failed to connect to MySQL server as DBD::mysql module is not installed at /home/mysql/admin/bin/percona-xtrabackup-2.1.5/innobackupex line 2913.
Copy after login
解决方法:

yum -y install perl-DBD-MySQL.x86_64
Copy after login

错误:

sh: xtrabackup_55: command not found
innobackupex: Error: no 'mysqld' group in MySQL options at /home/mysql/admin/bin/percona-xtrabackup-2.1.6/innobackupex line 4341.
Copy after login
解决方法:
cp xtrabackup_innodb55 xtrabackup_55
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles