Last active
January 3, 2016 09:19
-
-
Save ilyaevseev/8442294 to your computer and use it in GitHub Desktop.
Split mysqldump to separate databases or tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
my $filenum = 0; | |
my $fd; | |
while(<>) { | |
if (/^-- Current Database: /) { | |
if (defined $fd) { | |
close $fd; | |
undef $fd; | |
} | |
} | |
if (not defined $fd) { | |
my $s = sprintf("%04d.sql", $filenum++); | |
open $fd, ">$s"; | |
print STDERR "$s...\n"; | |
} | |
print $fd $_; | |
} | |
close $fd if defined $fd; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
my $filenum = 0; | |
my $fd; | |
sub fdclose() { | |
return if !defined $fd; | |
close $fd; | |
undef $fd; | |
} | |
while(<>) { | |
if (not $_ or $_ eq "\n" or /^--/) { | |
fdclose(); | |
next; | |
} | |
if (not defined $fd) { | |
my $s = sprintf("%05d.sql", $filenum++); | |
open $fd, ">$s"; | |
print $fd "SELECT 'CHECKPOINT: $s';\n"; | |
print STDERR "$s...\n"; | |
} | |
print $fd $_; | |
} | |
fdclose(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment