Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Restore a single table from a large MySQL backup

Say, for some reason, you need to restore the entire contents of a single table from a HUGE mysqldump generated backup containing several tables. For example:

   1  
   2  create table `baz`;
   3  
   4  GIGS OF SQL YOU DON'T WANT;
   5  
   6  create table `foo`;
   7  
   8  A COUPLE THOUSAND LINES YOU DO WANT;
   9  
  10  create table `bar`;
  11  
  12  MORE SQL YOU DON'T WANT;


With a little dash 'o ruby, you can extract just the part you want:

   1  
   2  $ ruby -ne '@found=true if $_ =~ /^CREATE TABLE `foo`/i; next unless @found; exit if $_ =~ /^CREATE TABLE (?!`foo`)/i; puts $_;' giant_sql_dump.sql > foo.sql
   3  $ cat foo.sql
   4  create table `foo`;
   5  
   6  A COUPLE THOUSAND LINES YOU DO WANT;
   7  


You can then easily restore that entire table:

   1  
   2  $ mysql mydatabase -e 'drop table foo'
   3  $ mysql mydatabase < foo.sql
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS