#! /usr/bin/perl # # Compare MASTER and SLAVE binlogs and if it's equal, PURGE old MASTER logs # # (c) 2008 by Kocmuk.ru use DBI; # Auth for mysql my $user="user"; my $passwd="pass"; # Put SLAVE server groups here @server_list=( ["server_a2"], ["server_b2"], ["server_c2","server_c3"] ); # #################################################################################################################### # my $debug = 0; if ( $ARGV[0] eq "--do" ) { $debug = 0; } else { print "Debug _read only_ mode: NOT realy purge anything.\n"; $debug = 1; } # foreach (@server_list) { my $master = ""; my $binlog = ""; my $error = 0; # print "\nPoll slave-servers in the same group:\n"; # foreach $server (@$_) { unless ($dbh = DBI->connect ("DBI:mysql::host=$server", $user, $passwd, {RaiseError=>0, PrintError=>1, AutoCommit => 1})) { print "Cann't connect to slave: slave=\"$server\"\n"; $error=1; last; } unless ($sth = $dbh->prepare("show slave status;")) { print "Cann't prepare to fetch slave status: slave=\"$server\"\n"; $error = 1; last; } unless ($sth->execute()) { print "Cann't exec fetch slave status: slave=\"$server\"\n"; $error = 1; last; } my @ary = $sth->fetchrow_array(); my $smaster = $ary[1]; my $sbinlog = $ary[9]; unless ($smaster or $sbinlog) { print "Abnormal slave status: slave=\"$server\" with master=\"$smaster\" and binlog=\"$sbinlog\"\n"; $error = 1; last; } print "Slave status: slave=\"$server\" with master=\"$smaster\" and binlog=\"$sbinlog\" \n"; if ($master eq "") { $master = $smaster; } elsif ($master ne $smaster) { print "Master name master=\"$smaster\" at slave=\"$server\" is different from the rest.\n"; $error = 1; last; } if ($binlog eq "") { $binlog = $sbinlog; } elsif ($binlog ne $sbinlog) { print "Binlog name binlog=\"$sbinlog\" at slave=\"$server\" is different from the rest.\n"; $error = 1; last; } } # if ($error == 1) { print "Slave-servers poll is failed, go to next group.\n"; next; } else { print "All slave-servers are consistent.\n"; } # unless ($dbh = DBI->connect ("DBI:mysql::host=$master", $user, $passwd, {RaiseError=>0, PrintError=>1, AutoCommit => 1})) { print "Cann't connect to master: master=\"$master\"\n"; print "Go to next group.\n"; next; } unless ($sth = $dbh->prepare("show master status;")) { print "Cann't prepare to fetch master status at: master=\"$master\"\n"; print "Go to next group.\n"; next; } unless ($sth->execute()) { print "Cann't exec fetch master status at: master=\"$master\"\n"; print "Go to next group.\n"; next; } my @ary=$sth->fetchrow_array(); my $masterbinlog = $ary[0]; unless ($masterbinlog) { print "Abnormal master=\"$master\" status: binlog=\"$masterbinlog\"\n"; print "Go to next group.\n"; next; } print "Master master=\"$master\" with: binlog=\"$masterbinlog\"\n"; if ($masterbinlog eq $binlog) { print "Master binlog=\"$masterbinlog\" and slave binlogs=\"$binlog\" are equal.\n"; if ($debug == 0 ) { print "PURGing MASTER LOGS TO '$masterbinlog' at $master...\n"; unless ($sth2 = $dbh->prepare("PURGE MASTER LOGS TO '$masterbinlog';")) { print "Cann't prepare \"PURGE MASTER LOGS TO '$masterbinlog'\" at master=\"$master\""; print "Go to next group.\n"; next; } unless ($sth2->execute()) { print "Cann't exec \"PURGE MASTER LOGS TO '$masterbinlog'\" at master=\"$master\""; print "Go to next group.\n"; next; } print "...done.\n\n"; } else { print "debug: \"PURGE MASTER LOGS TO '$masterbinlog'\" at master=\"$master\"\n"; } } else { print "WARNING! master-binlog: \"$masterbinlog\" and slaves-binlog: \"$binlog\" are NOT equal!\n"; print "Go to next group.\n"; next; } }