参考实现:
目前成都创新互联公司已为数千家的企业提供了网站建设、域名、雅安服务器托管、网站托管、服务器租用、企业网站设计、北流网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
https://www.percona.com/doc/percona-xtrabackup/LATEST/innobackupex/innobackupex_script.html
Restoring Individual Tables #5.6之前是不支持的; In server versions prior to 5.6, it is not possible to copy tables between servers by copying the files, even with innodb_file_per_table. However, with the Percona XtraBackup, you can export individual tables from any InnoDB database, and import them into Percona Server with XtraDB or MySQL 5.6 (The source doesn’t have to be XtraDB or or MySQL 5.6, but the destination does). This only works on individual .ibd files, and cannot export a table that is not contained in its own .ibd file.
数据备份:
[root@centos7x ~]$systemctl start mariadb [root@centos7x ~]$mysql < hellodb_InnoDB.sql 默认的每个表一个文件; [root@centos7x ~]$ll /var/lib/mysql/ total 122936 -rw-rw---- 1 mysql mysql 16384 Feb 25 16:11 aria_log.00000001 -rw-rw---- 1 mysql mysql 52 Feb 25 16:11 aria_log_control -rw-rw---- 1 mysql mysql 5 Feb 25 16:18 centos7x.pid drwx------ 2 mysql mysql 272 Feb 25 16:18 hellodb -rw-rw---- 1 mysql mysql 2795 Feb 25 16:11 ib_buffer_pool -rw-rw---- 1 mysql mysql 12582912 Feb 25 16:18 ibdata1 -rw-rw---- 1 mysql mysql 50331648 Feb 25 16:18 ib_logfile0 -rw-rw---- 1 mysql mysql 50331648 Feb 25 16:11 ib_logfile1 -rw-rw---- 1 mysql mysql 12582912 Feb 25 16:18 ibtmp1 -rw-rw---- 1 mysql mysql 0 Feb 25 16:18 multi-master.info drwx--x--x 2 mysql mysql 4096 Feb 25 16:11 mysql srwxrwxrwx 1 mysql mysql 0 Feb 25 16:18 mysql.sock drwx------ 2 mysql mysql 20 Feb 25 16:11 performance_schema -rw-rw---- 1 mysql mysql 24576 Feb 25 16:18 tc.log drwxr-xr-x 2 mysql mysql 6 Feb 25 16:11 test [root@centos7x ~]$ll /var/lib/mysql/hellodb/ total 704 -rw-rw---- 1 mysql mysql 1277 Feb 25 16:18 classes.frm -rw-rw---- 1 mysql mysql 98304 Feb 25 16:18 classes.ibd -rw-rw---- 1 mysql mysql 976 Feb 25 16:18 coc.frm -rw-rw---- 1 mysql mysql 98304 Feb 25 16:18 coc.ibd -rw-rw---- 1 mysql mysql 1251 Feb 25 16:18 courses.frm -rw-rw---- 1 mysql mysql 98304 Feb 25 16:18 courses.ibd -rw-rw---- 1 mysql mysql 61 Feb 25 16:18 db.opt -rw-rw---- 1 mysql mysql 1001 Feb 25 16:18 scores.frm -rw-rw---- 1 mysql mysql 98304 Feb 25 16:18 scores.ibd -rw-rw---- 1 mysql mysql 1208 Feb 25 16:18 students.frm -rw-rw---- 1 mysql mysql 98304 Feb 25 16:18 students.ibd -rw-rw---- 1 mysql mysql 1298 Feb 25 16:18 teachers.frm -rw-rw---- 1 mysql mysql 98304 Feb 25 16:18 teachers.ibd -rw-rw---- 1 mysql mysql 973 Feb 25 16:18 toc.frm -rw-rw---- 1 mysql mysql 98304 Feb 25 16:18 toc.ibd [root@centos7x ~]$mkdir -pv /backups mkdir: created directory ‘/backups’ [root@centos7x ~]$innobackupex --include='hellodb.students' /backups/ 这只是备份了数据了; [root@centos7x ~]$ll /backups/2018-02-25_16-23-08/hellodb/ total 100 -rw-r----- 1 root root 1208 Feb 25 16:23 students.frm -rw-r----- 1 root root 98304 Feb 25 16:23 students.ibd
备份表定义:
所以需要将表定义也导出来;否则将来的恢复过程是需要创建表定义的; [root@centos7x ~]$mysql -e 'show create table hellodb.students\G;' *************************** 1. row *************************** Table: students Create Table: CREATE TABLE `students` ( `StuID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Name` varchar(50) NOT NULL, `Age` tinyint(3) unsigned NOT NULL, `Gender` enum('F','M') NOT NULL, `ClassID` tinyint(3) unsigned DEFAULT NULL, `TeacherID` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`StuID`) ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8
表破坏操作:
进行表的破坏; [root@centos7x ~]$mysql -e 'drop table hellodb.students;' MariaDB [hellodb]> show tables; +-------------------+ | Tables_in_hellodb | +-------------------+ | classes | | coc | | courses | | scores | | teachers | | toc | +-------------------+ 6 rows in set (0.00 sec)
数据和表定义恢复操作:
恢复操作: 先整理; [root@centos7x ~]$innobackupex --apply-log --export /backups/2018-02-25_16-23-08/ 整理、导出数据的前后变化; [root@centos7x ~]$ll /backups/2018-02-25_16-23-08/hellodb/ -rw-r----- 1 root root 1208 Feb 25 16:23 students.frm -rw-r----- 1 root root 98304 Feb 25 16:23 students.ibd [root@centos7x ~]$ll /backups/2018-02-25_16-23-08/hellodb/ total 120 -rw-r--r-- 1 root root 640 Feb 25 16:49 students.cfg -rw-r----- 1 root root 16384 Feb 25 16:49 students.exp -rw-r----- 1 root root 1208 Feb 25 16:23 students.frm -rw-r----- 1 root root 98304 Feb 25 16:23 students.ibd 恢复表之前,先创建表结构; MariaDB [(none)]> use hellodb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [hellodb]> CREATE TABLE `students` ( -> `StuID` int(10) unsigned NOT NULL AUTO_INCREMENT, -> `Name` varchar(50) NOT NULL, -> `Age` tinyint(3) unsigned NOT NULL, -> `Gender` enum('F','M') NOT NULL, -> `ClassID` tinyint(3) unsigned DEFAULT NULL, -> `TeacherID` int(10) unsigned DEFAULT NULL, -> PRIMARY KEY (`StuID`) -> ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.01 sec) MariaDB [hellodb]> select * from students; Empty set (0.00 sec) MariaDB [hellodb]> desc students; +-----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------------------+------+-----+---------+----------------+ | StuID | int(10) unsigned | NO | PRI | NULL | auto_increment | | Name | varchar(50) | NO | | NULL | | | Age | tinyint(3) unsigned | NO | | NULL | | | Gender | enum('F','M') | NO | | NULL | | | ClassID | tinyint(3) unsigned | YES | | NULL | | | TeacherID | int(10) unsigned | YES | | NULL | | +-----------+---------------------+------+-----+---------+----------------+ 6 rows in set (0.00 sec) 此时虽然有表结构和数据文件,但是没有数据; [root@centos7x ~]$ll /var/lib/mysql/hellodb/ -h total 704K -rw-rw---- 1 mysql mysql 1.3K Feb 25 16:18 classes.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 classes.ibd -rw-rw---- 1 mysql mysql 976 Feb 25 16:18 coc.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 coc.ibd -rw-rw---- 1 mysql mysql 1.3K Feb 25 16:18 courses.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 courses.ibd -rw-rw---- 1 mysql mysql 61 Feb 25 16:18 db.opt -rw-rw---- 1 mysql mysql 1001 Feb 25 16:18 scores.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 scores.ibd -rw-rw---- 1 mysql mysql 1.2K Feb 25 16:53 students.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:53 students.ibd -rw-rw---- 1 mysql mysql 1.3K Feb 25 16:18 teachers.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 teachers.ibd -rw-rw---- 1 mysql mysql 973 Feb 25 16:18 toc.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 toc.ibd 于是要删除这个表空间文件,即数据文件;但是不要使用rm删除,而是使用命令删除; MariaDB [hellodb]> alter table students discard tablespace; Query OK, 0 rows affected (0.00 sec) [root@centos7x ~]$ll /var/lib/mysql/hellodb/ -h total 608K -rw-rw---- 1 mysql mysql 1.3K Feb 25 16:18 classes.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 classes.ibd -rw-rw---- 1 mysql mysql 976 Feb 25 16:18 coc.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 coc.ibd -rw-rw---- 1 mysql mysql 1.3K Feb 25 16:18 courses.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 courses.ibd -rw-rw---- 1 mysql mysql 61 Feb 25 16:18 db.opt -rw-rw---- 1 mysql mysql 1001 Feb 25 16:18 scores.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 scores.ibd -rw-rw---- 1 mysql mysql 1.2K Feb 25 16:53 students.frm -rw-rw---- 1 mysql mysql 1.3K Feb 25 16:18 teachers.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 teachers.ibd -rw-rw---- 1 mysql mysql 973 Feb 25 16:18 toc.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 toc.ibd [root@centos7x ~]$cp /backups/2018-02-25_16-23-08/hellodb/students.{cfg,ibd,exp} /var/lib/mysql/hellodb/ [root@centos7x ~]$ll /var/lib/mysql/hellodb/ -h total 724K -rw-rw---- 1 mysql mysql 1.3K Feb 25 16:18 classes.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 classes.ibd -rw-rw---- 1 mysql mysql 976 Feb 25 16:18 coc.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 coc.ibd -rw-rw---- 1 mysql mysql 1.3K Feb 25 16:18 courses.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 courses.ibd -rw-rw---- 1 mysql mysql 61 Feb 25 16:18 db.opt -rw-rw---- 1 mysql mysql 1001 Feb 25 16:18 scores.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 scores.ibd -rw-r--r-- 1 root root 640 Feb 25 16:59 students.cfg -rw-r----- 1 root root 16K Feb 25 16:59 students.exp -rw-rw---- 1 mysql mysql 1.2K Feb 25 16:53 students.frm -rw-r----- 1 root root 96K Feb 25 16:59 students.ibd -rw-rw---- 1 mysql mysql 1.3K Feb 25 16:18 teachers.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 teachers.ibd -rw-rw---- 1 mysql mysql 973 Feb 25 16:18 toc.frm -rw-rw---- 1 mysql mysql 96K Feb 25 16:18 toc.ibd [root@centos7x ~]$chown -R mysql.mysql /var/lib/mysql/hellodb/ 接着是导入表空间,尽管文件放在数据目录下了,但是表空间还没有关联; MariaDB [hellodb]> alter table students import tablespace; Query OK, 0 rows affected (0.02 sec)
数据和表结构验证操作:
验证; MariaDB [hellodb]> select * from students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 |