转自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. poj3041——最小点覆盖

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  2. 支撑百亿级应用的 NewSQL

    支撑百亿级应用的 NewSQL https://zhuanlan.zhihu.com/newsql/ 项目背景 初次接触 TiDB,是通过同程网首席架构师王晓波先生的分享,当时同程网正在使开发和数据库 ...

  3. codeforces round #424 div2

    A 暴力查询,分三段查就可以了 #include<bits/stdc++.h> using namespace std; ; int n, pos; int a[N]; int main( ...

  4. Win7系统专栏

    1.去掉Win7快捷方式小箭头的方法如下: 使用普通方法会使系统出现异常,比如开始菜单程序无法删除.收藏夹无法展开等,网上流传使用透明图标的方法会在快捷方式上留下一块黑痣,下面的方法是小君研究出来的, ...

  5. web.xml中load-on-startup

    <servlet> <servlet-name>SystemInit</servlet-name> <servlet-class>sjgl.system ...

  6. Spring MVC中传递json数据时显示415错误解决方法

    在ajax中设置 ContentType为'application/json;charset=utf-8' 传递的data类型必须是json字符串类型:{“key”:"value" ...

  7. python实践

    https://www.cnblogs.com/smallSevens/p/10016045.html --二手房交易记录 https://www.cnblogs.com/xiangyuecn/p/1 ...

  8. ACM_Exponentiation

    Exponentiation Time Limit: 2000/1000ms (Java/Others) Problem Description: Problems involving the com ...

  9. SQLServer2008 关于数据转换

    全进位 select cast(ceiling(2.1111) as dec(18,0)) 结果:3

  10. YOLO (You Only Look Once)

    YOLO (You Only Look Once) dl  cnn  object detection  一.YOLO YOLO是一个实时的目标检测系统.最新的V2版本在Titan X 上可以每秒处理 ...