现在大多数同学在线上采取的备份策略都是xtrabackup全备+binlog备份,那么当某天某张表意外的删除那么如何从xtrabackup全备中恢复呢?从mysql 5.6版本开始,支持可移动表空间(Transportable Tablespace)那么利用这个功能就可以实现单表的恢复,同样利用这个功能还可以把innodb表移动到另外一台服务器上。可以参考:https://yq.aliyun.com/articles/59271
下面进行从xtrabackup全备恢复单表的测试。
1. 开启了参数innodb_file_per_table
2. 安装工具:mysql-utilities,其中mysqlfrm可以读取表结构。
yum install mysql-utilities -y
查看原表中的数据:
mysql> select * from yayun.t1;
+------+------+
| id | name |
+------+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
+------+------+
4 rows in set (0.00 sec)
mysql>
执行备份:
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>
读取表结构
mysqlfrm --diagnostic /data/2017-03-22_16-13-00/yayun/t1.frm
输出:
# 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>
参考文章:
来源:oschina
链接:https://my.oschina.net/u/3023401/blog/2248171