Restore single MySQL table from large DB dump

Here’s how to restore one or more tables from a full database archive. It assumes you used mysqldump to save the database and stored it in a gz file. First create the file extract-table.pl:

#!/usr/bin/perl -wn
# extract-table.pl
BEGIN { $table = shift @ARGV }
print if /^create table $table\b/io .. /^create table (?!$table)\b/io;

Then do the following to restore the table:

zcat /path/mysqldump.date.gz | extract-table Mytable> ~/Mytable.dump
mysql  mydatabase -e "drop table Mytable"
mysql mydatabase

Advertisements

Comments are closed.

%d bloggers like this: