Xtrabackup 恢复Mariadb单个表

1、安装 mysql 备份工具 Percona XtraBackup

http://www.cndba.cn/leo1990/article/2488

2、恢复单个表说明

1). 针对InnoDB表恢复

2). 开启了参数innodb_file_per_table 此参数修改InnoDB为独立表空间模式,每个数据库的每个表都会生成一个数据空间

3、为备份建立一个只有备份权限的用户

MariaDB [(none)]> create user 'dbbackup'@'%' identified by '123456';
MariaDB [(none)]> grant reload,lock tables,replication client,create tablespace,process,super on *.* to dbbackup@'%' ;
MariaDB [(none)]> grant create,insert,select on percona_schema.* to dbbackup@'%' ;
MariaDB [(none)]> flush privileges;

4、创建一个测试,往里面插入数据后进行备份:

MariaDB [cndba]> create table test(id int);
Query OK, 0 rows affected (0.02 sec)

MariaDB [cndba]> insert into test values(10),(20),(30);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [cndba]> select * from tb1;
ERROR 1146 (42S02): Table 'cndba.tb1' doesn't exist
MariaDB [cndba]> select * from test;
+------+
| id   |
+------+
|   10 |
|   20 |
|   30 |
MariaDB [cndba]> show create table test/G
*************************** 1. row ***************************
       Table: test
Create Table: CREATE TABLE `test` (
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.03 sec)

如果忘记表结构可以安装工具:mysql-utilities,其中mysqlfrm可以读取表结构。

进行mysql-utilities安装:

yum install mysql-utilities -y

查看表结构

[root@www.cndba.cn yum.repos.d]# mysqlfrm --diagnostic /backup/2018-01-12_00-10-40/cndba/test.frm

5、xtrabackup备份数据库

[root@www.cndba.cn yum.repos.d]# innobackupex --host=localhost --user=dbbackup --password=123456 /backup/

6、准备apply-log

[root@www.cndba.cn backup]# innobackupex --apply-log --export /backup/2018-01-12_00-10-40/

7 、删除表

MariaDB [cndba]> drop table test;
Query OK, 0 rows affected (0.04 sec)

MariaDB [cndba]> show create table test/G
ERROR 1146 (42S02): Table 'cndba.test' doesn't exist

8、创建表

MariaDB [cndba]> CREATE TABLE `test` ( `id` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

9、丢弃表空间

MariaDB [cndba]> ALTER TABLE test DISCARD TABLESPACE;

10、拷贝备份目录中的ibd,cfg,exp 到mysql的datadir目录

[root@www.cndba.cn cndba]# cp /backup/2018-01-12_00-10-40/cndba/test.{ibd,exp,cfg} /data//mysql/cndba/
赋值权限
[root@www.cndba.cn cndba]# chown -R mysql.mysql /data/mysql/cndba/

11、导入表空间

MariaDB [cndba]> alter table test import tablespace;

12、表被还原

MariaDB [cndba]> select * from test;
+------+
| id   |
+------+
|   10 |
|   20 |
|   30 |
+------+