Xtrabackup

Xtrabackup包含两个主要的工具,即xtrabackup和innobackupex,二者区别如下: 
• xtrabackup只能备份innodb和xtradb引擎表,而不能备份MyISAM表 
• innobackupex是一个封装了xtrabackup的Perl脚本,支持同时备份innodb和MyISAM,但在对MyISAM备份时需要加一个全局的读锁

创建用户赋予权限

  1. CREATE USER 'backup'@'localhost' IDENTIFIED BY 'backup';
  2. GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT,PROCESS ON *.* TO 'backup'@'localhost';

innobackupex调用xtrabackup备份xtradb和innodb,调用perl脚本备份MyISAM表

创建完整备份示例

  1. innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup'/data/mysqldata/backup/

如果使用–defaults-file选项,则必须在第一位

备份完成后会在目录下创建一个以时间命名的目录

  1. [mysql@master backup]$ pwd
  2. /data/mysqldata/backup
  3. [mysql@master backup]$ ls
  4. 2016-08-18_18-23-33

–no-timestamp 该选项告诉innobackupex不创建一个时间戳目录存储备份

  1. innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup'--no-timestamp /data/mysqldata/backup/base

上例中备份会存放在base目录下,如果目录不存在会自动创建

创建备份后并不能直接用于恢复.需要先prepare.

prepare的目的是跑一下redo,将未提交的rollback,回滚的回滚,跑出一个一致性的备份

  1. innobackupex --apply-log /data/mysqldata/backup/base/

查看输出的最后一行 completed OK!表示prepare成功

prepare前

  1. backup-my.cnf ibdata1 performance_schema test xtrabackup_checkpoints xtrabackup_logfile
  2. fandb mysql sakila xtrabackup_binlog_info xtrabackup_info

prepare后

  1. backup-my.cnf ib_logfile0 ibtmp1 sakila xtrabackup_binlog_pos_innodb xtrabackup_logfile
  2. fandb ib_logfile1 mysql test xtrabackup_checkpoints
  3. ibdata1 ib_logfile2 performance_schema xtrabackup_binlog_info xtrabackup_info

ib_logfile0,1,2由prepare生成

–use-memory 使用此参数可以提升prepare速度,默认100M

  1. innobackupex --apply-log --use-memory=4G/data/mysqldata/backup/base/

restore全备

为了方便,innobackupex有一个–copy-back选项,可以将备份拷贝回datadir下

  1. innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --copy-back /data/mysqldata/backup/base

datadir必须是空的,除非使用了–force-non-empty-directories 参数.MySQL在copy-back时应该是关闭的

如果创建备份时使用的是其他OS用户,那么在copy-back后你应该修改权限

  1. chown -R mysql:mysql

增量备份(增量只针对xtradb和innodb引擎,其他引擎表仍是全备)

增量备份之前必须有一个全备份.

  1. innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup'--no-timestamp /data/mysqldata/backup/base
  2. 备份成功后,会生成一个文件xtrabackup_checkpoints
  3. [mysql@master backup]$ more base/xtrabackup_checkpoints
  4. backup_type = full-backuped
  5. from_lsn =0
  6. to_lsn =143682470
  7. last_lsn =143682470
  8. compact =0
  9. recover_binlog_info =0

使用上面的全备做为基础,做增量备份

  1. innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup'--no-timestamp --incremental /data/mysqldata/backup/incr1 --incremental-basedir=/data/mysqldata/backup/base
  2. 备份成功后,查看增备的xtrabackup_checkpoints
  3. [mysql@master backup]$ more incr1/xtrabackup_checkpoints
  4. backup_type = incremental
  5. from_lsn =143682470
  6. to_lsn =143683016
  7. last_lsn =143683016
  8. compact =0
  9. recover_binlog_info =0

再次创建一个增量备份,将以刚才的增备作为基础

  1. innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup'--no-timestamp --incremental /data/mysqldata/backup/incr2 --incremental-basedir=/data/mysqldata/backup/incr1
  2. [mysql@master backup]$ more incr2/xtrabackup_checkpoints
  3. backup_type = incremental
  4. from_lsn =143683016
  5. to_lsn =143683562
  6. last_lsn =143683562
  7. compact =0
  8. recover_binlog_info =0
  9. 增倍时应该是要读取base的xtrabackup_checkpoints文件,从该文件的last_lsn开始备份.
  10. 于是我手动创建一个目录incr3,并创建一个文件xtrabackup_checkpoints,以此作为base也是可以备份的.
  11. 将该xtrabackup_checkpoints文件的last_lsn改为0也可以,但仍不是全备
  12. 手动指定lsn,也可以成功创建增量备份.这种方法适用于当base不可用时
  13. innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --user=backup --password='backup'--incremental /data/mysqldata/backup/incr1 --incremental-lsn=143680036

–incremental-lsn=name 
This option specifies the log sequence number (LSN) to 
use for the incremental backup. The option accepts a 
string argument. It is used with the –incremental 
option. It is used instead of specifying 
–incremental-basedir. For databases created by MySQL and 
Percona Server 5.0-series versions, specify the LSN as 
two 32-bit integers in high:low format. For databases 
created in 5.1 and later, specify the LSN as a single 
64-bit integer.

prepare增备

prepare增量备份与prepare全备稍有不同

• First, only the committed transactions must be replayed on each backup. This will merge the base full backup with the incremental ones. 
• Then, the uncommitted transaction must be rolled back in order to have a ready-to-use backup. 
1.首先,在所有备份重演提交的事务,这会合并增量备份和全备 
2.所有未提交事务回滚

恢复基础备份(全备)这里一定要加–redo-only参数,该参数意思是只应用xtrabackup日志中已经提交的事务数据,不回滚还未提交的数据

  1. innobackupex --apply-log --redo-only /data/mysqldata/backup/base/

将增量备份incr应用到基础备份base

  1. innobackupex --apply-log --redo-only /data/mysqldata/backup/base/--incremental-dir=/data/mysqldata/backup/incr1

将增量备份incr2应用到基础备份base,注意恢复最后一个增量备份时需要去掉–redo-only参数,回滚xtrabackup日志中那些还未提交的数据

  1. innobackupex --apply-log /data/mysqldata/backup/base/--incremental-dir=/data/mysqldata/backup/incr2

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. 
–redo-only只在最后一个增备时不使用.如果在最后一个增倍时使用了–redo-only,备份仍是一致的,但在这种情况下,服务器将执行回滚阶段

要注意的是,prepare应该按照备份的时间顺序,即 全备-增备1-增备2-增备3… 
如果顺序错误,备份将不可用.如果你不知道正确的熟顺序,可以查看xtrabackup_checkpoints文件

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

  1. innobackupex --apply-log /data/mysqldata/backup/base/

这一步是可选的,如果你不做,MySQL会去回滚未提交的数据.同时,因为iblog文件不会被备份,这一步也会创建它们,如果不做这一步,MySQL启动时也会创建它们.

copy-back

  1. innobackupex --defaults-file=/data/mysqldata/3306/my.cnf --copy-back --rsync /data/mysqldata/backup/base

–rsync Uses the rsync utility to optimize local file transfers. 
When this option is specified, innobackupex uses rsync to 
copy all non-InnoDB files instead of spawning a separate 
cp for each file, which can be much faster for servers 
with a large number of databases or tables. This option 
cannot be used together with –stream.

PerconaXtraBackup --全备增备prepare restore的更多相关文章

  1. Mongodb全备+增备+oplog恢复误删数据

    此时测试表中有7条数据,做个全备. 全备: mongodump --host=192.168.43.43 --port=37017 --oplog --out=/opt/mongo/fullbacku ...

  2. innobackupex 单脚本循环7天一全备6增备脚本更新

    #!/bin/bash #日期转为天数 function date2days { echo "$*" | awk '{ z=-$)/); y=$+-z; m=$+*z-; j=*m ...

  3. Innobackupex MySQL 全备、增备及恢复

    简介: 在这之前都是通过 mysqldump 来备份数据库的,由于是逻辑备份,所以采用这种备份方式数据是很安全的,跨平台.版本都很容易. 凡事有利必有弊,逻辑备份在你数据库比较大时,备份.恢复数据所耗 ...

  4. innobackupex 全备、增备脚本

    全备脚本:innobackupex --defaults-file=/etc/my.cnf --user root --password mypasswd /mydata/fullbak/ 增备脚本: ...

  5. oracle 备份恢复篇(二)---rman 增备恢复--不完全恢复

    一,环境准备 全备脚本: export TMP=/tmp export TMPDIR=$TMP export ORACLE_BASE=/u01 export ORACLE_SID=prod expor ...

  6. 为MySQL选择合适的备份方式

    数据库的备份是极其重要的事情.如果没有备份,遇到下列情况就会抓狂: UPDATE or DELETE whitout where… table was DROPPed accidentally… IN ...

  7. mysql 备份与还原

    http://dev.yesky.com/281/35291281.shtml 每一种逻辑错误发生的概率都极低,但是当多种可能性叠加的时候,小概率事件就 放大成很大的安全隐患,这时候备份的必要性就凸显 ...

  8. 为MySQL选择合适的备份方式[转]

    原文链接:http://nettedfish.sinaapp.com/blog/2013/05/31/choose-suitable-backup-strategy-for-mysql/ 数据库的备份 ...

  9. 青铜到王者,快速提升你 MySQL 数据库的段位!

    Reference: https://mp.weixin.qq.com/s?src=11&timestamp=1513259125&ver=574&signature=Uxls ...

随机推荐

  1. git工作中的常用操作

    上班开始,打开电脑,git pull:拉取git上最新的代码: 编辑代码,准备提交时,git stash:将自己编辑的代码暂存起来,防止git pull时与库中的代码起冲突,否则自己的代码就白敲了: ...

  2. python Day01

    Python Day01 Python 简介 介绍 Python 是一种面向对象.直译式的计算机程序设计语言,也是一种功能强大的通用型语言,已经有将近二十年的发展历史,成熟稳定.包含了一组完善而且容易 ...

  3. cookie小记

    清除cookie可用下面2种的方法.注意如果要清除的cookie的域是指定的,则这里也应该指定,否则无效                1:                Response.Cooki ...

  4. Js文件中文乱码

    aspx页面引用的js文件中如果包括中文,中文显示乱码或者引起脚本错误.提示是'未结束的字符串' 原因:aspx页面的默认编码是utf-8,而js文件的默认编码是gb2312,两者之间不一致引起了中文 ...

  5. tcp/ip http socket笔记

    1.TCP/IP协议是传输层协议,主要解决数据如何在网络中传输 HTTP是应用层协议,主要解决如何包装数据 2.TCP连接的三次握手 第一次握手:客户端发送syn包到服务器,并进入SYN_SEND状态 ...

  6. SQL中CONVERT日期不同格式的转换用法

    SQL中CONVERT日期不同格式的转换用法 格式: CONVERT(data_type,expression[,style]) 说明:此样式一般在时间类型(datetime,smalldatetim ...

  7. 放假回来啦!!小技能:一个div不给width,怎么让它居中捏?`(*∩_∩*)′

    答案是:这个div没有浮动的话,就用text-align: center; 有的话...我也不知道了

  8. Java基础知识系列——数组

    数组是我们在编程中常用到的一种数据结构. 数组创建有三种方式,以int类型为例: 1.int value[] = new int[]{1,2,3,4,5}; //{}中的是元素 2.int value ...

  9. css总结

    1. css中的role属性 html 里面的 role 本质上是增强语义性,当现有的HTML标签不能充分表达语义性的时候,就可以借助role来说明.通常这种情况出现在一些自定义的组件上,这样可增强组 ...

  10. [转载]Docker的安装配置及使用详解

    简介    官网:http://www.docker.com/,点击get started进入下载,目前三个系统的docker容器都有,Windows版需要win10系统,我的是win7系统一开始用的 ...