Home >Database >Mysql Tutorial >快速从mysqldump文件中恢复一张表_MySQL

快速从mysqldump文件中恢复一张表_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:30:07965browse

mysqldump

bitsCN.com

快速从mysqldump文件中恢复一张表

 

很多时候我们需要从mysqldump备份文件中恢复出一张表,通常的做法可能是先把sql 文件恢复到一个测试数据库,然后再使用mysqldump 导出一张表,再恢复到线上,这样,如果数据量不大这方法是可行的,但是你依然需要有一个测试机器或者临时创建一个库,不是很方便,下面为大家介绍的方法,避免了上述问题的困扰:

 

1,使用 awk

 

  可以先到原数据库中使用 'show tables;'  查看数据库表的列表,注意,此列表已经按照字母排序 比如,

 

table1

table2

table3

然后使用awk 来过滤sql语句,假设你要恢复 table2 表,可以使用下面的语句

 

 ]# awk ‘/^-- Table structure for table .table2./,/^-- Table structure for table .table3./{print}’ mydumpfile.sql > /tmp/recovered_table.sql

 

 

2,使用sed

 

 方法和上面相同

 

假设你要恢复 table2 表,可以使用下面的语句

 

cat mydumpfile.sql | sed -n -e '/Table structure for table .test1./,/Table structure for table .test2./p' > /tmp/extracted_table.sql

 

 

然后就可以将过滤出来的 .sql文件导入库中就完成了恢复。

 

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn