Thursday, February 18, 2010

Backup and restore a single MySQL table

SkyHi @ Thursday, February 18, 2010
http://www.sysadmin.md/wp-content/uploads/mysql-backup-restore-table.png

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:


</p><pre><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 /></code><code>print if $printing;</code><br /><br /><code>

Invocation:

perl gettablefromdump.pl tablename dumpfile.sql


REFERENCE