[MySQL] innobackupex在线备份及恢复(全量和增量)
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
yum install cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr bison libtool ncurses-devel zlib-devel
tar -zxvf percona-xtrabackup-2.1.5.tar.gz cd percona-xtrabackup-2.1.5
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
./utils/build.sh innodb56
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
全备及其恢复
全备:
执行如下语句进行全备:
innobackupex --defaults-file=/opt/mysql/my.cnf --user=root --password=*** /backup/mysql/data
备份成功后,将在备份目录下创建一个时间戳目录(本例创建的目录为/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
从什么可以看出,恢复分为两个步骤,第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
上面语句执行成功之后,会在--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
backup_type = incremental from_lsn = 563759005914 to_lsn = 574765133284 last_lsn = 574765133284
那么,我们是否可以在增量备份的基础上再做增量备份呢?答案是肯定的,只要把--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
backup_type = incremental from_lsn = 574765133284 to_lsn = 574770200380 last_lsn = 574770200950
恢复:
增量备份的恢复比全备要复杂很多,第一步是在所有备份目录下重做已提交的日志,如:
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
这里要注意的是:最后一步的增量备份并没有--redo-only选项!还有,可以使用--use_memory提高性能。
以上语句执行成功之后,最终数据在BASE-DIR(即全备目录)下。
第一步完成之后,我们开始第二步:回滚未完成的日志:
innobackupex --apply-log BASE-DIR
innobackupex --copy-back BASE-DIR
常见错误及解决方法
错误:
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.
yum -y install perl-DBD-MySQL.x86_64
错误:
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.
cp xtrabackup_innodb55 xtrabackup_55

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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.

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 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.

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.

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.

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

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.
