如何從一個XtraBackup完整備份中恢復一個InnoDB表


有時候我們從一個完整的數據庫備份中不需要整庫恢復,只需要恢復其中的一個或者幾個表,如果使用Percona的XtraBackup備份在恢復的時候如何只恢復部分數據呢?

下面用例子來演示

首先爲了能恢復表需要做以下操作:
1、InnoDB_FAST_SHUTDOWN = 0     --此參數MySQL在關閉的時候,需要完成所有的full purge和merge insert buffer操作.
2、InnoDB_File_Per_Table = ON   --此參數修改InnoDB爲獨立表空間模式,每個數據庫的每個表都會生成一個數據空間.

開始使用XtraBackup備份,這裏用到--export參數.
[mysql@localhost mysql]$ innobackupex-1.5.1 --defaults-file=/etc/my.cnf --export /mysql/backup/

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

140312 16:06:45  innobackupex-1.5.1: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_file=/etc/my.cnf;mysql_read_default_group=xtrabackup' (using password: NO).
140312 16:06:45  innobackupex-1.5.1: Connected to MySQL server
140312 16:06:45  innobackupex-1.5.1: Executing a version check against the server...
140312 16:06:51  innobackupex-1.5.1: Done.
IMPORTANT: Please check that the backup run completes successfully.
......
......
......

備份完成之後應用日誌使備份保持一致性
[mysql@localhost backup]$ innobackupex-1.5.1 --defaults-file=/etc/my.cnf --apply-log --export /mysql/backup/2014-03-12_16-06-52/

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

140312 16:11:47  innobackupex-1.5.1: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_file=/etc/my.cnf;mysql_read_default_group=xtrabackup' (using password: NO).
140312 16:11:47  innobackupex-1.5.1: Connected to MySQL server
Connected successfully
140312 16:11:47  innobackupex-1.5.1: Connection to database server closed
IMPORTANT: Please check that the apply-log run completes successfully.
          At the end of a successful apply-log run innobackupex-1.5.1
          prints "completed OK!".
......
......
......

現在我們登錄MySQL刪除一些t表的數據
mysql> select * from t;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
| 11 |
+----+
11 rows in set (0.06 sec)

mysql> delete from t where id between 7 and 10;
Query OK, 4 rows affected (0.18 sec)

mysql> select * from t;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
| 11 |
+----+
7 rows in set (0.00 sec)

這時我們Discard表
mysql> ALTER TABLE t DISCARD TABLESPACE;
Query OK, 0 rows affected (0.12 sec)

從備份目錄複製文件到數據庫目錄
[mysql@localhost test]$ cp -p -r t.cfg t.ibd ../../../data/test/

我們進入到MySQL之後Import表
mysql> ALTER TABLE t import TABLESPACE;
Query OK, 0 rows affected (0.07 sec)

查看錶t
mysql> select * from t;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
| 11 |
+----+
11 rows in set (0.00 sec)

注意:
在複製備份文件的時候一定要複製後綴cfg文件,否則在Import的時候就會報Warning.例如如下的信息:
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1810 | InnoDB: IO Read error: (2, No such file or directory) Error opening './test/t.cfg', will attempt to import without schema verification    |
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------------+
cfg文件的用處主要是在MySQL5.6執行"Flush Table xxx Export;"之後使.ibd文件保持一致性,同時這個文件會生成一個.cfg文件,在做Import的時候會對導入過程進行校驗,但是在MySQL5.6.8版本之後也不是必須要有.cfg文件.如果真沒有,在導入的時候有可能就會報出上面的錯誤,所以爲了安全還是複製它.

如果表有外鍵在Discard的時候執行如下命令:
set FOREIGN_KEY_CHECKS=0;

在Import表之後執行以下命令恢復外鍵檢查
set FOREIGN_KEY_CHECKS=1;

參數--export的英文解釋如下:

       This option is passed directly to xtrabackup's --export option. It
       enables exporting individual tables for import into another server.

OK了.我們的數據又回來了,今天先到此吧.^_^

原博客地址:http://blog.chinaunix.net/uid-10661836-id-4144677.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章