如今大多數同窗在線上採起的備份策略都是xtrabackup全備+binlog備份,那麼當某天某張表意外的刪除那麼如何從xtrabackup全備中恢復呢?從mysql 5.6版本開始,支持可移動表空間(Transportable Tablespace)那麼利用這個功能就能夠實現單表的恢復,一樣利用這個功能還能夠把innodb表移動到另一臺服務器上。能夠參考:https://yq.aliyun.com/articles/59271mysql
下面進行從xtrabackup全備恢復單表的測試。sql
1. 開啓了參數innodb_file_per_table安全
2. 安裝工具:mysql-utilities,其中mysqlfrm能夠讀取表結構。服務器
yum install mysql-utilities -y
查看原表中的數據:app
mysql> select * from yayun.t1; +------+------+ | id | name | +------+------+ | 1 | aa | | 2 | bb | | 3 | cc | | 4 | dd | +------+------+ 4 rows in set (0.00 sec) mysql>
執行備份:ide
innobackupex --defaults-file=/data/mysql/3306/my.cnf --user=root --password=123 --sock=/data/mysql/3306/mysqltmp/mysql.sock /data/
apply-log工具
innobackupex --defaults-file=/data/mysql/3306/my.cnf --apply-log /data/2017-03-22_16-13-00/
刪除t1表:測試
mysql> use yayun Database changed mysql> drop table t1; Query OK, 0 rows affected (0.13 sec) mysql>
讀取表結構spa
mysqlfrm --diagnostic /data/2017-03-22_16-13-00/yayun/t1.frm
輸出:rest
# Reading .frm file for /data/2017-03-22_16-13-00/yayun/t1.frm: # The .frm file is a TABLE. # CREATE TABLE Statement: CREATE TABLE `yayun`.`t1` ( `id` int(11) DEFAULT NULL, `name` char(180) DEFAULT NULL ) ENGINE=InnoDB; #...done.
建表:
mysql> use yayun Database changed mysql> CREATE TABLE `yayun`.`t1` ( -> `id` int(11) DEFAULT NULL, -> `name` char(180) DEFAULT NULL -> ) ENGINE=InnoDB; Query OK, 0 rows affected (0.08 sec) mysql>
加一個寫鎖,確保安全
mysql> lock tables t1 write; Query OK, 0 rows affected (0.00 sec) mysql>
丟棄表空間:
mysql> alter table t1 discard tablespace; Query OK, 0 rows affected (0.07 sec) mysql>
從備份中拷貝ibd文件,而且修改權限
[root@db_server_yayun_01 ~]# cp /data/2017-03-22_16-13-00/yayun/t1.ibd /data/mysql/3306/data/yayun/ [root@db_server_yayun_01 ~]# chown -R mysql.mysql /data/mysql/3306/data/yayun/t1.ibd
載入表空間:
mysql> alter table t1 import tablespace; Query OK, 0 rows affected, 1 warning (0.15 sec) mysql> show warnings; +---------+------+------------------------------------------------------------------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+------------------------------------------------------------------------------------------------------------------------------------------+ | Warning | 1810 | InnoDB: IO Read error: (2, No such file or directory) Error opening './yayun/t1.cfg', will attempt to import without schema verification | +---------+------+------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
這裏有警告,能夠忽略。詳情能夠看:https://yq.aliyun.com/articles/59271
查詢數據是否一致:
mysql> select * from t1; +------+------+ | id | name | +------+------+ | 1 | aa | | 2 | bb | | 3 | cc | | 4 | dd | +------+------+ 4 rows in set (0.00 sec) mysql>
最後解鎖:
mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) mysql>
參考文章: