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

一、  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 ?les 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 ?les (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 ?les), and the log ?les 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 speci?ed by multiples of bytes:
    恢复期间,--use-memory选项可以加速prepare过程,如果系统可用内存够大的话,该值缺省被设置为100MB。

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. 数据库全备
[[email protected] ~]#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. 查看备份产生的内容
[[email protected] mysql]# ll /xtrabackup/full/
drwxr-x---. 6 root root 4096 Jun 8 20:54 2017-06-08_20-54-14
[[email protected] 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数据库
[[email protected] ~]# service mysql stop
Shutting down MySQL.... [ OK ]
[[email protected] ~]# netstat -nltp|grep mysql|grep 3606

7. apply-log ,准备全备文件
[[email protected] ~]# 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的检查点文件)
[[email protected] 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. 将原有文件夹重命名到新位置,并创建原文件夹
[[email protected] mysql]# mv /data/mysql /data/mysqlbak
[[email protected] mysql]# mkdir -p /data/mysql

10. 将已经恢复好的数据文件复制回原始位置
[[email protected] 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. 权限修改
[[email protected] ~]# mkdir -p /data/mysql/binarylog (说明:这里我binlog在datadir在路径下,所以要单独为binlog创建目录)
chown -R mysql:mysql /data/mysql
12. 启动被恢复的实例
[[email protected] mysql]# mysqld_safe --defaults-file=/etc/my.cnf &
13. 登录,验证
[[email protected] ~]# mysql -uroot -poracle
mysql> select * from test;
+------+
| id |
+------+
| 1 | >恢复成功!
+------+

时间: 2024-10-13 10:12:38

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

xtrabackup之Innobackupex全备恢复

一.当前环境 [[email protected] ~]$ mysql --defaults-file=/mysql/mysqldata/3306/my.cnf -uroot -p123456 -P3306 mysql> show variables like 'version'; +---------------+------------+ | Variable_name | Value | +---------------+------------+ | version | 5.6.29-l

基于Innobackupex的全备恢复

对于MySQL数据库的热备,xtrabackup是大多数DBA朋友们的选择.xtrabackup内嵌了一个innobackupex可用于热备MySQL数据库.本文描写叙述了基于innobackupex这个工具全备下的恢复并给出演示供大家參考. 有关Innobackupex的全备可參考:Innobackupex 全备数据库 1.Innobackupex恢复原理    After creating a backup, the data is not ready to be restored. The

innobackupex备份和恢复原理

备份原理: 1.找到redolog中最后一个checkpoint的lsn,从这个lsn开始记录所有新的redolog 2.cpinnodb数据 3.flush tables with read lock (加全局锁) 4.复制所有myisam表和所有表的表结构文件 5.锁定binlog文件 6.记录当前的binlogfile和pos 恢复原理: innobackupex --apply-log --redo-only innobackupex恢复和mysql系统崩溃的原理一样,通过比对页的lsn

ubuntu环境下数据误删除恢复--extundelete的恢复原理

1. 数据恢复软件extundelete介绍 作为一名运维人员,保证数据的安全是根本职责,所以在维护系统的时候,要慎之又慎,但是有时难免会出现数据被误删除的情况,在这个时候该如何快速.有效地恢复数据呢?本节我们就来介绍一下Linux系统下常用的几个数据恢复工具. 2. 如何使用“rm -rf”命令 在Linux系统下,通过命令“rm -rf”可以将任何数据直接从硬盘删除,并且没有任何提示,同时Linux下也没有与Windows下回收站类似的功能,也就意味着,数据在删除后通过常 规的手段是无法恢复

事务与数据库恢复原理

事务与数据库恢复原理 数据存储的逻辑单位是数据块,数据操作的逻辑单位是事务. 事务是用户定义的一组操作序列,有一条或多条相关SQL语句组成,是数据库应用程序的基本逻辑单位.事务管理技术主要包括数据库的恢复技术和并发控制技术. 事务的处理逻辑过程 1. 服务器进程捡取该事务的SQL语句,然后检查共享池,查看是否包含该SQL语句的共享SQL区. 2. 如有,则检查该用户是否有访问所涉及的数据的权限,如有,则使用共享SQL区处理该SQL语句.如果没有找到共享SQL区,则分配一个新的SQL区,以便分析.

Innobackupex 全备数据库

对于MySQL数据库的热备,xtrabackup是除了MySQL enterprise backup之外的不二之选.该工具提供了基于innodb存储引擎的热备,支持全量,增量备份,部分备份,时点恢复以及使用xtrabackup备份来重做slave等.xtrabackup工具包包含一个innobackupex命令行工具,同时支持InnoDB引擎以及MyISAM引擎.本文主要描述的是trabackup的备份原理并给出了相关演示. 1.安装部署Xtrabackup  下载地址:http://www.p

innobackupex备份恢复操作实录

下面开始做全备 [[email protected] mysql]# innobackupex --defaults/file=/etc/my.cnf /tmp 执行完以后产生/tmp/2017-03-28_15-50-37全备文件 下面做增量备份 innobackupex --incremental /tmp --incremental-basedir=/tmp/2017-03-28_15-50-37 产生文件/tmp/2017-03-28_15-52-55 下面做增量恢复,--apply-l

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

简介:以前备份都是通过mysqldump备份数据库的,由于是逻辑备份,所以采用这种备份方式数据是很安全的,跨平台.版本都很容易.凡事有利必有弊,逻辑备份在你数据库比较大时,备份.恢复数据所耗费的时间也是蛮长的,所以要不断改进,可以使用物理备份.Xtrabackup是由percona开发的一个开源软件,它是innodb热备工具ibbackup(收费的商业软件)的一个开源替代品.Xtrabackup由个部分组成:xtrabackup和innobackupex,其中xtrabackup工具用于备份in

Xtrabackup之innobackupex备份恢复详解(转)

原文:http://ourlinux.blog.51cto.com/274624/844854 安装配置Xtrabackup先看看如何安装Xtrabackup,最简单的安装方式是使用RPM包,不过想使用源代码方式安装的话,其安装方式有点古怪,因为它采用的在MySQL源代码上打补丁构建的方式安装的.这里使用二进制包的安装方式,相对比较灵活.Shell> mkdir /usr/local/xtrabackupShell> tar -zxvf xtrabackup-1.6.tar.gz –C /us