Table of Contents
背景说明
安装
相关准备
创建备份目录
备份实操作
1. 全量备份
2.制造新数据
3. 第一次增量备份
恢复操作
1. 对全量备份进行操作
2. 将第一次增量备份的数据合并到全量备份中
3.将第二次全量备份的数据合并到全量备份中
4.停止mysql服务
3.恢复数据
6.权限设置
7.启动mysql3307实例
8. 查看是否恢复成功
1 .模拟数据丢失
2. 停止3306实例
4. mysql的data目录进行权限设置
5.启动3306实例
碰到的问题及解决方法
1. innobackupex: Error: Original data directory 'XXX' is not empty!
2. ./ibdata1 can't be opened in read-write mode
Home Database Mysql Tutorial 多实例下percona-xtrbackup使用(2014-11-10)

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

Jun 07, 2016 pm 04:10 PM
use Example

背景说明 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
Copy after login
其他安装可以查看官网: 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
Copy after login
在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
Copy after login
3306.cnf原样保存即可,3307.cnf需要进行修改,在[mysqld]节点下添加"datadir=/data/mysql/mysql_3307/data/"。以方便数据恢复时使用。 常用参数说明:
--user: mysql用户
--password: 用户密码
--defaults-file:  指定my.cnf文件路径,若不指定则读取mysql默认的my.cnf文件
--socket:mysql实例对应的socket文件
Copy after login

备份实操作

1. 全量备份

首次备份为全量备份,也是增量备份的基础。
innobackupex --user=root --password=123456 --socket=/tmp/mysql_3306.sock  --defaults-file=/data/backup/conf/3306.cnf  /data/backup/full/ 
Copy after login
首次将数据库的所有数据备份到/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
Copy after login
xtrabackup_checkpoints: 记录备份类型及开始及结束的lsn位置。backup_type 有两种full-prepared (全备)、incremental (增备)。
backup_type = full-prepared 
from_lsn = 0 
to_lsn = 8234580547 
last_lsn = 8234580547 
compact = 0
Copy after login
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
Copy after login
xtrabackup_logfile: xtrabackup自己的日志文件,新版本中不直接可见。

2.制造新数据

创建表,并写入数据,作为新增的数据。
use test;
create table t3(col1 int,col2 int);
Copy after login
写入如下新数据:
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);
Copy after login

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

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
Copy after login
参数说明:
--incremental :指定存储本次增量备份的目录
--incremental-basedir:上次备份的存储目录
Copy after login
完成首次的增量备份后,在指定目录下也会生成一个新的目录,目录中的文件与全量备份文件部分不一样。

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
Copy after login

恢复操作

1. 对全量备份进行操作

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

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
Copy after login

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

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

4.停止mysql服务

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

5. 恢复数据。

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

6.权限设置

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

7.启动mysql3307实例

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

8. 查看是否恢复成功

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

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

1 .模拟数据丢失

drop database test;
Copy after login

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>
Copy after login

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

chown -R mysql:mysql   /data/mysql/mysql_3307/data 
Copy after login

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 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 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months 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)

How to use magnet links How to use magnet links Feb 18, 2024 am 10:02 AM

Magnet link is a link method for downloading resources, which is more convenient and efficient than traditional download methods. Magnet links allow you to download resources in a peer-to-peer manner without relying on an intermediary server. This article will introduce how to use magnet links and what to pay attention to. 1. What is a magnet link? A magnet link is a download method based on the P2P (Peer-to-Peer) protocol. Through magnet links, users can directly connect to the publisher of the resource to complete resource sharing and downloading. Compared with traditional downloading methods, magnetic

How to use mdf and mds files How to use mdf and mds files Feb 19, 2024 pm 05:36 PM

How to use mdf files and mds files With the continuous advancement of computer technology, we can store and share data in a variety of ways. In the field of digital media, we often encounter some special file formats. In this article, we will discuss a common file format - mdf and mds files, and introduce how to use them. First, we need to understand the meaning of mdf files and mds files. mdf is the extension of the CD/DVD image file, and the mds file is the metadata file of the mdf file.

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

How to use Xiaoai Speaker How to connect Xiaoai Speaker to mobile phone How to use Xiaoai Speaker How to connect Xiaoai Speaker to mobile phone Feb 22, 2024 pm 05:19 PM

After long pressing the play button of the speaker, connect to wifi in the software and you can use it. Tutorial Applicable Model: Xiaomi 12 System: EMUI11.0 Version: Xiaoai Classmate 2.4.21 Analysis 1 First find the play button of the speaker, and press and hold to enter the network distribution mode. 2 Log in to your Xiaomi account in the Xiaoai Speaker software on your phone and click to add a new Xiaoai Speaker. 3. After entering the name and password of the wifi, you can call Xiao Ai to use it. Supplement: What functions does Xiaoai Speaker have? 1 Xiaoai Speaker has system functions, social functions, entertainment functions, knowledge functions, life functions, smart home, and training plans. Summary/Notes: The Xiao Ai App must be installed on your mobile phone in advance for easy connection and use.

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

See all articles