PerconaXtraBackup --全备增备prepare restore

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. 增倍时应该是要读取basextrabackup_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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值