Created
October 27, 2010 02:45
-
-
Save skreuzer/648322 to your computer and use it in GitHub Desktop.
Purge MySQL Binary Logs
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/perl | |
use strict; | |
use DBI; | |
use Getopt::Std; | |
sub purge_logs { | |
my $db_ip = shift; | |
my $db_name = shift; | |
my $db_user = shift; | |
my $db_pass = shift; | |
my $sth; | |
my $db; | |
my $query; | |
my @logs; | |
my $log; | |
my $i; | |
$db = DBI->connect("DBI:mysql:$db_name:$db_ip", $db_user, $db_pass); | |
if($db) { | |
$query = "SHOW MASTER LOGS"; | |
$sth = $db->prepare($query) ; | |
if($sth->execute()) { | |
while(my ($log) = $sth->fetchrow_array()) { | |
push(@logs, $log); | |
} | |
if(@logs > 3) { | |
$i = (@logs - 3); | |
if(exists($logs[$i])) { | |
$sth->finish(); | |
$query = "PURGE MASTER LOGS TO '$logs[$i]' "; | |
$sth = $DB->prepare( $query ) ; | |
if(!$sth->execute()) { | |
print("Error cannot execute $query\n"); | |
} | |
} | |
} | |
} else { | |
print "Failed to execute: $query \n"; | |
} | |
@logs = (); | |
$sth->finish(); | |
$db->disconnect() ; | |
} else { | |
print "Cannot connect to $db_ip\n"; | |
} | |
} | |
MAIN: { | |
purge_logs("localhost", "mysql", "root", "asdfqwer"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment