Created
October 11, 2013 15:30
-
-
Save sh2/6936754 to your computer and use it in GitHub Desktop.
Parallel restore script for mysqldump
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/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