转自http://www.cnblogs.com/olinux/p/5207887.html

xtrabackup的官方下载地址为

http://www.percona.com/software/percona-xtrabackup。

grant all privileges on *.* to 'dbbak'@'localhost' identified by 'bk2016' ;
innobackupex --defaults-file=/etc/my.cnf --user=dbbak --password=bk2016 /data/dbbak/
innobackupex --apply-log /data/dbbak/2017-07-26_19-46-08/ -- 该语句用来校验 将增量1应用到完全备份
--apply-log预备全备份:创建完备份之后的数据不能马上用来还原,需要使用 --apply-log 回滚未提交事务,前滚已提交是会务,让数据库文件保持一致性。内部机制:读取备份文件夹中的配置文件,然后innobackupex重做已提交事务,回滚未提交事务,之后数据就被写到了备份的数据文件(innodb文件)中,并重建日志文件。这一步隐式调用了2次xtrabackup –prepare

innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /data/dbbak/2017-07-26_19-46-08/ -- 最终用来还原文件
innobackupex --defaults-file=/etc/my.cnf --user=dbbak --password=bk2016 --incremental /data/dbbak/ --incremental-basedir=/data/dbbak/2017-07-26_19-46-08/ --parallel=2
innobackupex --defaults-file=/etc/my.cnf --user=dbbak --password=bk2016 --incremental /data/dbbak/ --incremental-basedir=/data/dbbak/2017-07-26_20-14-05/ --parallel=2

innobackupex --apply-log --redo-only /data/dbbak/2017-07-26_19-46-08/

innobackupex --apply-log --redo-only /data/dbbak/2017-07-26_19-46-08/ --incremental-dir=/data/dbbak/2017-07-26_20-14-05/
innobackupex --apply-log /data/dbbak/2017-07-26_19-46-08/ --incremental-dir=/data/dbbak/2017-07-26_20-17-53/
innobackupex --apply-log /data/dbbak/2017-07-26_19-46-08/

innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /data/dbbak/2017-07-26_19-46-08/

-- 完整实现

xtrabackup包含两个主要的工具,即xtrabackup和innobackupex,二者区别如下:

1 xtrabackup只能备份innodb和xtradb两种引擎的表,而不能备份myisam引擎的表;

2 innobackupex是一个封装了xtrabackup的Perl脚本,支持同时备份innodb和myisam,但在对myisam备份时需要加一个全局的读锁。还有就是myisam不支持增量备份。

innobackupex工具的备份过程原理图

如图,备份开始的时候

1 首先会启动一个xtrabackup_log后台检测的进程,实时检测mysql redo的变化,一旦发现redo有新的日志写入,立刻将日志写入到日志文件xtrabackup_log中

2 复制innodb的数据文件和系统表空间文件idbdata1到对应的以默认时间戳为备份目录的地方

3 复制结束后,执行flush table with read lock操作

4 复制.frm .myd .myi文件

5 并且在这一时刻获得binary log 的位置

6 将表进行解锁unlock tables

7 停止xtrabackup_log进程

全库恢复的过程

这一阶段会启动xtrabackup内嵌的innodb实例,将xtrabackup日志xtrabackup_Log进行回放,将提交的事务信息变更应用到innodb数据或表空间,同时回滚未提交的事务

增量备份

增量备份主要是通过拷贝innodb中有变更的页(指的是LSN大于xtrabackup_checkpoints中的LSN号)。增量备份是基于全备的,第一次增量备份的数据是基于上一次全备,之后的每一次增倍都是基于上一次的增倍,最终达到一致性的增倍,增倍的过程中,和全备很类似,区别在于第二步

增量备份的恢复

和全库恢复类似,也需要两步

1 数据文件的恢复 分3部分 全备 增量备份和xtrabackup_log

2 对未提交事务的回滚

=================================================================================

innobackupex的使用案例

rpm -Uhv http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm
yum -y install percona-xtrabackup

1 创建备份用户

mysql> grant reload,lock tables,replication client on *.* to 'dbbak'@'localhost' identified by 'bk2016' ;
mysql> flush privileges;

进行数据库全备

mkdir -pv /data/dbbak
cd /data/dbbak

使用以下参数进行全库备份

[root@MASTER_03 dbbak]#  innobackupex --defaults-file=/etc/my.cnf --user=dbbak --password=bk2016 --socket=/data/3306/tmp/mysql.sock  /data/dbbak/

xtrabackup: Stopping log copying thread.
.160204 00:36:20 >> log scanned up to (1095197210)

160204 00:36:20 Executing UNLOCK TABLES
160204 00:36:20 All tables unlocked
160204 00:36:20 Backup created in directory '/data/dbbak//2016-02-04_00-35-36'
MySQL binlog position: filename 'mysql-bin.000011', position '1338619'
160204 00:36:20 [00] Writing backup-my.cnf
160204 00:36:20 [00] ...done
160204 00:36:20 [00] Writing xtrabackup_info
160204 00:36:20 [00] ...done
xtrabackup: Transaction log of lsn (1095197210) to (1095197210) was copied.
160204 00:36:20 completed OK!说明备份成功


查看对应生成的文件

[root@MASTER_03 dbbak]# ll 2016-02-04_00-35-36/
total 1048648
-rw-r-----. 1 root root 387 Feb 4 00:36 backup-my.cnf
-rw-r-----. 1 root root 1073741824 Feb 4 00:35 ibdata1
drwx------. 2 root root 4096 Feb 4 00:36 iot
drwx------. 2 root root 12288 Feb 4 00:36 iot2
drwx------. 2 root root 4096 Feb 4 00:36 iot3
drwx------. 2 root root 4096 Feb 4 00:36 lsn
drwx------. 2 root root 4096 Feb 4 00:36 mysql
drwx------. 2 root root 4096 Feb 4 00:36 performance_schema
drwx------. 2 root root 4096 Feb 4 00:36 sakila
drwx------. 2 root root 4096 Feb 4 00:36 sbtest
drwx------. 2 root root 4096 Feb 4 00:36 test
drwx------. 2 root root 4096 Feb 4 00:36 xtrabackup0219
-rw-r-----. 1 root root 25 Feb 4 00:36 xtrabackup_binlog_info
-rw-r-----. 1 root root 119 Feb 4 00:36 xtrabackup_checkpoints
-rw-r-----. 1 root root 539 Feb 4 00:36 xtrabackup_info
-rw-r-----. 1 root root 2560 Feb 4 00:36 xtrabackup_logfile

需要注意的几个文件

[root@MASTER_03 dbbak]# cat 2016-02-04_00-35-36/xtrabackup_checkpoints
backup_type = full-backuped ###全备
from_lsn = 0
to_lsn = 1095197210
last_lsn = 1095197210 #####LSN号
compact = 0
recover_binlog_info = 0
[root@MASTER_03 dbbak]# cat 2016-02-04_00-35-36/xtrabackup_binlog_info
mysql-bin.000011 1338619

删掉某个数据库,进行全库恢复

mysql> drop database iot2;
Query OK, 49 rows affected (7.93 sec)

关闭数据库

[root@MASTER_03 dbbak]# /etc/init.d/mysqld stop
Shutting down MySQL.......... SUCCESS!
[root@MASTER_03 dbbak]# mv /data/3306/data /data/3306/data_bak
[root@MASTER_03 dbbak]# mkdir /data/3306/data

恢复

[root@MASTER_03 dbbak]# innobackupex --apply-log /data/dbbak/2016-02-04_00-35-36/
160204 00:56:47 innobackupex: Starting the apply-log operation IMPORTANT: Please check that the apply-log run completes successfully.
At the end of a successful apply-log run innobackupex
prints "completed OK!". innobackupex version 2.3.3 based on MySQL server 5.6.24 Linux (x86_64) (revision id: 525ca7d)
xtrabackup: cd to /data/dbbak/2016-02-04_00-35-36/
xtrabackup: This target seems to be not prepared yet.
xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(1095197210)
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 1
xtrabackup: innodb_log_file_size = 2097152
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 1
xtrabackup: innodb_log_file_size = 2097152
xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
InnoDB: Using atomics to ref count buffer pool pages
InnoDB: The InnoDB memory heap is disabled
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Memory barrier is not used
InnoDB: Compressed tables use zlib 1.2.3
InnoDB: Using CPU crc32 instructions
InnoDB: Initializing buffer pool, size = 100.0M
InnoDB: Completed initialization of buffer pool
InnoDB: Highest supported file format is Barracuda.
InnoDB: The log sequence numbers 532847032 and 532847032 in ibdata files do not match the log sequence number 1095197210 in the ib_logfiles!
InnoDB: Database was not shutdown normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages
InnoDB: from the doublewrite buffer...
InnoDB: 128 rollback segment(s) are active.
InnoDB: Waiting for purge to start
InnoDB: 5.6.24 started; log sequence number 1095197210
xtrabackup: Last MySQL binlog file position 1337268, file name mysql-bin.000011 xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1095198530
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:1G:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 3
xtrabackup: innodb_log_file_size = 1073741824
InnoDB: Using atomics to ref count buffer pool pages
InnoDB: The InnoDB memory heap is disabled
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Memory barrier is not used
InnoDB: Compressed tables use zlib 1.2.3
InnoDB: Using CPU crc32 instructions
InnoDB: Initializing buffer pool, size = 100.0M
InnoDB: Completed initialization of buffer pool
InnoDB: Setting log file ./ib_logfile101 size to 1024 MB
InnoDB: Progress in MB: 100 200 300 400 500 600 700 800 900 1000
InnoDB: Setting log file ./ib_logfile1 size to 1024 MB
InnoDB: Progress in MB: 100 200 300 400 500 600 700 800 900 1000
InnoDB: Setting log file ./ib_logfile2 size to 1024 MB
InnoDB: Progress in MB: 100 200 300 400 500 600 700 800 900 1000
InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
InnoDB: New log files created, LSN=1095198530
InnoDB: Highest supported file format is Barracuda.
InnoDB: 128 rollback segment(s) are active.
InnoDB: Waiting for purge to start
InnoDB: 5.6.24 started; log sequence number 1095198732
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1095202631
160204 00:57:31 completed OK!

以上对应的目录就是innobackupex全备份自己创建的目录

[root@MASTER_03 dbbak]# innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /data/dbbak/2016-02-04_00-35-36/

160204 01:08:39 [01] ...done
160204 01:08:39 [01] Copying ./iot2/t_hash1#P#p3.ibd to /data/3306/data/iot2/t_hash1#P#p3.ibd
160204 01:08:39 [01] ...done
160204 01:08:39 [01] Copying ./iot2/db.opt to /data/3306/data/iot2/db.opt
160204 01:08:39 [01] ...done
160204 01:08:39 [01] Copying ./xtrabackup0219/db.opt to /data/3306/data/xtrabackup0219/db.opt
160204 01:08:39 [01] ...done
160204 01:08:39 completed OK!

更改权限

[root@MASTER_03 tmp]# chown -R mysql.mysql /data/3306/data/

启动mysqld

[root@MASTER_03 tmp]# /etc/init.d/mysqld start

[root@MASTER_03 tmp]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.28-log MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| iot |
| iot2 | ###被删除的库
| iot3 |
| lsn |
| mysql |
| performance_schema |
| sakila |
| sbtest |
| test |
| xtrabackup0219 |
+--------------------+
11 rows in set (0.00 sec)

发现数据是已经成功恢复

先全备

mysql> use xtrabackup0219;
mysql> create table t1(id int(5) primary key auto_increment,name varchar(20));
innobackupex --defaults-file=/etc/my.cnf --user=dbbak --password=bk2016 --socket=/data/3306/tmp/mysql.sock  /data/dbbak/

增量备份

#####往表里插入数据

mysql> insert into t1 select 1,'love sql';
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> insert into t1 select 2,'love sql';
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> insert into t1 select 3,'love sql';
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> select * from t1;
+----+----------+
| id | name |
+----+----------+
| 1 | love sql |
| 2 | love sql |
| 3 | love sql |
+----+----------+
3 rows in set (0.00 sec)

[root@MASTER_03 dbbak]# innobackupex --defaults-file=/etc/my.cnf --user=dbbak --password=bk2016 --socket=/data/3306/tmp/mysql.sock --incremental /data/dbbak/ --incremental-basedir=/data/dbbak/2016-02-04_01-44-24/ --parallel=2

[root@MASTER_03 dbbak]# du -sh *
1.5G 2016-02-04_01-44-24
6.0M 2016-02-04_01-46-48

[root@MASTER_03 dbbak]# cat 2016-02-04_01-46-48/xtrabackup_checkpoints 
backup_type = incremental   ###说明是增量的
from_lsn = 1095215215
to_lsn = 1095217565
last_lsn = 1095217565
compact = 0
recover_binlog_info = 0

此时再插入数据

mysql> insert into t1 select 4,'mysql dba';
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0

增量备份2

[root@MASTER_03 dbbak]# innobackupex --defaults-file=/etc/my.cnf --user=dbbak --password=bk2016 --socket=/data/3306/tmp/mysql.sock --incremental /data/dbbak/ --incremental-basedir=/data/dbbak/2016-02-04_01-46-48/ --parallel=2

[root@MASTER_03 dbbak]# du -sh *
1.5G 2016-02-04_01-44-24
6.0M 2016-02-04_01-46-48
5.9M 2016-02-04_01-49-21

增量备份的恢复

增量备份的恢复需要有3个步骤

1 恢复完全备份

2 恢复增量备份到完全备份(开始恢复的增量备份要添加--redo-only参数,到最后一次增量备份要去掉--redo-only)

3 对整体的完全备份进行恢复,回滚未提交的数据

[root@MASTER_03 dbbak]# innobackupex --apply-log --redo-only /data/dbbak/2016-02-04_01-44-24/

xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1095209828
160204 01:27:09 completed OK!

将增量1应用到完全备份

[root@MASTER_03 dbbak]# innobackupex --apply-log --redo-only /data/dbbak/2016-02-04_01-44-24/ --incremental-dir=/data/dbbak/2016-02-04_01-46-48/

[root@MASTER_03 dbbak]# innobackupex --apply-log /data/dbbak/2016-02-04_01-44-24/ --incremental-dir=/data/dbbak/2016-02-04_01-49-21/

把所有合在一起的完全备份整体进行一次apply操作,回滚未提交的数据

[root@MASTER_03 dbbak]# innobackupex --apply-log /data/dbbak/2016-02-04_01-44-24/

模拟测试

mysql> drop table t1;
Query OK, 0 rows affected (0.05 sec) [root@MASTER_03 dbbak]# rm -rf /data/3306/data/
[root@MASTER_03 dbbak]# mkdir /data/3306/data
[root@MASTER_03 dbbak]# innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /data/dbbak/2016-02-04_01-44-24/
[root@MASTER_03 dbbak]# chown -R mysql.mysql /data/3306/data/
登录查看

mysql> select * from t1;
+----+-----------+
| id | name |
+----+-----------+
| 1 | love sql |
| 2 | love sql |
| 3 | love sql |
| 4 | mysql dba |
+----+-----------+
4 rows in set (0.05 sec)

发现数据是已经正确

0726xtrbackup实例详解的更多相关文章

  1. linux基础-磁盘阵列(RAID)实例详解

    磁盘阵列(RAID)实例详解 raid技术分类 软raid技术 硬raid技术 Raid和lvm的区别 为什么选择用raid RAID详解 RAID-0 RAID-1 RAID-5 Raid-10 R ...

  2. Cocos2d-x 3.X手游开发实例详解

    Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...

  3. JavaScript学习笔记-实例详解-类(二)

    实例详解-类(二)   //===给Object.prototype添加只读\不可枚举\不可配置的属性objectId(function(){ Object.defineProperty(Object ...

  4. JavaScript学习笔记-实例详解-类(一)

    实例详解-类(一): //每个javascript函数(除了bind())都自动拥有一个prototype对象// 在未添加属性或重写prototype对象之前,它只包含唯一一个不可枚举属性const ...

  5. Entity Framework实例详解

    Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...

  6. 免费的HTML5连载来了《HTML5网页开发实例详解》连载(二)

    最近新浪.百度.腾讯.京东.大众点评.淘宝等流行的网站都加大了招聘HTML5的力度,HTML5开发人员成了抢手货,本次连载的是由大众点评前端工程师和一淘网前端工程师基情奉献的<HTML5网页开发 ...

  7. Linux下rz命令使用的实例详解

    Linux中rz命令和sz命令都可用于文件传输,而rz命令主要用于文件的上传,下面将通过几个实例来给大家详细介绍下Linux下rz命令的用法,一起来学习下吧. rz命令可以批量上传文件,当然也可上传单 ...

  8. 实例详解 DB2 排序监控和调优

    实例详解 DB2 排序监控和调优http://automationqa.com/forum.php?mod=viewthread&tid=2882&fromuid=2

  9. 转:【工欲善其事必先利其器】—Entity Framework实例详解

    开始本篇文章之前,先说一下Entity Framework 6 Alpha1在NuGet中已可用,原文链接http://blogs.msdn.com/b/adonet/archive/2012/10/ ...

随机推荐

  1. 803E

    dp dp[i][j]表示到了i赢和输的差为j 如果这位是?向dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1]转移,如果是W向dp[i-1][j-1]转移,如果是L向dp[i- ...

  2. Firefox浏览器中,input输入框输入的内容在刷新网页后为何还在?

    转自:http://www.webym.net/jiaocheng/473.html 这个问题比较容易解决,如果不希望浏览器保留以前输入的内容,只要给对应的 input 输入框加上以下参数: auto ...

  3. XML案例(使用JAXP进行DOM解析)

    1.book.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> ...

  4. HTML 14 JS事件

    一 :什么是事件 发生的某一件事:触发特定的条件,完成某一项功能 二:学习的目的 在特定的条件下,完成特定的功能 条件满足的情况下,系统会自动执行 ( 回调 ) 绑定的方法 学习要点: 1.事件的两种 ...

  5. UOJ 129/BZOJ 4197 寿司晚宴 状压DP

    //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; ; struct Node ...

  6. oracle命令行登录(默认用户名和密码)

    oracle数据库安装成功之后会有默认的用户名和密码,之前因为没有整理,每次用的时候都要百度很麻烦,所以现在把这些都整理一下,也方便以后使用: 使用scott用户连接:使用sys用户连接:使用syst ...

  7. windows phone控件

    常用控件: 包括: Button控件.CheckBox控件.HyperlinkButton控件.Iamege控件.ListBox控件.PasswordBox控件.ProgressBar控件.Radio ...

  8. swift-自定义TabBar工具栏

    class EBTAppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(ap ...

  9. Node.js文件系统Api总结

    //公共引用 var fs = require('fs'), path = require('path'); 1.读取文件readFile函数 //readFile(filename,[options ...

  10. 【Linux】tmpfs简介及增加方式

    一. 什么是tmpfs? tmpfs是一种基于内存的文件系统,它和虚拟磁盘ramdisk比较类似,但不完全相同,和ramdisk一样,tmpfs可以使用RAM,但它也可以使用swap分区来存储.而且传 ...