Skip to content

Instantly share code, notes, and snippets.

@ilyaevseev
Last active January 3, 2016 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilyaevseev/8442294 to your computer and use it in GitHub Desktop.
Save ilyaevseev/8442294 to your computer and use it in GitHub Desktop.
Split mysqldump to separate databases or tables
#!/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;
#!/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