有时需从mysqldump备份文件恢复一张表的数据,通常有两种做法:

1):将整个文件导入测试server中,再mysqldump导出需要的表,再导入线上server。

如果库的量很少,这样倒是也不慢,可当库的量有一定的级别了,就会很慢。很可能备份时使用了压缩,再需要解压缩的步骤。

2):通过mysql>show tables;查看到mysqldump备份时的表的备份顺序,其和show tables的看的顺序一样的。锁定了表的位置,通过awk或sed取出其需要的表数据。

针对方法2:

假如你show tables的结果为:

table_1

table_2

table_3

.......

table_n

此时想恢复table_10的数据时,使用awk做如下操作:

zcat mysqldump.date.sql.gz|awk '/^-- Table structure for table `table_10`/,/^-- Table structure for table `table_11`/{print}' >/tmp/table_10.sql

注意:awk的''和""的区别,假如""中使用``就会报错;其中``可以替换为..。如下

zcat mysqldump.date.sql.gz|awk "/^-- Table structure for table .table_10./,/^-- Table structure for table .table_11./{print}">/tmp/table_10.sql

使用sed作如下操作:

zcat mysqldump.date.sql.gz |sed -n -e '/-- Table structure for table `table_10`/,/-- Table structure for table `table_11`/p' >/tmp/table_10.sql

其实方法2就是要知道,show tables的表顺序和mysqldump时的顺序相同;其次是dump出的文件特殊标志-- Table structure for table `table_n` 。