mysql之 Innobackupex(全备+增量)备份恢复
MySQL的热备(物理备份)可以采取全备加增量备份的方式来减轻数据库I/O压力及系统资源的占用。增量备份主要是以全备或增量备份为基础,备份那些变更过的页面。其备份的原理是基于一个不断增长的LSN序列,这个LSN与Oracle的SCN类似。在恢复期间,我们需要将已提交的事务前滚,未提交的事务回滚。本文主要描述了增量备份及增量恢复。
1、增备的相关知识点
As not all information changes between each backup, the incremental backup strategy uses this to reduce the storage needs and the duration of making a backup. This can be done because each InnoDB page has a log sequence number, LSN, which acts as a version number of the entire database. Every time the database is modified, this number gets incremented. An incremental backup copies all pages since a specific LSN. Once the pages have been put together in their respective order, applying the logs will recreate the process that affected the database, yielding the data at the moment of the most recently created backup.
增备是备份上次以来发生变化的页面,通过增备可以减轻存储以及系统资源开销。增量备份主要针对于InnoDB,因为InnoDB采用了日志序列号(LSN)的方式。InnoDB的LSN是一个增长的序列,类似于Oracle的SCN,记录了InnoDB的变化情况。增量备份则是备份特定的LSN之后变化的情况。通过按序重组这些LSN即可将数据库恢复到故障点或任意时刻。
innobackupex --incremental /data/backups --incremental-lsn=1291135
innobackupex --incremental /data/backups --incremental-lsn=1358967
如上,我们可以使用--incremental-lsn选项来实施增量备份
Warning: This procedure only affects XtraDB or InnoDB-based tables. Other tables with a different storage engine, e.g. MyISAM, will be copied entirely each time an incremental backup is performed.
对于非XtraDB或者InnoDB存储引擎,热备方式依旧会全部备份所有的数据文件,索引文件,格式文件等。
Preparing an Incremental Backup with innobackupex Preparing incremental backups is a bit different than full ones. This is, perhaps, the stage where more attention is needed:
• 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.
对于增量备份的Prepare阶段,有2个需要注意的地方,一个是提交的事务需要replayed,一个未提交的事务需要rollback。
If you replay the committed transactions and rollback the uncommitted ones on the base backup, you will not be able to add the incremental ones. If you do this on an incremental one, you won’t be able to add data from that moment and the remaining increments. Having this in mind, the procedure is very straight-forward using the --redo-only option, starting with the base backup:
如果在Prepare阶段replay了已提交的事务以及回滚了未提交的事务,则后续的增量备份无法添加到当前全备。因此在Prepare阶段全备应使用--redo-only选项。
--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 选项。如果使用了的话,rollback将由服务器启动的时候来完成。
二、 演示
1. 准备实验环境
mysql> select version();
+------------+
| version() |
+------------+
| 5.6.25-log |
+------------+
1 row in set (0.00 sec)
mysql> create database inc_rec;
Query OK, 1 row affected (0.00 sec)
mysql> use inc_rec;
Database changed
mysql> create table andy (id int);
Query OK, 0 rows affected (0.08 sec)
mysql> insert into andy values(1),(2);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
2. 全备
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=oracle --port=3606 /xtrabackup/full/
xtrabackup: Transaction log of lsn (1648193) to (1648193) was copied.
170609 03:53:56 completed OK!
3. 查看全备生成文件
[root@mysql02 full]# ll /xtrabackup/full/
total 4
drwxr-x---. 6 root root 4096 Jun 9 03:53 2017-06-09_03-53-51
4. 模拟业务新数据
mysql> insert into andy values(3),(4);
Query OK, 2 rows affected (0.14 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
5. 增量备份
-- 创建存放增量备份目录并赋权
[root@mysql02 full]# mkdir -p /xtrabackup/increament/
[root@mysql02 full]# chown -R mysql:mysql /xtrabackup/increament/
[root@mysql02 full]# ll /xtrabackup/
total 8
drwxr-xr-x. 3 mysql mysql 4096 Jun 9 03:53 full
drwxr-xr-x. 2 mysql mysql 4096 Jun 9 04:00 increament
-- 正式开始增量备份
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=oracle --incremental \
--incremental-basedir=/xtrabackup/full/2017-06-09_03-53-51/ /xtrabackup/increament/
########################################下面是增量备份输出
170609 04:02:21 Backup created in directory '/xtrabackup/increament/2017-06-09_04-02-16/'
MySQL binlog position: filename 'binlog.000003', position '894'
。。。省略
xtrabackup: Transaction log of lsn (1652210) to (1652210) was copied.
170609 04:02:22 completed OK!
补充:
[root@mysql02 2017-06-09_04-02-16]# pwd
/xtrabackup/increament/2017-06-09_04-02-16
-- 查看增备产生的相关文件
[root@mysql02 2017-06-09_04-02-16]# ll
total 8580
-rw-r-----. 1 root root 418 Jun 9 04:02 backup-my.cnf
-rw-r-----. 1 root root 360448 Jun 9 04:02 ibdata1.delta
-rw-r-----. 1 root root 44 Jun 9 04:02 ibdata1.meta
drwxr-x---. 2 root root 4096 Jun 9 04:02 inc_rec
drwxr-x---. 2 root root 4096 Jun 9 04:02 mysql
drwxr-x---. 2 root root 4096 Jun 9 04:02 performance_schema
drwxr-x---. 2 root root 4096 Jun 9 04:02 test
-rw-r-----. 1 root root 18 Jun 9 04:02 xtrabackup_binlog_info
-rw-r-----. 1 root root 117 Jun 9 04:02 xtrabackup_checkpoints
-rw-r-----. 1 root root 584 Jun 9 04:02 xtrabackup_info
-rw-r-----. 1 root root 8388608 Jun 9 04:24 xtrabackup_logfile
-- 文件 xtrabackup_info 含有备份类型
[root@mysql02 2017-06-09_04-02-16]# more xtrabackup_info|grep ^incremental
incremental = Y
-- 文件xtrabackup_checkpoints包含了备份的相关检查点信息
[root@mysql02 2017-06-09_04-02-16]# more xtrabackup_checkpoints
backup_type = incremental
from_lsn = 1648193
to_lsn = 1652210
last_lsn = 1652210
compact = 0
recover_binlog_info = 0
-- 文件xtrabackup_binlog_info包含了binlog的位置
[root@mysql02 2017-06-09_04-02-16]# more xtrabackup_binlog_info
binlog.000003 894
6. 误操作,truncate表
mysql> truncate table andy;
Query OK, 0 rows affected (0.22 sec)
mysql> select * from andy;
Empty set (0.01 sec)
7. 停止mysql数据库
[root@mysql02 ~]# service mysql stop
[root@mysql02 ~]# ps -ef|grep mysql
8.恢复数据库 (先恢复全备,再按增量备份时间先后顺序,依次恢复增量备份)
8.1 先恢复完整的备份集:
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --apply-log --redo-only /xtrabackup/full/2017-06-09_03-53-51/
170609 04:19:30 completed OK!
8.2 在恢复增量备份集: (如果有多份增量备份,最好最后一份增量不用 --redo-only , 其他的都用 --redo-only)
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --apply-log /xtrabackup/full/2017-06-09_03-53-51 --incremental-dir=/xtrabackup/increament/2017-06-09_04-02-16/
170609 04:24:45 completed OK! #结果出现completed OK表示完全成功
说明: /xtrabackup/full/2017-06-09_03-53-51 为全备基目录 , incremental-dir 为增量备份目录
9.将原有文件夹重命名到新位置,并创建原文件夹
[root@mysql02 full]# mv /data/mysql /data/mysqlbak
[root@mysql02 full]# mkdir -p /data/mysql
10.执行拷贝恢复的文件到原来的数据位置
[root@mysql02 full]# innobackupex --defaults-file=/etc/my.cnf --user=root --copy-back /xtrabackup/full/2017-06-09_03-53-51/
170609 04:33:06 completed OK! #结果出现completed OK表示完全成功
说明: /xtrabackup/full/2017-06-09_03-53-51/ 为全备基目录
11. 权限修改
[root@mysql02 ~]# mkdir -p /data/mysql/binarylog (说明:这里我binlog在datadir在路径下,所以要单独为binlog创建目录)
chown -R mysql:mysql /data/mysql
12. 启动被恢复的实例
[root@mysql02 mysql]# mysqld_safe --defaults-file=/etc/my.cnf &
[root@mysql02 ~]# mysql -uroot -poracle
mysql> use inc_rec;
mysql> select * from andy;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 | > 恢复成功!
| 4 |
+------+
总结:
a、增量备份是基于增量或全备的基础之上完成的。
b、增量备份的基础是InnoDB引擎使用了LSN机制,非InnoDB引擎不存在增量备份的说法,每次都是全备。
c、对于增量备份的恢复期间需要对已提交的事务前滚,未提交的事务回滚。
d、增量备份的恢复应按照备份的顺利逐个逐个replay,需要使用--apply-log --redo-only选项。
e、仅仅最后一个增量备份不需要使用--redo-only选项。
f、如果要做完全恢复或时点恢复,需要结合binlog来实现。
参考:http://blog.csdn.net/leshami/article/details/42141627 感谢大师
mysql之 Innobackupex(全备+增量)备份恢复的更多相关文章
- MySQL 利用xtrabackup进行增量备份详细过程汇总 (转)
Xtrabackup下载.安装以及全量备份请参考:http://blog.itpub.net/26230597/viewspace-1465772/ 1,创建mysql备份用户 mysql -uroo ...
- MySQL for OPS 06:备份恢复
写在前面的话 人在河边走,湿鞋是早晚是事情,操作服务器,数据库也一样.谁也不知道自己哪一天控制不住自己就手贱.这时候有两个东西能救我们,一是备份,二是 bin log,bin log 前面讲了,但是 ...
- mysql如何从全备文件中恢复单个库或者单个表
mysql如何从全备文件中恢复单个库或者单个表 在mysql dba的日常实际工作中,一个实例下有多个库,而我们常见的备份就是全库备份.那么问题就来了,如果需要恢复单个库或者单个表,怎么办了,网上有很 ...
- MySQL和MSSQL差异(增量)备份的原理
MySQL和MSSQL差异(增量)备份的原理 对于真正的增量备份来说,只需要记录当前每页最后的检查点的LSN,如果大于之前全备时的LSN,则备份该页面,否则不用备份 这大大加快了备份速度和恢复时间,同 ...
- 如何通过rman的增量备份恢复dataguard中standby端的数据
很多正在使用dataguard的客户,都会遇到一个棘手的问题: 在备份端与主库同步的过程中由于网络原因或磁盘问题导致一个或多个归档日志丢失,进而dataguard同步无法继续.很多客户都选择了重新全库 ...
- ORACLE异机增量备份恢复
PROD异机增量备份恢复验证实施文档 准备工作:source 源库:PROD数据库备份策略:周日0级RMAN备份,周一至周六1级差异增量备份0 4 * * 0 /data/rmanlev0.sh &g ...
- MySQL innobackupex全量备份恢复
转自 http://blog.itpub.net/27099995/viewspace-1295099/ 先简单介绍一下这个工具:innobackupexinnobackupex比xtarbackup ...
- 【转】mysql增量备份恢复实战企业案例
来源地址:http://seanlook.com/2014/12/05/mysql_incremental_backup_example/ 小量的数据库可以每天进行完整备份,因为这也用不了多少时间,但 ...
- mysql之 Innobackupex全备恢复(原理、演示)
一. Innobackupex恢复原理 After creating a backup, the data is not ready to be restored. There might b ...
- mysql用户授权及数据备份恢复
用户授权与权限撤销 修改数据库管理员从本机登陆的密码测试: mysqladmin -hlocalhost -uroot -p password "新密码" Enter passwo ...
随机推荐
- windows 服务安装报错
使用windows服务开发的定时任务,在win7上都运行良好,在windows server 2008上运行报错,报错信息如下 错误应用程序名称: GCWindowsService.exe,版本: 1 ...
- mongodb 中的Multikey Index Bounds解释$elemMatch
首先说一下 $elemMatch的用法: { _id: 1, results: [ 82, 85, 88 ] } { _id: 2, results: [ 75, 88, 89 ] } $elemMa ...
- EF与手写Model的区别以及联系
1.在数据库表名上,EF是随意的,但是如果是Model的话,就应该在建立数据库的时候考虑到讲数据库表名变为复数,如Movie.cs 数据库应该为Movies
- nova instance启动中的同步与异步
instance create:nova/api/servers.create -- self.compute_api.create -- self.compute_task_api.build_in ...
- python中常见的日期换算
time模块提供各种操作时间的函数 说明:一般有两种表示时间的方式: 第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的 第二种 ...
- 调用摄像头并将其显示在UGUI image上自适应屏幕大小
参考链接:http://www.cnblogs.com/Erma-king/p/5869177.html 不过该博主是竖屏,我的是横屏 代码修改: using UnityEngine; using S ...
- 绑定当前对象例子——Tag="{Binding}"
<TreeView Margin="10,5,0,0" HorizontalAlignment="Left" VerticalAlignment=&qu ...
- hdu 2147 kiki's game(找规律)
kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/10000 K (Java/Others)Total ...
- 如何学习Android系统源码(转)
一. Android系统的源代码非常庞大和复杂,我们不能贸然进入,否则很容易在里面迷入方向,进而失去研究它的信心.我们应该在分析它的源代码之前学习好一些理论知识,下面就介绍一些与Android系统相关 ...
- AliRedis单机180w QPS, 8台服务器构建1000w QPS Cache集群(转)
http://blog.sina.com.cn/s/blog_e59371cc0101br74.html 引言: 如今redis凭借其高性能的优势, 以及丰富的数据结构作为cache已越 ...