141 lines
4.5 KiB
Bash
Executable File
141 lines
4.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# - 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
|
|
|
|
# - MySQL / MariaDB credentials
|
|
# -
|
|
# - Giving password on command line is insecure an sind mysql 5.5
|
|
# - you will get a warning doing so.
|
|
# -
|
|
# - Reading username/password fro file ist also possible, using MySQL/MariaDB
|
|
# - commandline parameter '--defaults-file'.
|
|
# -
|
|
# - Since Version 5.6, that method is considered as insecure.
|
|
# - To avoid giving the password on command line, we use an
|
|
# - encrypted option file
|
|
# -
|
|
# - Create (encrypted) option file:
|
|
# - $ mysql_config_editor set --login-path=local --socket=/tmp/mysql.sock --user=root --password
|
|
# - $ Password:
|
|
# -
|
|
# - Use of option file:
|
|
# - $ mysql --login-path=local ...
|
|
# -
|
|
# - Example
|
|
# - mysql_credential_args="--login-path=local"
|
|
# - mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf" (Debian default)
|
|
# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
|
|
# -
|
|
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
|