Ruslan Bondar Site: RuslanBondar/projects/scripts/mysqlrepair

Automatic My Sql Repair of crash tables

This script may run from cron

* * * * * /scripts/script.pl

#!/usr/bin/perl -w

# autor: Ruslan Bondar
# date: 2007-01-20
use strict;
use DBI;
use Sys::Hostname;

my $host = 'localhost';
my $user = 'root';
my $pass = '*****';

sub trim {
    my $s = shift;
    for ($s) { s/^\s+//; s/\s+$//; }
    return $s;
}
my $dbh = DBI->connect("dbi:mysql:host=$host", $user, $pass) or die("MySql connect error");
my $sth = $dbh->prepare('select TABLE_SCHEMA, TABLE_name from information_schema.tables'.
                 ' where ROW_FORMAT is NULL AND table_type <> 'VIEW'');
                or die "Couldn't prepare statement: " . $dbh->errstr;

$sth->execute();
while (my @data = $sth->fetchrow_array()) {
    print('Try to repair '.$data[1].'.'.$data[2]."\n");
    my $ste = $dbh->prepare('repair table '.$data[1].'.'.$data[2])
                or die "Couldn't prepare statement: " . $dbh->errstr;
    $ste->execute();
}