You are here

Mysql Dump Splitter

I saw that several people had already written things like this, but this is the one I wrote.

Some people love perl and some people hate it, but for certain things it works pretty damn well.

#!/usr/local/bin/perl
open(DUMP, "<freakishly_large_dump.sql") || die "can't open\n";
while () {
if(/^CREATE DATABASE.*/) {
if($db) {
close DB;
$db="";
}
($trash,$db,$trash) = split(/\`/);
open(DB, ">${db}.sql");
}
if($db) {
print DB;
}
}
close DUMP;

Comments

#!/usr/bin/perl -w
my $filename = '/usr/local/mysql/backups/primary/Mon/all-databases.sql';
my $db = "";
my $trash = "";

open(my $fh, ') {
  if($row =~ m/^CREATE DATABASE.*/) {
    if($db) {
      close DB;
      $db="";
    }
    ($trash,$db,$trash) = split /\`/, $row;
    open(DB, ">${db}.sql");
  }
  if($db) {
    print DB $row;
  }
}
close DUMP;