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-log |
+---------------+------------+
1 row in set (0.00 sec)

mysql> 

#完全备份集
[[email protected] 2016-05-27_00-23-05]# ls -l
total 153824
-rw-rw-r-- 1 mysql mysql       290 May 27 00:23 backup-my.cnf
-rw-rw---- 1 mysql mysql 104857600 May 27 00:23 ibdata01
drwx------ 2 mysql mysql      4096 May 27 00:23 mysql
drwxrwxr-x 2 mysql mysql      4096 May 27 00:23 performance_schema
drwx------ 2 mysql mysql      4096 May 27 00:23 pls_innodb
drwxrwxr-x 2 mysql mysql      4096 May 27 00:23 pls_myisam
drwxrwxr-x 2 mysql mysql      4096 May 27 00:23 test
-rw-rw---- 1 mysql mysql  10485760 May 27 00:23 undo001
-rw-rw---- 1 mysql mysql  10485760 May 27 00:23 undo002
-rw-rw---- 1 mysql mysql  10485760 May 27 00:23 undo003
-rw-rw---- 1 mysql mysql  10485760 May 27 00:23 undo004
-rw-rw---- 1 mysql mysql  10485760 May 27 00:23 undo005
-rw-rw-r-- 1 mysql mysql        13 May 27 00:23 xtrabackup_binary
-rw-rw-r-- 1 mysql mysql        24 May 27 00:23 xtrabackup_binlog_info
-rw-rw---- 1 mysql mysql        77 May 27 00:23 xtrabackup_checkpoints
-rw-rw---- 1 mysql mysql      2560 May 27 00:23 xtrabackup_logfile
[[email protected] 2016-05-27_00-23-05]# 

二、关闭原有实例

[[email protected] ~]$ mysqladmin --defaults-file=/mysql/mysqldata/3306/my.cnf -uroot -p123456 -S /mysql/mysqldata/3306/mysql.sock shutdown
[[email protected] ~]$ netstat -an|grep 3306
#将原有数据库重命名
[[email protected] mysql]$ mv 3306  3306_bk
[[email protected] mysql]$ mkdir 3306

#将备份集备份
[[email protected] 3306_full]# ls
2016-05-27_00-23-05
[[email protected] 3306_full]# cp -rf  2016-05-27_00-23-05 2016-05-27_00-23-05_02

三、准备全备文件,当前并不做实际的恢复

#创建临时文件夹,否则会报错
[[email protected] 3306]$ mkdir tmp
[[email protected] 3306]$ innobackupex --defaults-file=/mysql/mysqldata/my.cnf --apply-log /mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona Ireland Ltd 2009-2012.  All Rights Reserved.

This software is published under
the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.

IMPORTANT: Please check that the apply-log run completes successfully.
           At the end of a successful apply-log run innobackupex
           prints "completed OK!".

160527 01:14:19  innobackupex: Starting ibbackup with command: xtrabackup_56  --defaults-file="/mysql/mysqldata/my.cnf"  --defaults-group="mysqld" --prepare --target-dir=/mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05 --tmpdir=/mysql/mysqldata/3306/tmp

xtrabackup_56 version 2.0.7 for MySQL server 5.6.10 Linux (x86_64) (revision id: 552)
xtrabackup: cd to /mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05
xtrabackup: This target seems to be not prepared yet.
xtrabackup: xtrabackup_logfile detected: size=2359296, start_lsn=(1527120)
xtrabackup: Temporary instance for recovery is set as followings.
xtrabackup:   innodb_data_home_dir = ./
xtrabackup:   innodb_data_file_path = ibdata01:100M:autoextend
xtrabackup:   innodb_log_group_home_dir = ./
xtrabackup:   innodb_log_files_in_group = 1
xtrabackup:   innodb_log_file_size = 2359296
xtrabackup: Temporary instance for recovery is set as followings.
xtrabackup:   innodb_data_home_dir = ./
xtrabackup:   innodb_data_file_path = ibdata01:100M:autoextend
xtrabackup:   innodb_log_group_home_dir = ./
xtrabackup:   innodb_log_files_in_group = 1
xtrabackup:   innodb_log_file_size = 2359296
xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
InnoDB: The InnoDB memory heap is disabled
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Compressed tables use zlib 1.2.3
InnoDB: CPU does not support crc32 instructions
InnoDB: Initializing buffer pool, size = 100.0M
InnoDB: Completed initialization of buffer pool
InnoDB: Opened 5 undo tablespaces
InnoDB: Highest supported file format is Barracuda.
InnoDB: The log sequence numbers 1512687 and 1512687 in ibdata files do not match the log sequence number 1527120 in the ib_logfiles!
InnoDB: Database was not shutdown normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages
InnoDB: from the doublewrite buffer...
InnoDB: Last MySQL binlog file position 0 1714, file name mysql-bin.000003
InnoDB: 128 rollback segment(s) are active.
InnoDB: Waiting for purge to start
2016-05-27 01:14:20 2b7849f40940  InnoDB: Warning: table ‘mysql/innodb_index_stats‘
InnoDB: in InnoDB data dictionary has unknown flags 50.
2016-05-27 01:14:20 2b7849f40940  InnoDB: Warning: table ‘mysql/innodb_table_stats‘
InnoDB: in InnoDB data dictionary has unknown flags 50.
InnoDB: 1.2.10 started; log sequence number 1527120

[notice (again)]
  If you use binary log and don‘t use any hack of group commit,
  the binary log position seems to be:
InnoDB: Last MySQL binlog file position 0 1714, file name mysql-bin.000003

xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1527144

160527 01:14:22  innobackupex: Restarting xtrabackup with command: xtrabackup_56  --defaults-file="/mysql/mysqldata/my.cnf"  --defaults-group="mysqld" --prepare --target-dir=/mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05 --tmpdir=/mysql/mysqldata/3306/tmp
for creating ib_logfile*

xtrabackup_56 version 2.0.7 for MySQL server 5.6.10 Linux (x86_64) (revision id: 552)
xtrabackup: cd to /mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05
xtrabackup: This target seems to be already prepared.
xtrabackup: notice: xtrabackup_logfile was already used to ‘--prepare‘.
xtrabackup: Temporary instance for recovery is set as followings.
xtrabackup:   innodb_data_home_dir = ./
xtrabackup:   innodb_data_file_path = ibdata01:100M:autoextend
xtrabackup:   innodb_log_group_home_dir = ./
xtrabackup:   innodb_log_files_in_group = 3
xtrabackup:   innodb_log_file_size = 134217728
xtrabackup: Temporary instance for recovery is set as followings.
xtrabackup:   innodb_data_home_dir = ./
xtrabackup:   innodb_data_file_path = ibdata01:100M:autoextend
xtrabackup:   innodb_log_group_home_dir = ./
xtrabackup:   innodb_log_files_in_group = 3
xtrabackup:   innodb_log_file_size = 134217728
xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
InnoDB: The InnoDB memory heap is disabled
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Compressed tables use zlib 1.2.3
InnoDB: CPU does not support crc32 instructions
InnoDB: Initializing buffer pool, size = 100.0M
InnoDB: Completed initialization of buffer pool
InnoDB: Setting log file ./ib_logfile101 size to 128 MB
InnoDB: Progress in MB: 100
InnoDB: Setting log file ./ib_logfile1 size to 128 MB
InnoDB: Progress in MB: 100
InnoDB: Setting log file ./ib_logfile2 size to 128 MB
InnoDB: Progress in MB: 100
InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
InnoDB: New log files created, LSN=1527144
InnoDB: Opened 5 undo tablespaces
InnoDB: Highest supported file format is Barracuda.
InnoDB: 128 rollback segment(s) are active.
InnoDB: Waiting for purge to start
2016-05-27 01:14:24 2ac2c62dd940  InnoDB: Warning: table ‘mysql/innodb_index_stats‘
InnoDB: in InnoDB data dictionary has unknown flags 50.
2016-05-27 01:14:24 2ac2c62dd940  InnoDB: Warning: table ‘mysql/innodb_table_stats‘
InnoDB: in InnoDB data dictionary has unknown flags 50.
InnoDB: 1.2.10 started; log sequence number 1527308

[notice (again)]
  If you use binary log and don‘t use any hack of group commit,
  the binary log position seems to be:
InnoDB: Last MySQL binlog file position 0 1714, file name mysql-bin.000003

xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1527318
160527 01:14:26  innobackupex: completed OK!
[[email protected] 3306]$ 

四、查看备份集的变化

[[email protected] 2016-05-27_00-23-05]# pwd
/mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05
[[email protected] 2016-05-27_00-23-05]# ls -l
total 549744
-rw-rw-r-- 1 mysql mysql       290 May 27 00:23 backup-my.cnf
-rw-rw---- 1 mysql mysql 104857600 May 27 01:14 ibdata01
-rw-rw-r-- 1 mysql mysql 134217728 May 27 01:14 ib_logfile0
-rw-rw-r-- 1 mysql mysql 134217728 May 27 01:14 ib_logfile1
-rw-rw-r-- 1 mysql mysql 134217728 May 27 01:14 ib_logfile2
drwx------ 2 mysql mysql      4096 May 27 00:23 mysql
drwxrwxr-x 2 mysql mysql      4096 May 27 00:23 performance_schema
drwx------ 2 mysql mysql      4096 May 27 00:23 pls_innodb
drwxrwxr-x 2 mysql mysql      4096 May 27 00:23 pls_myisam
drwxrwxr-x 2 mysql mysql      4096 May 27 00:23 test
-rw-rw---- 1 mysql mysql  10485760 May 27 01:14 undo001
-rw-rw---- 1 mysql mysql  10485760 May 27 01:14 undo002
-rw-rw---- 1 mysql mysql  10485760 May 27 01:14 undo003
-rw-rw---- 1 mysql mysql  10485760 May 27 01:14 undo004
-rw-rw---- 1 mysql mysql  10485760 May 27 01:14 undo005
-rw-rw-r-- 1 mysql mysql        13 May 27 00:23 xtrabackup_binary
-rw-rw-r-- 1 mysql mysql        24 May 27 00:23 xtrabackup_binlog_info
-rw-rw-r-- 1 mysql mysql        22 May 27 01:14 xtrabackup_binlog_pos_innodb
-rw-rw---- 1 mysql mysql        77 May 27 01:14 xtrabackup_checkpoints
-rw-rw---- 1 mysql mysql   2359296 May 27 01:14 xtrabackup_logfile
[[email protected] 2016-05-27_00-23-05]# 

注意文件时间的变化,说明准备全备文件的操作只是对备份集本身做了相关处理,所以在准备恢复前最好对备份集再次备份

五、将已经恢复好的数据文件复制回原始位置

#手动创建相关目录 否则会报错
[[email protected] 3306]$ mkdir data
[[email protected] 3306]$ mkdir binlog
[[email protected] 3306]$ mkdir logs
[[email protected] 3306]$ mkdir redo
[[email protected] 3306]$ mkdir undo

[[email protected] 3306]$ innobackupex --defaults-file=/mysql/mysqldata/my.cnf --copy-back /mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05
InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona Ireland Ltd 2009-2012.  All Rights Reserved.

This software is published under
the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.

IMPORTANT: Please check that the copy-back run completes successfully.
           At the end of a successful copy-back run innobackupex
           prints "completed OK!".

innobackupex: Starting to copy files in ‘/mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05‘
innobackupex: back to original data directory ‘/mysql/mysqldata/3306/data‘
innobackupex: Copying ‘/mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05/xtrabackup_binlog_pos_innodb‘ to ‘/mysql/mysqldata/3306/data/xtrabackup_binlog_pos_innodb‘
innobackupex: Creating directory ‘/mysql/mysqldata/3306/data/test‘
innobackupex: Creating directory ‘/mysql/mysqldata/3306/data/mysql‘
.......拷贝相关文件
innobackupex: Creating directory ‘/mysql/mysqldata/3306/data/performance_schema‘
.......拷贝相关文件
innobackupex: Creating directory ‘/mysql/mysqldata/3306/data/pls_innodb‘
.......拷贝相关文件
nnobackupex: Creating directory ‘/mysql/mysqldata/3306/data/pls_myisam‘
.......拷贝相关文件

innobackupex: Starting to copy InnoDB system tablespace
innobackupex: in ‘/mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05‘
innobackupex: back to original InnoDB data directory ‘/mysql/mysqldata/3306/data‘
innobackupex: Copying ‘/mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05/ibdata01‘ to ‘/mysql/mysqldata/3306/data/ibdata01‘

innobackupex: Starting to copy InnoDB undo tablespaces
innobackupex: in ‘/mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05‘
innobackupex: back to ‘/mysql/mysqldata/3306/data‘
innobackupex: Copying ‘/mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05/undo005‘ to ‘/mysql/mysqldata/3306/data‘
.......拷贝相关文件

innobackupex: Starting to copy InnoDB log files
innobackupex: in ‘/mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05‘
innobackupex: back to original InnoDB log directory ‘/mysql/mysqldata/3306/redo‘
innobackupex: Copying ‘/mysql/mysqldata/backup/3306_full/2016-05-27_00-23-05/ib_logfile1‘ to ‘/mysql/mysqldata/3306/redo‘
.......拷贝相关文件
innobackupex: Finished copying back files.

160527 01:27:10  innobackupex: completed OK!
[[email protected] 3306]$  

疑问:undo文件还原的位置为data目录,并不是my.cnf中指定的(innodb_undo_directory= /mysql/mysqldata/3306/undo)目录。需手动移到该目录

六、测试恢复结果

[[email protected] logs]$  mysqld_safe --defaults-file=/mysql/mysqldata/3306/my.cnf &

#启动日志
[[email protected] logs]$ more mysql-error.log
160527 01:38:38 mysqld_safe Starting mysqld daemon with databases from /mysql/mysqldata/3306/data
2016-05-27 01:38:48 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.29-log) starting as process 14386 ...
2016-05-27 01:38:49 14386 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-05-27 01:38:49 14386 [Note] InnoDB: The InnoDB memory heap is disabled
2016-05-27 01:38:49 14386 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-05-27 01:38:49 14386 [Note] InnoDB: Memory barrier is not used
2016-05-27 01:38:49 14386 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-05-27 01:38:49 14386 [Note] InnoDB: Not using CPU crc32 instructions
2016-05-27 01:38:49 14386 [Note] InnoDB: Initializing buffer pool, size = 256.0M
2016-05-27 01:38:51 14386 [Note] InnoDB: Completed initialization of buffer pool
2016-05-27 01:38:52 14386 [Note] InnoDB: Opened 5 undo tablespaces
2016-05-27 01:38:52 14386 [Note] InnoDB: Highest supported file format is Barracuda.
2016-05-27 01:38:53 14386 [Note] InnoDB: 128 rollback segment(s) are active.
2016-05-27 01:38:53 14386 [Note] InnoDB: Waiting for purge to start
2016-05-27 01:38:53 14386 [Note] InnoDB: 5.6.29 started; log sequence number 1527318
2016-05-27 01:38:53 14386 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID:
 4c2b3a8e-23cd-11e6-bc50-080027c83f2b.
2016-05-27 01:38:53 14386 [Note] Server hostname (bind-address): ‘*‘; port: 3306
2016-05-27 01:38:53 14386 [Note] IPv6 is not available.
2016-05-27 01:38:53 14386 [Note]   - ‘0.0.0.0‘ resolves to ‘0.0.0.0‘;
2016-05-27 01:38:53 14386 [Note] Server socket created on IP: ‘0.0.0.0‘.
2016-05-27 01:38:53 14386 [Note] Event Scheduler: Loaded 0 events
2016-05-27 01:38:53 14386 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: ‘5.6.29-log‘  socket: ‘/mysql/mysqldata/3306/mysql.sock‘  port: 3306  For mysqltest
[[email protected] logs]$ 

七、小结

a、Xtrabackup恢复的目的要是保证事务(数据)的一致性,Xtrabackup log会记录这些事务备份期间的状态
b、恢复过程分为2个阶段,一个是Prepare阶段,一个是copy back阶段,恢复前关闭原有实例
c、Prepare阶段会根据从innodb logfile捕获出来的信息(记录在xtrabackup log)来进行相应的前滚或回滚
d、Prepare阶段会在成功前滚或回滚后创建新的innodb logfile(空)
e、copy back阶段则是将成功恢复的全部文件复制回原来或指定的数据目录(目录应为空目录)
f、copy back前需要关闭原有实例,如果恢复到不同的实例则不需要
g、copy back完成后应做相应的权限修改
h、启动恢复后的实例并进行相关验证
时间: 2024-12-22 10:29:45

xtrabackup之Innobackupex全备恢复的相关文章

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

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

xtrabackup之Innobackupex全备数据库

一.Xtrabackup是什么: Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品. Xtrabackup有两个主要的工具:xtrabackup.innobackupex xtrabackup只能备份InnoDB和XtraDB两种数据表,而不能备份MyISAM数据表 innobackupex是参考了InnoDB Hotbackup的innoback脚本修改而来的.innobackup

基于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

xtrabackup单库全备&增量

xtrabackup 介绍: xtrabackup有两个主要的工具:innobackupex和xtrabackup,xtrabackup只能备份InnoDB和XtraDB数据表,innobackupex封装了xtrabackup,可以备份MyISAM数据表. 第一步安装: 1.1.下载xtackbackup 地址:http://www.percona.com/downloads/XtraBackup/ 1.2.安装: tar -zxf percona-xtrabackup-2.0.8-587.t

Xtrabackup 增量备份、恢复、原理

整合了网上的一些资料,结合自己的理解,并进行了实验验证 理解一: 1,Xtrabackup是什么 Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品. Xtrabackup有两个主要的工具:xtrabackup.innobackupex (1).xtrabackup只能备份InnoDB和XtraDB两种数据表,而不能备份MyISAM数据表 (2). innobackupex是参考了In

Xtrabackup之innobackupex在线热备mysql数据库

XtraBackup简介 Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好的替代品. Xtrabackup有两个主要的工具:xtrabackup.innobackupex (1)xtrabackup只能备份InnoDB和XtraDB两种数据表,而不能备份MyISAM数据表 (2)innobackupex-1.5.1则封装了xtrabackup,是一个脚本封装,所以能同时备份处理innodb和

MySQL5.7.18 备份、Mysqldump,mysqlpump,xtrabackup,innobackupex 全量,增量备份,数据导入导出

粗略介绍冷备,热备,温暖,及Mysqldump,mysqlpump,xtrabackup,innobackupex 全量,增量备份 --备份的目的 灾难恢复:意外情况下(如服务器宕机.磁盘损坏等)对损坏的数据进行恢复和还原保证数据不丢失,最小程度地丢失需求改变:因需求改变而需要把数据还原到改变以前测试:测试新功能是否可用 --备份与恢复概述 根据备份的方法可以分为: 1.Hot Backup(热备) 2.Cold Backup(冷备) 3.Warm Backup(温备) Hot Backup是指

Innobackupex 全备数据库

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