Wednesday, December 9, 2009

Backup and restore a single MySQL table

SkyHi @ Wednesday, December 09, 2009


Dump a single table to a SQL file:

<code>mysqldump -uuser -ppassword dbName tableName > backup.sql</code>

If you want to get or restore a single table from a large MySQL dump file you can use the following methods:
Using AWK
Using Ruby
Using Perl:

<code>#!/usr/bin/perl -wn<br />BEGIN {<br />  $table = shift @ARGV;<br />  $printing = 0;<br />}<br />$printing = 1 if /^create table $table\b/io;<br />exit if $printing && /^create table (?!$table)\b/io;<br />print if $printing;</code>


<code>perl tablename dumpfile.sql<br /><br />Reference: <a href=""></a><br /></code>