Skip to content

Instantly share code, notes, and snippets.

@sh2
Created October 11, 2013 15:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sh2/6936754 to your computer and use it in GitHub Desktop.
Save sh2/6936754 to your computer and use it in GitHub Desktop.
Parallel restore script for mysqldump
#!/usr/bin/env perl
use strict;
use warnings;
use File::Temp qw/tempfile/;
my ($state, $nprocs) = (0, 0);
my ($fh, $tempfile, @settings);
open(my $pipe, '|-', qw/mysql -u root tpcc/) or die $!;
while (my $line = <STDIN>) {
if ($line =~ /^-- Table structure/) {
($fh, $tempfile) = tempfile();
$state = 1;
}
if ($state == 0) {
push @settings, $line;
print $pipe $line;
} elsif ($state == 1) {
print $fh $line;
}
if ($line =~ /^UNLOCK TABLES;/) {
close($fh);
$state = 0;
if (my $pid = fork()) {
$nprocs++;
if ($nprocs >= 3) {
wait();
$nprocs--;
}
} else {
open(my $pipe_child, '|-', qw/mysql -u root tpcc/) or die $!;
foreach my $setting (@settings) {
print $pipe_child $setting;
}
open(my $fh_child, '<', $tempfile) or die $!;
print "temp $tempfile\n";
while (my $line_child = <$fh_child>) {
print $pipe_child $line_child;
}
close($fh_child);
close($pipe_child);
unlink($tempfile);
exit(0);
}
}
}
close($pipe);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment