#!/bin/bash
# (c) 2011 Marco FONTANI - MFONTANI@cpan.org
# Optimize all tables on the current server's MySQL instance
# Possibly to be run nightly via cron, or by hand as requested
MYSQL_USERNAME=root
MYSQL_PASSWORD=CHANGEME
MYSQL_INVOKE="mysql -u $MYSQL_USERNAME -p$MYSQL_PASSWORD --skip-column-names -Be"
for database in $($MYSQL_INVOKE 'show databases' | grep -v information_schema); do
    for table in $($MYSQL_INVOKE "use $database;show tables;"); do
        $MYSQL_INVOKE "use $database; optimize table $table" >/dev/null
    done
done
