innobackupex恢复mysql数据

获取备份

首先,备份文件先获取。
比如,我们选取备份文件为:
2015-04-20_05-00-06,该文件下有如下文件:

1
2
3
4
5
6
7
8
-rw-r–r–. 1 root root 359 Apr 20 15:54 backup-my.cnf
-rw-r—–. 1 root root 3800039424 Apr 21 10:12 ibdata1
-rw-r–r–. 1 root root 50331648 Apr 21 10:12 ib_logfile0
-rw-r–r–. 1 root root 50331648 Apr 21 10:12 ib_logfile1
-rw-r–r–. 1 root root 48 Apr 21 10:12 xtrabackup_binlog_pos_innodb
-rw-r–r–. 1 root root 77 Apr 21 10:12 xtrabackup_checkpoints
-rw-r—–. 1 root root 2097152 Apr 21 10:11 xtrabackup_logfile
-rw-r—–. 1 root root 5 Apr 20 15:54 xtrabackup_suspended_2

恢复步骤

1.停止mysql服务

[root@vanelife-mysql ~]# mysqladmin -uroot -p -S /data/mysql-5.6/3306/mysql.sock shutdown

2.备份原有数据物理文件

[root@vanelife-mysql ~]# cp -rf /data/mysql-5.6 /data/mysql-5.6_bak

3.删除原有数据物理文件

[root@vanelife-mysql ~]# rm -rf /data/mysql-5.6/3306/data/*

4.恢复

[root@vanelife-mysql 3306]# innobackupex –user=root –defaults-file=/data/mysql-5.6/3306/my.cnf –apply-log /data/2015-04-20_05-00-06/
[root@vanelife-mysql 3306]# innobackupex –user=root –defaults-file=/data/mysql-5.6/3306/my.cnf –copy-back /data/2015-04-20_05-00-06/

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona LLC and/or its affiliates 2009-2013. All Rights Reserved.

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

Get the latest version of Percona XtraBackup, documentation, and help resources:
http://www.percona.com/xb/p

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

Warning: World-writable config file ‘/data/mysql-5.6/3306/my.cnf’ is ignored
innobackupex: Error: Original data directory ‘.’ is not empty! at /usr/bin/innobackupex line 2162.

出现这个错误主要是因为数据datadir里面不为空。
清空/data/mysql-5.6/3306/data目录

[root@vanelife-mysql mysql-5.6]# innobackupex –user=root –defaults-file=/data/mysql-5.6/3306/my.cnf –copy-back /data/2015-04-20_05-00-06/

InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona LLC and/or its affiliates 2009-2013. All Rights Reserved.

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

Get the latest version of Percona XtraBackup, documentation, and help resources:
http://www.percona.com/xb/p

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 ‘/data/2015-04-20_05-00-06’
innobackupex: back to original data directory ‘/data/mysql-5.6/3306/data’
innobackupex: Copying ‘/data/2015-04-20_05-00-06/xtrabackup_suspended_2’ to ‘/data/mysql-5.6/3306/data/xtrabackup_suspended_2’
innobackupex: Copying ‘/data/2015-04-20_05-00-06/xtrabackup_binlog_pos_innodb’ to ‘/data/mysql-5.6/3306/data/xtrabackup_binlog_pos_innodb’

innobackupex: Starting to copy InnoDB system tablespace
innobackupex: in ‘/data/2015-04-20_05-00-06’
innobackupex: back to original InnoDB data directory ‘/data/mysql-5.6/3306/data’
innobackupex: Copying ‘/data/2015-04-20_05-00-06/ibdata1’ to ‘/data/mysql-5.6/3306/data/ibdata1’

innobackupex: Starting to copy InnoDB undo tablespaces
innobackupex: in ‘/data/2015-04-20_05-00-06’
innobackupex: back to ‘/data/mysql-5.6/3306/data’

innobackupex: Starting to copy InnoDB log files
innobackupex: in ‘/data/2015-04-20_05-00-06’
innobackupex: back to original InnoDB log directory ‘/data/mysql-5.6/3306/data’
innobackupex: Copying ‘/data/2015-04-20_05-00-06/ib_logfile1’ to ‘/data/mysql-5.6/3306/data/ib_logfile1’
innobackupex: Copying ‘/data/2015-04-20_05-00-06/ib_logfile0’ to ‘/data/mysql-5.6/3306/data/ib_logfile0’
innobackupex: Finished copying back files.

150421 10:30:50 innobackupex: completed OK!

其他

启动的时候报错1:
2015-04-21 10:56:45 7febe7b0f720 InnoDB: Warning: Using innodb_additional_mem_pool_size is DEPRECATED. This option may be removed in future releases, together with the option innodb_use_sys_malloc and with the InnoDB’s internal memory allocator.
2015-04-21 10:56:45 59528 [Note] InnoDB: Using atomics to ref count buffer pool pages
2015-04-21 10:56:45 59528 [Note] InnoDB: The InnoDB memory heap is disabled
2015-04-21 10:56:45 59528 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2015-04-21 10:56:45 59528 [Note] InnoDB: Memory barrier is not used
2015-04-21 10:56:45 59528 [Note] InnoDB: Compressed tables use zlib 1.2.3
2015-04-21 10:56:45 59528 [Note] InnoDB: Not using CPU crc32 instructions
2015-04-21 10:56:45 59528 [Note] InnoDB: Initializing buffer pool, size = 256.0M
2015-04-21 10:56:45 59528 [Note] InnoDB: Completed initialization of buffer pool
2015-04-21 10:56:45 59528 [ERROR] InnoDB: ./ibdata1 can’t be opened in read-write mode
2015-04-21 10:56:45 59528 [ERROR] InnoDB: The system tablespace must be writable!
2015-04-21 10:56:45 59528 [ERROR] Plugin ‘InnoDB’ init function returned error.
2015-04-21 10:56:45 59528 [ERROR] Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed.
2015-04-21 10:56:45 59528 [ERROR] /usr/local/mysql-5.6/bin/mysqld: unknown variable ‘table_cache=614’
2015-04-21 10:56:45 59528 [ERROR] Aborting

原因是权限问题,恢复的时候我用的是root用户,所以ibdata1的权限不够大,修改权限即可。
由于mysql版本问题,有些属性已经改变, table_cache改为属性table_open_cache = 614

报错2:
2015-04-21 11:07:50 61514 [ERROR] /usr/local/mysql-5.6/bin/mysqld: unknown variable ‘log-slow-queries=/data/mysql-5.6/3306/slow.log’
5.6版本log-slow-queries取消。

[root@vanelife-mysql data]# chown mysql:mysql /data/mysql-5.6 -R

启动mysql:

[root@vanelife-mysql mysql-5.6]# mysqld_safe –defaults-file=/data/mysql-5.6/3306/my.cnf &