目录:


1.前言:

Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品。
XtraBackup优势 :
  • 1、无需停止数据库进行InnoDB热备
  • 2、增量备份MySQL
  • 3、流压缩到传输到其它服务器
  • 4、能比较容易地创建主从同步
  • 5、备份MySQL时不会增大服务器负载
 
Xtrabackup有两个主要的工具:xtrabackup、innobackupex
(1)xtrabackup只能备份InnoDB和XtraDB两种数据表,而不能备份MyISAM数据表
(2)innobackupex-1.5.1则封装了xtrabackup,是一个脚本封装,所以能同时备份处理innodb和myisam,但在处理myisam时需要加一个读锁

2.环境:
[root@bogon . (Final)
Kernel \r on an \m
[root@bogon . Distrib .., (x86_64) using  EditLine wrapper
[root@bogon .. .. unknown)
xtrabackup的安装包,可以从http://www.percona.com/下载
 
3.开始备份
3.1.备份工具采用innobackupex
格式:innobackupex 选项 备份文件的路径(不需要写备份的文件名)

其常用的通用选项如下:
--user=    具备完全权限的账号和密码
    --password=    
--socket=    指定mysql的socket文件,默认/var/lib/mysql/mysql.sock,一般来说都需要指定这个,因为基本遇到的数据库pid都不在这个位置.
--port=    指定mysql端口,默认3306
--defaults-file=    采用的mysql配置文件
 
如果不明确定义socket,可能在使用innobackupex进行备份的时候会报:
innobackupex: Error: Failed to connect to MySQL server: DBI connect(';mysql_read_default_group=xtrabackup','root',...) failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) at /usr/bin/innobackupex line 2945
即innobackupex默认寻找的socket是/var/lib/mysql/mysql.sock
 
3.2.一次完全备份:
额外的备份可选选项:
--database=    即将备份的数据库(默认是全部数据库)
--stream=    备份文件的存储格式,例如tar格式,# innobackupex --stream=tar  /backup | gzip > /backup/`date +%F_%H-%M-%S`.tar.gz 不过如此以来,恢复的时候就需要先解压.
    --tmpdir=    指定临时输出目录,因为--stream会使输出文件暂存到临时目录/tmp中,如果mysql并发很大,则/tmp的压力就可能很大,故而采用此选项重新指定一个临时输出目录
[root@localhost mysql]# innobackupex --user=root --password=123456 --socket=/tmp/mysql.sock /data/back_data/
[root@localhost mysql]# ls /data/back_data/2014-04-09_19-35-40/
backup-my.cnf  ib_logfile0  mysql               test               xtrabackup_checkpoints
ibdata1        ib_logfile1  performance_schema  xtrabackup_binary  xtrabackup_logfile
3.3.一次完全恢复:
完全恢复必须的恢复选项:
--apply-log    暂时通过备份文件启动mysql服务,将未提交的事务回滚,并将提交但未同步的数据进行同步,保证备份文件在备份时刻的数据一致性.
官方解析>>
 After creating a backup, the data is not ready to be restored. There might be uncommitted transactions to be undone or transactions in the logs to be replayed. Doing those pending operations will make the data files consistent and it is the purpose of the prepare stage . Once this has been done, the data is ready to be used.
--copy-back    将备份文件copy到my.cnf中指定的数据库位置
root root Apr   ibdata1
root root Apr   ib_logfile0
root root Apr   ib_logfile1
drwxr root root     Apr   mysql
drwxr root root     Apr   performance_schema
drwxr root root     Apr   test
修改数据库权限
[root@localhost mydata]# chown -R mysql.mysql /mydata
启动成功
[root@localhost mydata]# service mysqld start
Starting MySQL...... SUCCESS!
[root@localhost mysql]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.13 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2013, 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> 
 
3.4.增量备份
既然是增量备份,必然需要指定上一次备份的路径.且,第一次增量备份时,指定的上一次备份路径应该是完全备份.
那么增量备份需要额外添加的选项如下:
--incremental    即开启增量备份,后面跟增量备份的路径
--incremental-basedir    即上次备份的存储路径,不管是上次的完全备份,还是上次的增量备份
 
当前mysql状态
mysql rows . sec)
此时备份目录里只有一次完全备份
[root@localhost

添加一个数据库in1,便于测试第一次增量备份
mysql row affected (. sec)

mysql rows .00 sec)

开始第一次增量备份
[root@localhost ~]# innobackupex --user=root --password=123456 --socket=/tmp/mysql.sock --incremental-basedir=/data/back_data/2014-04-09_19-35-40/ --incremental /data/back_data/
此时备份目录状态
[root@localhost  

添加一个数据库in2,便于测试第二次增量备份
mysql row affected (. sec)

mysql rows . sec)

开始第二次增量备份
[root@localhost ~]# innobackupex --user=root --password=123456 --socket=/tmp/mysql.sock --incremental-basedir=/data/back_data/2014-04-10_11-25-56/ --incremental /data/back_data/
此时备份目录状态
[root@localhost    
三个目录从左至右依次为完全备份-->增量备份1-->增量备份2
 
3.5.增量备份的恢复过程
增量备份如果需要恢复的话,需要先将某个完全备份之后的所有增量备份的数据整合到此完全备份中.因此,需要新的额外整合选项
--redo-only    将增量备份的数据重放到完全备份中,但是不要作用于最后一次增量恢复,这个为什么不在最后一个上面执行,具体原因不了解.
 
官方解析>>
Note:--redo-only should be used when merging all incrementals except the last one. That’s why the previous
line doesn’t contain the --redo-only option. Even if the --redo-only was used on the last step, backup would
still be consistent but in that case server would perform the rollback phase.
    --incremental-dir=    指定本次整合中增量备份的存储路径,配合--redo-only选项使用
首先模拟故障,删除所有数据库,(我这里的数据库路径是/mydata)
[root@localhost ~]# service mysqld stop
Shutting down MySQL................ SUCCESS!
[root@localhost mydata]# rm -rf *
[root@localhost mydata]# ls
其次执行增量整合过程,将两个增量备份整合到完全备份中
三次命令依次是:
重放完全备份
整合增量备份1到完全备份
增量备份2到完全备份(不执行--redo-only)
[root@localhost ~]# innobackupex --user=root --password=123456 --apply-log --redo-only /data/back_data/2014-04-09_19-35-40/
[root@localhost ~]# innobackupex --user=root --password=123456 --apply-log --redo-only --incremental-dir=/data/back_data/2014-04-10_11-25-56/ /data/back_data/2014-04-09_19-35-40/
[root@localhost ~]# innobackupex --user=root --password=123456 --apply-log  --incremental-dir=/data/back_data/2014-04-10_11-48-44/ /data/back_data/2014-04-09_19-35-40/
最后,执行还原,即还原整合后的备份.
[root@localhost ~]# innobackupex --user=root --password=123456 --apply-log /data/back_data/2014-04-09_19-35-40/
[root@localhost ~]# innobackupex --user=root --password=123456 --copy-back /data/back_data/2014-04-09_19-35-40/
查看mydata目录
[root@localhost mydata]# ls
ibdata1  ib_logfile0  ib_logfile1  in1  in2  mysql  performance_schema  test
可以看到in1和in2两个数据库都还原成功.
修改数据库权限
[root@localhost mydata]# chown -R mysql.mysql .
启动mysql数据库,并登陆
[root@localhost
Server version.. MySQL Community Server (GPL)

Copyright (c) , , Oracle rows . sec)

 
 
 
 
 

mysql之7xtrabackup的更多相关文章

  1. Hadoop 中利用 mapreduce 读写 mysql 数据

    Hadoop 中利用 mapreduce 读写 mysql 数据   有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...

  2. mysql每秒最多能插入多少条数据 ? 死磕性能压测

    前段时间搞优化,最后瓶颈发现都在数据库单点上. 问DBA,给我的写入答案是在1W(机械硬盘)左右. 联想起前几天infoQ上一篇文章说他们最好的硬件写入速度在2W后也无法提高(SSD硬盘) 但这东西感 ...

  3. LINUX篇,设置MYSQL远程访问实用版

    每次设置root和远程访问都容易出现问题, 总结了个通用方法, 关键在于实用 step1: # mysql -u root mysql mysql> Grant all privileges o ...

  4. nodejs进阶(6)—连接MySQL数据库

    1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...

  5. MySQL高级知识- MySQL的架构介绍

    [TOC] 1.MySQL 简介 概述 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,将数据保存在不同的表中,而 ...

  6. 闰秒导致MySQL服务器的CPU sys过高

    今天,有个哥们碰到一个问题,他有一个从库,只要是启动MySQL,CPU使用率就非常高,其中sys占比也比较高,具体可见下图. 注意:他的生产环境是物理机,单个CPU,4个Core. 于是,他抓取了CP ...

  7. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

  8. Entity Framework Core 实现MySQL 的TimeStamp/RowVersion 并发控制

    将通用的序列号生成器库 从SQL Server迁移到Mysql 遇到的一个问题,就是TimeStamp/RowVersion并发控制类型在非Microsoft SQL Server数据库中的实现.SQ ...

  9. Docker笔记一:基于Docker容器构建并运行 nginx + php + mysql ( mariadb ) 服务环境

    首先为什么要自己编写Dockerfile来构建 nginx.php.mariadb这三个镜像呢?一是希望更深入了解Dockerfile的使用,也就能初步了解docker镜像是如何被构建的:二是希望将来 ...

随机推荐

  1. windows server 2008 设置多用户同时远程登录

    >Windows server 2008默认只支持一个administrator用户登陆,一个登录后另一个就被踢掉了,有没有办法像Windows Server 2003那样允许多用户用同时同一个 ...

  2. #include<iostream>与#include<iostream.h>的区别

                                           转载于祝长洋的BLOG:http://blog.sina.com.cn/s/blog_514b5f600100ayks.h ...

  3. 自定义JsonResult处理JSON序列化DateTime类型数据(Ext4.2+ASP.NET MVC 4)

    最近项目中前台页面使用Extjs4.2 ,在后台ASP.NET MVC4返回的DateTime类型的数据错返回的DateTime类型的JsonResult的结果中的值是“\/Date(13784461 ...

  4. Spring AOP 本质(4)

    这一主要看看Spring AOP是如何实现通知包围的.   Spring AOP包围通知在功能上和前置通知加后置通知类似,但还是有区别的:包围通知可以修改返回值,还可以阻止.替换目标方法的执行.   ...

  5. 深入浅出CChart 每日一课——第十八课 女神的套娃,玩转对话框

    前面笨笨已经给大家展示了CChart编程的N个例子.这些例子中,我们的CChart图像都是绘制在程序的主窗口中的. 在很多情况下,我们面对的情形不是这样的.这节课笨笨就给大家介绍一下怎样在对话框中用C ...

  6. UML--核心元素之分析类

    分析类包括边界类.控制类和实体类. 边界类是一种用于对系统外部环境与其内部运作之间的交互进行建模的类. myself:就像建模时,不是所有的属性都要建模一样.不是所有的方法都要建模一样.学习也是如此, ...

  7. 使用 Strace 和 GDB 调试工具的乐趣

    编写 UNIX® 系统程序充满乐趣,并且具有教育意义.使用 UNIX strace 工具和 GDB(GNU 项目调试工具),您可以真正地深入研究系统的功能,并了解组成这些功能的各种各样的程序.同时使用 ...

  8. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  9. CF 545E Paths and Trees

    题目大意:给出n个点,m条无向边,每条边有长度.求一棵树,要求树上的每个点到源点距离最小的前提下,使得树上的边的长度和最小.输出树上边的总长度,以及树上的边的序号(按输入顺序 1...m). 思路 : ...

  10. python学习之路-4 内置函数和装饰器

    本篇涉及内容 内置函数 装饰器 内置函数 callable()   判断对象是否可以被调用,返回一个布尔值 1 2 3 4 5 6 7 8 9 10 11 num = 10 print(callabl ...