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. laravel框架学习

    在聊技术之前,我们首先谈谈研究生的生活现状.进入到研究生忙碌的生活中,研究生是这么一个群体,外界对研究生的爱称是"研究僧",为什么我自己会觉得会是爱称.因为研究僧说的是研究生对自己 ...

  2. 广播后刷新界面-调用其他界面的方法触动广播后刷新该界面UI

    new CigaretteLoginActivity().login(ac,"switch_account",list.get(arg2).CUST_CODE,list.get(a ...

  3. C#使用ESC指令控制POS打印机打印小票

    1.前言 C#打印小票可以与普通打印机一样,调用PrintDocument实现.也可以发送标注你的ESC指令实现.由于 调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的po ...

  4. (转载)python2+selenium自动化测试系列(一)

    1.Selenium2+python自动化1-环境搭建 2.Selenium2+python自动化2-pip降级selenium3.0 3.Selenium2+python自动化3-解决pip使用异常 ...

  5. Web UI自动化测试中绕开验证码登陆方式浅谈

    web自动化测试中让测试者感到困惑的是登陆验证码,每次都不一样.现在推荐一种绕开验证码登陆的方式,其实就是将web浏览器获取的登陆cookie加载到程序中就可以了,这样程序就会认为你已经登陆,就可以跳 ...

  6. 关于dp dip dpi px

    在Android开发中,屏幕适配是一件非常让人头疼的事情.有时候在这个机型上调试的漂漂亮亮的UI界面,换一部手机就丑的不忍直视.但为了我们软件更好的用户体验,我们必须适应Android市场上形形色色的 ...

  7. Hibernate一对多(注解)

    <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...

  8. null 和 NULL 判断

    遇到问题,服务器传回 null,我擦嘞,接收不了. 解决如下: NULL 直接 判断就好,能在 xcode 上直接敲出 null 的话  可以  ==[NSNull class] 或者[respons ...

  9. wget下载工具

    转自于:http://www.jb51.net/LINUXjishu/86326.html 1.使用wget下载单个文件  e.g. wget http://cn.wordpress.org/word ...

  10. DDX_Text ()函数 C++

    DDX_Text()函数管理着对话框.表格视或控件视对象中的编辑控件与对话框.表格视或控件视对象的CString型数据成员之间的int,UINT,long,DWORD,CString,float或do ...