欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL使用全库备份数据恢复单表数据的方法

程序员文章站 2023-08-12 21:19:44
前言 备份数据库时,采用了全库备份,但是因为某些原因需要回滚一个表的数据到备份数据库上,如果回滚整个库就比较费时间,因为可能这个表只有几十m,但是其它表可能有十几上百g,...

前言

备份数据库时,采用了全库备份,但是因为某些原因需要回滚一个表的数据到备份数据库上,如果回滚整个库就比较费时间,因为可能这个表只有几十m,但是其它表可能有十几上百g,这时候就需要将需要恢复的表提取出来了

我们在实际工作中都遇到过这种情况,一个mysql实例中可能有多个database。而我们备份时,通常采用完全备份,将所有database都备份到一个文件中。

但是,偶尔会遇到只恢复一个database或者一个表的情况。怎么解决呢?

现在有备份库fdcsqlmysql-2018_11_30-03_00_01.sql,里面有多张表,现在需要恢复其中fdc_document这张表的数据

提取建表语句

sed -e '/./{h;$!d;}' -e 'x;/create table `表名`/!d;q' mysqldump.sql(备份文件的文件名)

sed -e '/./{h;$!d;}' -e 'x;/create table `fdc_document`/!d;q' fdcsqlmysql-2018_11_30-03_00_01.sql

drop table if exists `fdc_document`;
/*!40101 set @saved_cs_client  = @@character_set_client */;
/*!40101 set character_set_client = utf8 */;
create table `fdc_document` (
 `id` int(10) unsigned not null auto_increment comment '文档id',
 `uid` int(10) unsigned not null default '0' comment '用户id',
 `name` char(40) not null default '' comment '标识',
 ...
 ...
 ...
 `entrust_rule` tinyint(3) unsigned not null default '0' comment ' 经纪人点击是否和用户签委托协议:1为有;0为没有',
 `audit` tinyint(3) not null default '0' comment '审核:0为未审核;1为图片已审核;2为描述已审核;3为图片和描述都已审核',
 primary key (`id`),
 key `idx_area_house` (`partition`,`category_id`,`status`,`is_off`) using btree,
 key `idx_model_house` (`model_id`,`status`,`is_off`) using btree,
 key `idx_community_house` (`community_id`,`estate`,`status`,`is_off`) using btree,
 key `idx_uid_house` (`uid`,`model_id`,`is_off`) using btree,
 key `idx_pid_house` (`id`,`pid`,`status`,`is_off`) using btree,
 key `is_video` (`is_video`) using btree
) engine=innodb auto_increment=211138 default charset=utf8;
/*!40101 set character_set_client = @saved_cs_client */;

提取表数据

grep 'insert into表名' mysqldump.sql(备份文件的文件名) > table_data.sql

这里应该执行grep 'insert intofdc_document' fdcsqlmysql-2018_11_30-03_00_01.sql > document.sql

执行完后会得到文件document.sql,这就是需要的单独的表文件,就可以正常恢复表数据了

建库建表

先创建数据库,再根据上面的sql语句创建表fdc_document

导入表数据

mysql [document]> souce /data/backup/mysql/document.sql

ok,完工!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。