Innobackupex全备恢复(原理、演示)

时间:2022-09-15 09:27:11

一、  Innobackupex恢复原理
    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.
    To prepare a backup with innobackupex you have to use the --apply-log and the path to the backup directory as an argument:

Innobackupex replayed the committed transactions in the log files (some transactions could have been done while the backup was being done) and rolled back the uncommitted ones. Once this is done, all the information lay in the tablespace (the InnoDB files), and the log files are re-created.

在备份期间(copy数据时)事务存在不一致,即copy开始时,有些事务已开始,有些刚刚开始,而copy结束前或结束后才提交或回滚。
    这些不确定的事务需要在恢复前来确定最终是否最终提交或回滚。在这个阶段的操作称之为prepare阶段。
    这个prepare阶段依赖于备份时的xtrabackup log(来自innodb logfile),使用--apply-log参数实现一致性。
    --apply-log参数会根据xtrabackup log做相应的前滚或回滚,完成后会重建innodb logfile文件。

The --use-memory option The preparing process can be speed up by using more memory in it. It depends on the free or available RAM on your system, it defaults to 100MB. In general, the more memory available to the process,the better. The amount of memory used in the process can be specified by multiples of bytes:
    恢复期间,--use-memory选项可以加速prepare过程,如果系统可用内存够大的话,该值缺省被设置为100MB。

Innobackupex恢复示意图
Innobackupex全备恢复(原理、演示)

二、  全备恢复流程

1. 当前环境
mysql> show variables like 'version';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| version | 5.6.25-log |
+---------------+------------+
1 row in set (0.00 sec)
2. 准备实验数据
mysql> create database hot_recover;
Query OK, 1 row affected (0.04 sec)

mysql> use hot_recover;
Database changed
mysql> create table test(id int);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into test values(1);
Query OK, 1 row affected (0.01 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
3. 数据库全备
[root@mysql01 ~]#innobackupex --user=root --password=oracle --port=3606 /xtrabackup/full/
xtrabackup: Transaction log of lsn (1631855) to (1631855) was copied.
170608 20:54:21 completed OK!
4. 查看备份产生的内容
[root@mysql02 mysql]# ll /xtrabackup/full/
drwxr-x---. 6 root root 4096 Jun 8 20:54 2017-06-08_20-54-14
[root@mysql02 mysql]# ll /xtrabackup/full/2017-06-08_20-54-14
total 12324
-rw-r-----. 1 root root 418 Jun 8 20:54 backup-my.cnf
drwxr-x---. 2 root root 4096 Jun 8 20:54 hot_recover
-rw-r-----. 1 root root 12582912 Jun 8 20:54 ibdata1
drwxr-x---. 2 root root 4096 Jun 8 20:54 mysql
drwxr-x---. 2 root root 4096 Jun 8 20:54 performance_schema
drwxr-x---. 2 root root 4096 Jun 8 20:54 test
-rw-r-----. 1 root root 18 Jun 8 20:54 xtrabackup_binlog_info
-rw-r-----. 1 root root 113 Jun 8 20:54 xtrabackup_checkpoints
-rw-r-----. 1 root root 482 Jun 8 20:54 xtrabackup_info
-rw-r-----. 1 root root 2560 Jun 8 20:54 xtrabackup_logfile

5. 在线删除数据,做恢复对比
mysql> drop table test;
Query OK, 0 rows affected (0.24 sec)

mysql> select * from test;
ERROR 1146 (42S02): Table 'hot_recover.test' doesn't exist

6. 停止mysql数据库
[root@mysql01 ~]# service mysql stop
Shutting down MySQL.... [ OK ]
[root@mysql01 ~]# netstat -nltp|grep mysql|grep 3606

7. apply-log ,准备全备文件
[root@mysql02 ~]# innobackupex --apply-log --user=oracle --password=oracle --port=3606 /xtrabackup/full/2017-06-08_20-54-14
170608 20:56:38 innobackupex: Starting the apply-log operation ##开始 apply-log
IMPORTANT: Please check that the apply-log run completes successfully.
At the end of a successful apply-log run innobackupex
prints "completed OK!".
innobackupex version 2.4.7 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 6f7a799)
xtrabackup: cd to /xtrabackup/full/2017-06-08_20-54-14/
xtrabackup: This target seems to be not prepared yet.
InnoDB: Number of pools: 1
xtrabackup: xtrabackup_logfile detected: size=8388608, start_lsn=(1631855)
xtrabackup: using the following InnoDB configuration for recovery:
xtrabackup: innodb_data_home_dir = .
xtrabackup: innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup: innodb_log_group_home_dir = .
xtrabackup: innodb_log_files_in_group = 1
xtrabackup: innodb_log_file_size = 8388608
xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
。。。。省略
InnoDB: Initializing buffer pool, total size = 100M, instances = 1, chunk size = 100M
InnoDB: Completed initialization of buffer pool
InnoDB: page_cleaner coordinator priority: -20
InnoDB: Highest supported file format is Barracuda.
InnoDB: The log sequence number 1626143 in the system tablespace does not match the log sequence number 1631855 in the ib_logfiles!
InnoDB: Starting crash recovery. ##开始innodb实例恢复
InnoDB: xtrabackup: Last MySQL binlog file position 586, file name binlog.000008
InnoDB: Removed temporary tablespace data file: "ibtmp1"
InnoDB: Creating shared tablespace for temporary tables
InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
InnoDB: File './ibtmp1' size is now 12 MB.
InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active.
InnoDB: 32 non-redo rollback segment(s) are active.
InnoDB: Waiting for purge to start
InnoDB: 5.7.13 started; log sequence number 1632277
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1632296
170608 20:56:44 completed OK! ##成功恢复

8. 查看恢复后文件的相关信息
(时间为 20:56 的文件是刚才更新的或者生成的,主要是生成了系统表空间数据文件及innodb日志文件,更新了Innodb的检查点文件)
[root@mysql02 mysql]# ll /xtrabackup/full/2017-06-08_20-54-14/
total 131108
-rw-r-----. 1 root root 418 Jun 8 20:54 backup-my.cnf
drwxr-x---. 2 root root 4096 Jun 8 20:54 hot_recover
-rw-r-----. 1 root root 12582912 Jun 8 20:56 ibdata1
-rw-r-----. 1 root root 50331648 Jun 8 20:56 ib_logfile0
-rw-r-----. 1 root root 50331648 Jun 8 20:56 ib_logfile1
-rw-r-----. 1 root root 12582912 Jun 8 20:56 ibtmp1
drwxr-x---. 2 root root 4096 Jun 8 20:54 mysql
drwxr-x---. 2 root root 4096 Jun 8 20:54 performance_schema
drwxr-x---. 2 root root 4096 Jun 8 20:54 test
-rw-r-----. 1 root root 18 Jun 8 20:54 xtrabackup_binlog_info
-rw-r--r--. 1 root root 18 Jun 8 20:56 xtrabackup_binlog_pos_innodb
-rw-r-----. 1 root root 113 Jun 8 20:56 xtrabackup_checkpoints
-rw-r-----. 1 root root 482 Jun 8 20:54 xtrabackup_info
-rw-r-----. 1 root root 8388608 Jun 8 20:56 xtrabackup_logfile

9. 将原有文件夹重命名到新位置,并创建原文件夹
[root@mysql02 mysql]# mv /data/mysql /data/mysqlbak
[root@mysql02 mysql]# mkdir -p /data/mysql

10. 将已经恢复好的数据文件复制回原始位置
[root@mysql02 mysql]# innobackupex --defaults-file=/etc/my.cnf --user=root --password=oracle --port=3606 --copy-back /xtrabackup/full/2017-06-08_20-54-14/
170608 21:01:02 innobackupex: Starting the copy-back operation ##启动将备份的文件复制回原路径
innobackupex version 2.4.7 based on MySQL server 5.7.13 Linux (x86_64) (revision id: 6f7a799)
170608 21:01:02 [01] Copying ib_logfile0 to /data/mysql/ib_logfile0
170608 21:01:03 [01] ...done
170608 21:01:03 [01] Copying ib_logfile1 to /data/mysql/ib_logfile1
。。。。省略
170608 21:01:07 [01] Copying ./performance_schema/table_io_waits_summary_by_table.frm to /data/mysql/performance_schema/table_io_waits_summary_by_table.frm
170608 21:01:07 [01] ...done
170608 21:01:07 [01] Copying ./performance_schema/events_stages_history_long.frm to /data/mysql/performance_schema/events_stages_history_long.frm
170608 21:01:07 [01] ...done
170608 21:01:07 completed OK! ##copy结束

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 &
13. 登录,验证
[root@mysql02 ~]# mysql -uroot -poracle
mysql> select * from test;
+------+
| id |
+------+
| 1 | >恢复成功!
+------+

Innobackupex全备恢复(原理、演示)的更多相关文章

  1. mysql之 Innobackupex全备恢复(原理、演示)

    一.  Innobackupex恢复原理    After creating a backup, the data is not ready to be restored. There might b ...

  2. xtrabackup之Innobackupex全备恢复

    一.当前环境 [mysql@hadoop1 ~]$ mysql --defaults-/my.cnf -uroot -p123456 -P3306 mysql> show variables l ...

  3. 基于Innobackupex的全备恢复

    对于MySQL数据库的热备,xtrabackup是大多数DBA朋友们的选择.xtrabackup内嵌了一个innobackupex可用于热备MySQL数据库.本文描写叙述了基于innobackupex ...

  4. (4.10)mysql备份还原——利用binlog+全备恢复误删表【不推荐使用】

    关键误操作:mysql误删除 1.备份+binlog恢复数据 [1.1]场景:不小心误删除某张表 [1.2]解决方法:在另外一台机器,恢复全库+日志,然后导出删除的表,再插入会生产库. [1.3]案例 ...

  5. NT平台ADSL拨号连接密码恢复原理

    NT平台拨号连接密码恢复原理 创建时间:2004-11-08 更新时间:2004-11-12文章属性:原创文章提交:eyas (eyas_at_xfocus.org)NT平台拨号连接密码恢复原理Aut ...

  6. Oracle特殊恢复原理与实战(DSI系列)

    1.深入浅出Oracle(DSI系列Ⅰ) 2.Oracle特殊恢复原理与实战(DSI系列Ⅱ) 3.Oracle SQL Tuning(DSI系列Ⅲ)即将开设 4.Oracle DB Performan ...

  7. Innobackupex 全备数据库

    对于MySQL数据库的热备.xtrabackup是除了MySQL enterprise backup之外的不二之选. 该工具提供了基于innodb存储引擎的热备.支持全量,增量备份,部分备份,时点恢复 ...

  8. 记一次innobackupex备份恢复数据库过程

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

  9. VBA Dumper v0.1.4.2, 提取office文档中的VBA代码,监查宏病毒恢复代码(演示版

    http://club.excelhome.net/thread-970051-1-1.html VBA Dumper 0.1.4.2更新,填补国内同类程序空白 此程序为演示版,可以在无office的 ...

随机推荐

  1. jquery中的$的特殊用法

    通过父级元素选取子元素, $('父元素选择器,子元素选择器')        $('子元素选择器',父元素jquery对象); 通过$创建代码片段 $('<div/>',{ 'class' ...

  2. MyEclipse 中各种 libraries 的含义

    MyEclipse 中各种 libraries 的含义       JRE System Library,Java EE 5 Libraries,Referenced  Libraries这三个都是各 ...

  3. JDBC链接

    //1. MySQL(http://www.mysql.com)mm.mysql-2.0.2-bin.jar  Connection con = null;  Class.forName( &quot ...

  4. Chromium如何显示Web页面

    Displaying A Web Page In Chrome 概念化的应用分层 参见原文档:http://goo.gl/MsEJX 每一个box代表一个抽象层.下层不依赖于上层. WebKit:渲染 ...

  5. Android IPC机制全解析&lt&semi;二&gt&semi;

    在AIDL文件中并不是所有的数据类型都可以使用,AIDL支持的数据类型如下: 基本数据类型(int.long.char.boolean.double等) String和CharSequence Lis ...

  6. Gradle 同步时报错,Could not find com&period;android&period;support&period;constraint&colon;constraint-layout&colon;1&period;0&period;0-alpha8的解决方法

    Error:Could not find com.android.support.constraint:constraint-layout:1.0.0-alpha8. 原因: SDK 中可能是没有安装 ...

  7. Python OpenCV 图像相识度对比

    强大的openCV能做什么我就不啰嗦,你能想到的一切图像+视频处理. 这里,我们说说openCV的图像相似度对比, 嗯,说好听一点那叫图像识别,但严格讲, 图像识别是在一个图片中进行类聚处理,比如图片 ...

  8. 页面中去除浮动 clear&colon;both

    今天写代码发现一个很奇怪的问题,发现上面的div加浮动(不管是否包含div)以后对下面div的浮动有所影响,通过去除浮动,搞定: 只需要在受影响的div中的样式中,加入clear:both即可

  9. abp Cannot access a disposed object&period; A common cause of this error is disposing

    框架:abp 异常信息: An unhandled exception was thrown by the application.System.ObjectDisposedException: Ca ...

  10. &period;NET Core微服务之路:文章系列和内容索引汇总 &lpar;v0&period;52&rpar;

    微服务架构,对于从事JAVA架构的童鞋来说,早已不是什么新鲜的事儿,他们有鼎鼎大名的Spring Cloud这样的全家桶框架支撑,包含微服务核心组件如 1. Eureka:实现服务注册与发现. 2. ...