MySQL and more

Tuesday, November 24, 2009

Restore a Single Table From mysqldump

Ok, this is inspired by another post on the same subject. I would have just commented, but there was a required signup. Ew.


While the method there with juggling grants works fine, I'd like to highlight another way that I think is much simpler: use grep & sed to create a file which contains only the table you want to restore.


mysql> show tables;
+------------------+
| Tables_in_gtowey |
+------------------+
| t1 |
| t2 |
| t3 |
+------------------+
3 rows in set (0.00 sec)


# mysqldump gtowey > dump.sql

Given 3 tables in a mysqldump file, lets restore just t2


# grep -n 'Table structure' dump.sql
19:-- Table structure for table `t1`
40:-- Table structure for table `t2`
61:-- Table structure for table `t3`


Now just use the line numbers to extract the table with sed:

sed -n '40,61 p' dump.sql > t2.sql

That's it, you have everything you need with no cleanup of mysql grants to do afterward.

9 comments:

Roland Bouman said...

Hi!

nice trick, thanks!

John Dzilvelis said...

I used the perl script posted here and found that it works very well:

http://blog.tsheets.com/2008/tips-tricks/extract-a-single-table-from-a-mysqldump-file.html

Lachlan said...

Hey that is pretty nifty - Thanks for sharing!

Prabhat Kumar said...

Good.
There is another way I used. for database.

http://adminlinux.blogspot.com/2009/11/extract-single-dbtable-from-dump-file.html

Dali said...

thanks, saved my time

The Leading Wedge said...

Very. Thanks!

ale8oneboy said...

I've developed a PHP script that allow users to browse dump files and extract single tables. http://mattkendrick.com/development/new-tool-mysqldump-browser/

Rieds said...

I always revisit this post when I need to do perform such a tast... very slick.

yuan fang said...

briliant

Followers