Wednesday, December 9, 2009

Backup and restore a single MySQL table

SkyHi @ Wednesday, December 09, 2009

mysql-backup-restore-table

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>

Invocation:

<code>perl gettablefromdump.pl tablename dumpfile.sql<br /><br />Reference: <a href="http://www.sysadmin.md/backup-and-restore-a-single-mysql-table.html">http://www.sysadmin.md/backup-and-restore-a-single-mysql-table.html</a><br /></code>