search
HomeDatabaseMysql TutorialXtraBackup与InnoBackupex 联机备份

专注于InnoDB、XtraBackup的热备工具,是C语言开发的程序,专用于备份InnoDB及XtraDB引擎对象

XtraBackup联机备份:

[root@mysql1 /]# rpm -ivh percona-xtrabackup-2.2.3-4982.el6.x86_64.rpm

[mysql@mysql1 ~]$ echo "export PATH=/usr/bin:\$PATH" >> /home/mysql/.bash_profile

(system@localhost) [(none)]> create user xtrabk@'localhost' identified by 'onlybackup';

(system@localhost) [(none)]> grant reload,lock tables,Replication client,super on *.* to xtrabk@'localhost';

[mysql@mysql1 ~]$ xtrabackup -help

使用xtrabackup命令进行备份

xtrabackup命令有两种模式,--backup(备份模式)和--prepara(恢复准备模式)

--backup指定当前的操作模式,backup就是说要创建备份集

--target-dir指定备份集的存储路径

--defaults-file从MySQL的选项文件中读取参数

[mysql@mysql1 ~]$ xtrabackup --defaults-file=/data/mysqldata/3306/my.cnf --backup --target-dir=/data/mysqldata/backup/full_bak

 

使用innobackupex命令进行备份,还能备份非InnoDB对象,innobackupex内部调用xtrabackup进行备份

[mysql@mysql1 ~]$ innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=xtrabk --password='onlybackup' /data/mysqldata/backup/

创建增量备份:

在上一步全量备份完成后,修改InnoDB表的数据,进行第一次增量备份

[mysql@mysql1 ~]$ innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=xtrabk --password='onlybackup' --incremental --incremental-basedir=/data/mysqldata/backup/2014-07-10_09-29-32 /data/mysqldata/backup_inc

1)在做完第一次增量备份后,继续修改InnoDB表的数据,进行第二次增量备份

[mysql@mysql1 ~]$ innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=xtrabk --password='onlybackup' --incremental --incremental-basedir=/data/mysqldata/backup/2014-07-10_09-29-32 /data/mysqldata/backup_inc

此种方式对应Oracle的累积增量备份

2)在做完第一次增量备份后,继续修改InnoDB表的数据,进行第二次增量备份

[mysql@mysql1 ~]$ innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=xtrabk --password='onlybackup' --incremental --incremental-basedir=/data/mysqldata/backup_inc/2014-07-10_09-31-36 /data/mysqldata/backup_inc

此种方式对应Oracle的差异增量备份

--incremental:告诉xtrabackup这次是要创建增量备份

--incremental-basedir:指定一个全量备份的路径,作为增量备份的基础

--incremental-lsn:指定备份开始时的LSN

 

InnoDB的Log Sequence Number日志序列号LSN。InnoDB的每个页(Page)都保存有LSN,这个序号能够标识该页最后修改时间,增量备份正是根据这个LSN来的,因为每次备份(含xtrabackup和innobackupex),XtraBackup都会在备份集中创建一个xtrabackup_checkpoints文件,这个文件中的内容就记录了最后修改的LSN序号。创建增量备份集时,只需要从上次的备份集中找到xtrabackup_checkpoints文件,读取最新的LSN,而后在创建增量是,选择LSN大于这个序号的页,以及这期间产生的二进制日志就可以了,甚至不需要对比全量备份集和当前数据库的数据文件

 

XtraBackup备份工作机制:

XtraBackup本质是基于InnoDB的故障恢复(crash-recovery)机制,先复制InnoDB的数据文件,复制的时候数据仍有可能正在读写,复制出的文件可能是不一致的状态,所以在备份过程中,需要定时扫描日志并作记录,而后通过备份的日志文件执行故障恢复,使文件恢复到一个一致的状态,使数据库达到可用状态。核心就是InnoDB维护的重做日志(redo log)。XtraBackup会在启动时先记录下当前的日志序列号(LSN),然后开始复制数据文件,同时XtraBackup运行一个后台进程,监控着事务日志,并复制新发生的修改。这项操作会在XtraBackup备份执行过程中一直执行,就是log scanned up to信息,以确保记录下所有备份期间数据库发生的修改。接下来是准备进程(prepare process),在这一步中,XtraBackup对复制的数据文件执行故障恢复,将数据库恢复到可用状态

 

执行恢复:全量恢复和增量恢复:

准备恢复(prepare):就是为恢复做准备。备份集没有办法直接拿来用,所有需要一个对备份集做准备的过程

对于xtrabackup对应的参数是--prepare,对于innobackupex对应的参数是--apply-log

执行恢复(copy-back):备份集准备好以后,可以执行恢复了

对于xtrabackup没有特殊说明,简单cp/mv过程,对于innobackupex对应的参数是--copy-back,它的功能是将制定的备份集,恢复到指定的路径下

 

首先对全量备份做恢复的准备工作

[mysql@mysql1 ~]$ innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --apply-log --redo-only /data/mysqldata/backup/2014-07-10_09-29-32/

--apply-log从指定的选项文件中读取配置信息并应用日志等,表示要做的是对备份集做恢复的准备工作

--redo-only如果进行准备工作的备份集操作完成后,还有其他增量备份集待处理,那么就必须指定本参数

 

继续执行innobackupex命令,应用增量备份,这次要操作的备份集不是最后一份,需要再指定--redo-only参数

[mysql@mysql1 ~]$ innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --apply-log /data/mysqldata/backup/2014-07-10_09-29-32/ --incremental-dir=/data/mysqldata/backup_inc/2014-07-10_09-31-36 /

 

继续执行innobackupex命令,,应用增量备份,这次要操作的备份集就是最后一份,不需要再指定--redo-only参数

[mysql@mysql1 ~]$ innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --apply-log /data/mysqldata/backup/2014-07-10_09-29-32/ --incremental-dir=/data/mysqldata/backup_inc/2014-07-10_09-35-13/

注意在备份集进行恢复的准备过程中,不要随意中断该任务,否则有可能导致备份集处于不一致状态。由于XtraBackup是直接在备份集中进行准备,一旦有异常,搞不好想恢复都没办法。建议操作之前,将备份集备份一次。

建议再执行一遍innobackupex --apply-log

[mysql@mysql1 ~]$ innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --apply-log  /data/mysqldata/backup/2014-07-10_09-29-32/

执行文件的恢复

[mysql@mysql1 ~]$ innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --copy-back /data/mysqldata/backup/2014-07-10_09-29-32/

 

恢复操作可以在任意机器上执行,只要您拥有完整备份集,可以把XtraBackup当做数据库迁移工具

 

打包和压缩备份集

XtraBackup支持流(stream)模式,能够直接将备份输出到指定的格式进行处理,比如tar或xbstream

 

[mysql@mysql1 ~]$ innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=xtrabk --password=onlybackup --stream=tar /tmp | gzip -> /data/mysqldata/backup/xtra_fullbackup.tar.gz

全备

innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=xtrabk --password='onlybackup' /data/mysqldata/backup/

 

修改数据

(system@localhost) [mysql]> create table mytable(a int);

Query OK, 0 rows affected (0.06 sec)

(system@localhost) [mysql]> insert into mytable values (1);

Query OK, 1 row affected (0.04 sec)

(system@localhost) [mysql]> insert into mytable values (2);

Query OK, 1 row affected (0.01 sec)

(system@localhost) [mysql]> insert into mytable values (3);

Query OK, 1 row affected (0.00 sec)

(system@localhost) [mysql]> insert into mytable values (4);

Query OK, 1 row affected (0.00 sec)

(system@localhost) [mysql]> insert into mytable values (5);

Query OK, 1 row affected (0.01 sec)

 

进行第一增量备份

Statement
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
When should you use a composite index versus multiple single-column indexes?When should you use a composite index versus multiple single-column indexes?Apr 11, 2025 am 12:06 AM

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)Apr 10, 2025 am 09:36 AM

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL: Essential Skills for DevelopersMySQL and SQL: Essential Skills for DevelopersApr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Describe MySQL asynchronous master-slave replication process.Describe MySQL asynchronous master-slave replication process.Apr 10, 2025 am 09:30 AM

MySQL asynchronous master-slave replication enables data synchronization through binlog, improving read performance and high availability. 1) The master server record changes to binlog; 2) The slave server reads binlog through I/O threads; 3) The server SQL thread applies binlog to synchronize data.

MySQL: Simple Concepts for Easy LearningMySQL: Simple Concepts for Easy LearningApr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL: A User-Friendly Introduction to DatabasesMySQL: A User-Friendly Introduction to DatabasesApr 10, 2025 am 09:27 AM

The installation and basic operations of MySQL include: 1. Download and install MySQL, set the root user password; 2. Use SQL commands to create databases and tables, such as CREATEDATABASE and CREATETABLE; 3. Execute CRUD operations, use INSERT, SELECT, UPDATE, DELETE commands; 4. Create indexes and stored procedures to optimize performance and implement complex logic. With these steps, you can build and manage MySQL databases from scratch.

How does the InnoDB Buffer Pool work and why is it crucial for performance?How does the InnoDB Buffer Pool work and why is it crucial for performance?Apr 09, 2025 am 12:12 AM

InnoDBBufferPool improves the performance of MySQL databases by loading data and index pages into memory. 1) The data page is loaded into the BufferPool to reduce disk I/O. 2) Dirty pages are marked and refreshed to disk regularly. 3) LRU algorithm management data page elimination. 4) The read-out mechanism loads the possible data pages in advance.

MySQL: The Ease of Data Management for BeginnersMySQL: The Ease of Data Management for BeginnersApr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use