131 lines
4.0 KiB
Bash
Executable File
131 lines
4.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
working_dir="$(dirname $(realpath $0))"
|
|
conf_file="${working_dir}/conf/optimize_mysql_tables.conf"
|
|
|
|
# - Is this script running on terminal ?
|
|
# -
|
|
if [[ -t 1 ]] ; then
|
|
terminal=true
|
|
LOGGING=true
|
|
else
|
|
terminal=false
|
|
LOGGING=false
|
|
fi
|
|
|
|
mysql=`which mysql`
|
|
|
|
if [ -z "$mysql" ]; then
|
|
if [ -x "/usr/local/mysql/bin/mysql" ]; then
|
|
mysql=/usr/local/mysql/bin/mysql
|
|
fi
|
|
fi
|
|
|
|
|
|
#---------------------------------------
|
|
#-----------------------------
|
|
# Read Configurations from $conf_file
|
|
#-----------------------------
|
|
#---------------------------------------
|
|
|
|
if [[ -f "$conf_file" ]]; then
|
|
source "$conf_file"
|
|
fi
|
|
|
|
[[ -z "$mysql_credential_args" ]] && mysql_credential_args="--login-path=local"
|
|
|
|
|
|
if [ -n "$mysql_credential_args" ] ; then
|
|
DATABASES=`$mysql $mysql_credential_args -N -s -e "show databases"`
|
|
else
|
|
DATABASES=`$mysql -u$mysql_user -p$mysql_password -N -s -e "show databases"`
|
|
fi
|
|
|
|
if $terminal ; then
|
|
echo ""
|
|
echo "MySQL: optimize (and try to repair) tables of databases at host $(hostname -f)."
|
|
echo ""
|
|
fi
|
|
|
|
|
|
|
|
for db in $DATABASES ; do
|
|
|
|
|
|
[ "$db" = "information_schema" ] && continue
|
|
[ "$db" = "performance_schema" ] && continue
|
|
[ "$db" = "mysql" ] && continue
|
|
|
|
if $LOGGING ;then
|
|
echo -e "\n[`date`] Optimize tables in database $db.."
|
|
fi
|
|
|
|
if [ -n "$mysql_credential_args" ] ; then
|
|
TABLES=`$mysql $mysql_credential_args $db -N -s -e "show tables"`
|
|
else
|
|
TABLES=`$mysql -u$mysql_user -p$mysql_password $db -N -s -e "show tables"`
|
|
fi
|
|
|
|
for table in $TABLES ; do
|
|
|
|
if [ -n "$mysql_credential_args" ] ; then
|
|
if $LOGGING ;then
|
|
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`"
|
|
else
|
|
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
|
|
fi
|
|
else
|
|
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
|
|
fi
|
|
if [ "$?" != "0" ]; then
|
|
echo -e "\t[ Warning ]: Optimizing table \"${table}\" of database \"$db\" failed. Trying to repair.."
|
|
if [ -n "$mysql_credential_args" ] ; then
|
|
if $LOGGING ;then
|
|
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`"
|
|
else
|
|
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > /dev/null 2>&1
|
|
fi
|
|
if [ "$?" != "0" ]; then
|
|
echo -e "\t[ERROR]: Reparing table \"${table}\" of database \"$db\" failed !!"
|
|
else
|
|
if $LOGGING ;then
|
|
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`"
|
|
else
|
|
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
|
|
fi
|
|
[[ $? -gt 0 ]] && echo -e "\t[ERROR]: Optimizing table \"${table}\" of database \"$db\" failed !!"
|
|
fi
|
|
else
|
|
if $LOGGING ;then
|
|
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "REPAIR TABLE \`$table\`"
|
|
else
|
|
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "REPAIR TABLE \`$table\`" > /dev/null 2>&1
|
|
fi
|
|
if [ "$?" != "0" ]; then
|
|
echo -e "\t[ERROR]: Reparing table \"${table}\" of database \"$db\" failed !!"
|
|
else
|
|
if $LOGGING ;then
|
|
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "OPTIMIZE TABLE \`$table\`"
|
|
else
|
|
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
|
|
fi
|
|
[[ $? -gt 0 ]] && echo -e "\t[ERROR]: Optimizing table \"${table}\" of database \"$db\" failed !!"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
done
|
|
if $LOGGING ;then
|
|
echo -e "\n[`date`] End optimize tables in database $db.."
|
|
fi
|
|
|
|
done
|
|
|
|
if $terminal ; then
|
|
echo ""
|
|
echo "Finished optimizing MySQL databases at host $(hostname -f)."
|
|
echo ""
|
|
fi
|
|
|
|
exit 0
|