数据库的备份与还原系列——单表备份和恢复详细完整实现
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,参考实现:https://www.percona.com/doc/percona-xtrabackup/LATEST/innobackupex/innobackupex_script.htmlRest
千家信息网最后更新 2026年11月01日数据库的备份与还原系列——单表备份和恢复详细完整实现
参考实现:
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.piddrwx------ 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.infodrwx--x--x 2 mysql mysql 4096 Feb 25 16:11 mysqlsrwxrwxrwx 1 mysql mysql 0 Feb 25 16:18 mysql.sockdrwx------ 2 mysql mysql 20 Feb 25 16:11 performance_schema-rw-rw---- 1 mysql mysql 24576 Feb 25 16:18 tc.logdrwxr-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 /backupsmkdir: 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: studentsCreate 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 namesYou can turn off this feature to get a quicker startup with -ADatabase changedMariaDB [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/ -htotal 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/ -htotal 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/ -htotal 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 |
数据
文件
备份
空间
结构
验证
只是
命令
数据备份
目录
而是
过程
关联
参考
变化
支持
数据库
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技行业发展
互联网金融公司科技
奇酷互联网科技
成都互联网科技公司
碳银互联网科技有限公司
狮子座互联网科技培训
互联网科技论坛
互联网 科技服务平台
青海省互联网新科技
浙江互联网科技研学营
互联网科技革命什么开始
搜狐科技互联网
迪庆互联网科技招生
朱啸虎曾投资哪些互联网科技企业
福建互联网展示科技馆设计
互联网科技手机排行榜
银川新华互联网科技学校是国营吗
互联网科技对化妆品行业
杭州科技互联网公司有哪些
北京瑞云互联网科技有限公司
科技互联网企业
互联网电商科技
圆梦互联网科技
撒么互联网科技公司
海南购全球互联网科技
中盈科技互联网信息有限公司
青海科技产业互联网发展
天津腾达互联网科技有限公司
四川慕妮娅互联网科技有限公司
梅州GM互联网络科技有限公司