Created
September 20, 2011 07:58
-
-
Save kazeburo/1228602 to your computer and use it in GitHub Desktop.
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 FindBin; | |
| use lib "$FindBin::Bin/extlib/lib/perl5"; | |
| use DBI; | |
| $|=1; | |
| my @databases = @ARGV; | |
| if ( !@databases ) { | |
| warn "$0 [database,[database]...]\n"; | |
| exit(1); | |
| } | |
| my @mysqldump = ('/usr/bin/mysqldump','/usr/local/bin/mysqldump','/usr/local/mysql/bin/mysqldump'); | |
| my $mysqldump; | |
| for ( @mysqldump ) { | |
| if ( -x $_ ) { | |
| $mysqldump = $_; | |
| last; | |
| } | |
| } | |
| die "couldnot find mysqldump" unless $mysqldump; | |
| my $dbh = DBI->connect_cached('dbi:mysql:;host=localhost','root','',{ | |
| RaiseError => 1, | |
| PrintError => 0, | |
| ShowErrorStatement => 1, | |
| }); | |
| $dbh->do('FLUSH TABLES WITH READ LOCK'); | |
| my $sth = $dbh->prepare('SHOW MASTER STATUS'); | |
| $sth->execute; | |
| my $master_status = $sth->fetchrow_hashref('NAME'); | |
| print "-- mysql40dump.pl start " . localtime . "\n"; | |
| printf "-- CHANGE MASTER TO MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s;\n", $master_status->{File}, $master_status->{Position}; | |
| print "set FOREIGN_KEY_CHECKS=0;\n"; | |
| pipe my $logrh, my $logwh | |
| or die "failed to create pipe:$!"; | |
| my $pid = fork; | |
| if ( ! defined $pid ) { | |
| die "fork failed: $!"; | |
| } | |
| elsif ( $pid == 0 ) { | |
| #child | |
| $dbh->STORE(InactiveDestroy => 1); | |
| close $logrh; | |
| open STDOUT, '>&', $logwh | |
| or die "failed to redirect STDOUT"; | |
| close $logwh; | |
| exec( | |
| $mysqldump, | |
| '--quick', | |
| '--add-locks', | |
| '--extended-insert', | |
| '--single-transaction', | |
| '--databases', | |
| @databases | |
| ); | |
| die "exec failed: $!"; | |
| } | |
| #parent | |
| close $logwh; | |
| my $unlock=0; | |
| while(<$logrh>){ | |
| print; | |
| if ( $unlock == 0 && m!^CREATE DATABASE!) { | |
| $dbh->do('UNLOCK TABLE'); | |
| $unlock++; | |
| } | |
| } | |
| close $logrh; | |
| while (wait == -1) {} | |
| my $exit_code = $?; | |
| exit($exit_code >> 8); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment