search
HomeDatabaseMysql Tutorial多实例下percona-xtrbackup使用(2014-11-10)

背景说明 percona-xtrabackup是由percona公司开发的备份工具,主要有两个工具,一个是xtrabackup,另一个是innobackupex。其中中innobackupex是对xtrabackup封装,是一个perl脚本。本文操作相对比较简单,通过innobackupex将3306实例的数据进行备份,再恢复

背景说明

percona-xtrabackup是由percona公司开发的备份工具,主要有两个工具,一个是xtrabackup,另一个是innobackupex。其中中innobackupex是对xtrabackup封装,是一个perl脚本。本文操作相对比较简单,通过innobackupex将3306实例的数据进行备份,再恢复到3307实例上。同时也简单的介绍下通过这个备份恢复数据。

安装

percona-xtrbackup可以使用二进制、源码、yum安装,本文主要使用yum安装,步骤如下:
 yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm 
 yum install percona-xtrabackup.x86_64
 yum search percona
其他安装可以查看官网: http://www.percona.com/doc/percona-xtrabackup/2.2/installation.html http://www.percona.com/doc/percona-xtrabackup/2.2/installation/compiling_xtrabackup.html

相关准备

创建备份目录

cd /
mkdir data
cd data
mkdir mkdir backup
在backup目录下,创建三个目录:mkdir {conf,incremental,full}
三个目录具体功能如下: conf:存放自定义的my.cnf配置信息 full:存放首次全量备份数据
incremental:存放增量备份数据 备份my.cnf到conf目录
cp /etc/my.cnf /data/backup/conf/3306.cnf
cp /etc/my.cnf /data/backup/conf/3307.cnf
3306.cnf原样保存即可,3307.cnf需要进行修改,在[mysqld]节点下添加"datadir=/data/mysql/mysql_3307/data/"。以方便数据恢复时使用。 常用参数说明:
--user: mysql用户
--password: 用户密码
--defaults-file:  指定my.cnf文件路径,若不指定则读取mysql默认的my.cnf文件
--socket:mysql实例对应的socket文件

备份实操作

1. 全量备份

首次备份为全量备份,也是增量备份的基础。
innobackupex --user=root --password=123456 --socket=/tmp/mysql_3306.sock  --defaults-file=/data/backup/conf/3306.cnf  /data/backup/full/ 
首次将数据库的所有数据备份到/data/backup/full/目录,在/data/backup/full/ 目录下将生成一个当前时间戳的子目录,如图1。若要不生成时间戳的子目录,可以使用--no-timestamp参数,使其不自动生成时间戳的子目录,所以备份数据将存储在/data/backup/full/ 下。 全备只需指定用于备份的用户名、密码和备份路径即可,最后出现innobackupex: completed OK! 则代表备份成功。

\

图1

全备后的目录文件,如图2。

\

图2

mysql的data目录下的文件,如图3。

\

图3

可以对比图2、图3的目录文件,xtrabackup生成的文件有backup-my.cnf、xtrabackup_checkpoints、xtrabackup_info、xtrabackup_lofile。

 

文件说明:

backup-my.cnf: 主要是记录innobackupex中使用到Mysql参数。

 

# This MySQL options file was generated by innobackupex. 

# The MySQL server 
[mysqld] 
innodb_checksum_algorithm=innodb 
innodb_data_file_path=ibdata1:12M:autoextend 
innodb_log_files_in_group=2 
innodb_log_file_size=50331648 
innodb_page_size=16384 
innodb_undo_directory=. 
innodb_undo_tablespaces=0
xtrabackup_checkpoints: 记录备份类型及开始及结束的lsn位置。backup_type 有两种full-prepared (全备)、incremental (增备)。
backup_type = full-prepared 
from_lsn = 0 
to_lsn = 8234580547 
last_lsn = 8234580547 
compact = 0
xtrabackup_info: 记录mysql相关信息。
uuid = 3d090541-6649-11e4-bb2a-000c295bd3a3 
name = 
tool_name = innobackupex 
tool_command = --user=root --password=... --incremental /data/backup/incremental/ --incremental-base=/data/backup/incremental/2014-11-07_14-24-54/ --defaults-file =/data/backup/conf/3306.cnf --socket=/tmp/mysql_3306.sock 
tool_version = 1.5.1-xtrabackup 
ibbackup_version = xtrabackup version 2.2.6 based on MySQL server 5.6.21 Linux (x86_64) (revision id: ) 
server_version = 5.6.21-log 
start_time = 2014-11-07 14:41:52 
end_time = 2014-11-07 14:42:27 
lock_time = 2 
binlog_pos = 
innodb_from_lsn = 8234579864 
innodb_to_lsn = 8234580547 
partial = N 
incremental = Y 
format = file 
compact = N 
compressed = N
xtrabackup_logfile: xtrabackup自己的日志文件,新版本中不直接可见。

2.制造新数据

创建表,并写入数据,作为新增的数据。
use test;
create table t3(col1 int,col2 int);
写入如下新数据:
insert into t3(col1,col2)value(1,1);
insert into t3(col1,col2)value(2,1);
insert into t3(col1,col2)value(3,1);
insert into t3(col1,col2)value(4,1);
insert into t3(col1,col2)value(5,1);
insert into t3(col1,col2)value(6,1);
insert into t3(col1,col2)value(7,1);
insert into t3(col1,col2)value(8,1);
insert into t3(col1,col2)value(9,1);
insert into t3(col1,col2)value(10,1);

注:可以删除、更新数据,再观察首次增量备份后的目录下的表文件,与全量备份的表文件进行对比,分析不同之处。

3. 第一次增量备份

第二次备份即首次增量备份,增量备份是在上次备份的基础上对最新的数据进行备份。语句如下:
innobackupex --user=root --password=123456 --socket=/tmp/mysql_3306.sock  --defaults-file=/data/backup/conf/3306.cnf  --incremental /data/backup/incremental/ --incremental-basedir=/data/backup/full/2014-11-07_14-06-47
参数说明:
--incremental :指定存储本次增量备份的目录
--incremental-basedir:上次备份的存储目录
完成首次的增量备份后,在指定目录下也会生成一个新的目录,目录中的文件与全量备份文件部分不一样。

4. 第二次增量备份

在第二次备份(增备)的基础上,再进行备份。

innobackupex --user=root --password=123456 --socket=/tmp/mysql_3306.sock  --defaults-file=/data/backup/conf/3306.cnf  --incremental /data/backup/incremental/  --incremental-basedir=/data/backup/incremental/2014-11-07_14-21-25

恢复操作

1. 对全量备份进行操作

了解两个参数 :
--apply-log :创建新的事务日志,从backup-my.cnf文件中读取innodb配置信息。
--redo-only:只读已提交的事务,在最后一次增量合并时,不需要填写这个参数。
全量备份恢复前准备
innobackupex  --apply-log --redo-only /data/backup/full/2014-11-07_14-06-47

2. 将第一次增量备份的数据合并到全量备份中

innobackupex  --apply-log --redo-only  /data/backup/full/2014-11-07_14-06-47 --incremental-dir=/data/backup/incremental/2014-11-07_14-21-25

3.将第二次全量备份的数据合并到全量备份中

innobackupex --apply-log  /data/backup/full/2014-11-06_14-52-38/ --incremental-dir=/data/backup/incremental/2014-11-07_14-23-52
注:最后一次的合并操作中不需要添加--redo-only参数。

4.停止mysql服务

恢复时需要停掉MySQL,所以我们停掉MySQL3307实例。
/usr/local/mysql/bin/mysqld_multi stop 3307
如果无法停止mysql,执行kill -9 mysql3307实例的进程。

5. 恢复数据。

innobackupex --defaults-file=/data/backup/conf/3307.cnf --copy-back /data/backup/full/2014-11-06_14-52-38/ 

6.权限设置

对mysql的data目录进行权限设置。
chown -R mysql:mysql   /data/mysql/mysql_3307/data

7.启动mysql3307实例

/usr/local/mysql/bin/mysqld_mutil start 3306
查看实例是否被启动:
/usr/local/mysql/bin/mysqld_mutil report

8. 查看是否恢复成功

登录数据库核对两个数据库中相关表的数据是否一致。

另外也可以通过这个备份恢复3306的数据,通过如下操作模拟数据丢失情况。

1 .模拟数据丢失

drop database test;

2. 停止3306实例

3.恢复数据

<span style="font-size:14px;">innobackupex --defaults-file=/data/backup/conf/3306.cnf --copy-back /data/backup/full/2014-11-06_14-52-38/ </span>

4. mysql的data目录进行权限设置

chown -R mysql:mysql   /data/mysql/mysql_3307/data 

5.启动3306实例

查看test数据库是所有表及数据都被恢复。

注:

1. 在备份的时候如果磁盘空间还足够的话,建议每个备份都再做一个副本,防止备份数据异常。

2. 进行恢复的时候一定要注意合并的先后顺序,如全备->增量备2->增量备份2,先后顺序不能乱,否则将使用数据不一致。

碰到的问题及解决方法

1. innobackupex: Error: Original data directory 'XXX' is not empty!

需要删除之前的data目录下的,我们可以对原有的data目录进行重命名。

mv /data/mysql/mysql_3307/data /data/mysql/mysql_3307/data_bak_20141107

2. ./ibdata1 can't be opened in read-write mode

\

图4

查看下data及子目录的所属用户及用户组,如果是root用户及用户组,可以做如下操作:

chown -R mysql:mysql /data/mysql/mysql_3307/data

3.InnoDB: Cannot create ./ib_logfile101

如下错误:

2014-11-07 10:24:05 12406 [ERROR] InnoDB: Cannot create ./ib_logfile101

2014-11-07 10:24:05 12406 [ERROR] Plugin 'InnoDB' init function returned error.

2014-11-07 10:24:05 12406 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.

2014-11-07 10:24:05 12406 [ERROR] Unknown/unsupported storage engine: InnoDB

2014-11-07 10:24:05 12406 [ERROR] Aborting

为了解决上一个问题,将data目录下ib_*文件都删除或重命名,建议重命名。
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
How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.