数据库的备份与还原系列——单表备份和恢复详细完整实现
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,参考实现:https://www.percona.com/doc/percona-xtrabackup/LATEST/innobackupex/innobackupex_script.htmlRest
千家信息网最后更新 2025年11月07日数据库的备份与还原系列——单表备份和恢复详细完整实现
参考实现:
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安全错误
数据库的锁怎样保障安全
pg数据库隔离级别
数据库两个数据表的数据分两列
网络安全设置蜜罐记录
德颐网络技术通化分公司
朗新科技瀚云工业互联网平台
golangdns服务器
消息中间件网络安全
mecache数据库
深圳市网络安全专业培训机构
安卓哪个平台下软件开发
边界网络安全方案
php数据库参数
票房数据库技术学院
写入数据库为乱码
跑网络安全的业务
数据库汉字显示问号
山西汾酒网络安全招标
计算机网络技术概述课堂提问
台式电脑服务器硬盘怎么装系统
网络安全法则有哪些行为禁止
2017网络安全演讲
剑网三更换服务器费用
逃离塔科夫俄罗斯选哪个服务器
国家网络安全战略基地
网络安全专项整治工作是落实
烟台软件开发技术
ps5 无法在限定时间与服务器连接
埃森哲软件开发薪水
远程修改mdb数据库
网络安全工程师月薪3万