restore a single table from mysqldump

using mysqldump is a quick an easy way to backup databases, but restoring a single table (possibly to retrieve a single row) is a pain in the arse.

This article has a useful perl script which does just that

The bit that does the actual work is quite simple:

<snip>

125     ## go through the file one line at a time
126     while (my $line = <STDIN>) {

<snip>

140             ## set a flag when we encounter the table we want
141             if ($line =~ /^-- Table structure for table `$conf{'tableName'}`/) {
142                 $flag = 1;
143                 printmsg("Turning flag on", 1);
144             }
145             ## turn flag off as soon as we encounter next table definition
146             elsif ($line =~ /^-- Table structure for table/) {
147                 $flag = 0;
148                 printmsg("Turning flag off", 1);
149             }
150
151             ## if flag is set, then print to STDOUT, otherwise just move on
152             if ($flag) {
153                 print $line;
154             }

<snip>

156     }

Tags:

Leave a Reply

You must be logged in to post a comment.