#!/bin/bash
LOGFILE="/home/cmd_shell/mysql_repair.log"
AUTH='-uroot -ppassword'
DBNAME=$2
printUsage() {
echo "Usage: "
echo "$0 <option> <dbname>"
echo " --optimize/-O Optimize table"
echo " --repair/-R Repairs a possibly corrupted table"
echo " --help/-h This help text"
return
}
doAllTables() {
# get the table names
echo "[`date +%c`] GET THE TABLE NAME OF $DBNAME" >>$LOGFILE 2>&1
TABLENAMES=`mysql $AUTH -D $DBNAME -s -e "SHOW TABLES;"|grep -v 'Tables_in_'`
echo -ne "[`date +%c`] TABLE LIST:\n$TABLENAMES\n" >>$LOGFILE 2>&1
# loop through the tables and optimize them
echo "[`date +%c`] $DBCMD TABLE BEGIN" >>$LOGFILE 2>&1
for TABLENAME in $TABLENAMES
do
# echo "[`date +%c`] $DBCMD TABLE \`$TABLENAME\`;" >>$LOGFILE 2>&1
mysql $AUTH -D $DBNAME -e "$DBCMD TABLE \`$TABLENAME\`;" >>$LOGFILE 2>&1
done
echo "[`date +%c`] $DBCMD TABLE END" >>$LOGFILE 2>&1
}
if [ $# -lt 2 ] ; then
printUsage
exit 1
fi
case $1 in
--optimize | -O) DBCMD=OPTIMIZE; doAllTables;;
--repair | -R) DBCMD=REPAIR; doAllTables;;
--help | -h) printUsage; exit 1;;
*) printUsage; exit 1;;
esac
|