optimize_mysql_tables-ND.sh: redesign in dependence to script 'optimize_mysql_tables.sh'.

This commit is contained in:
Christoph 2018-03-05 02:47:42 +01:00
parent 3016fb6f27
commit ab2421a717

View File

@ -1,11 +1,127 @@
#!/usr/bin/env bash
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/mysql_credetials.conf"
tmp_log_file="$(mktemp)"
# -------------
# - Variable settings
# -------------
DEFAULT_MYSQL_CREDENTIAL_ARGS="--login-path=local"
# -------------
# --- Some functions
# -------------
clean_up() {
# Perform program exit housekeeping
rm -f $tmp_log_file
exit $1
}
echononl(){
if $terminal ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
fi
}
fatal(){
echo ""
if $terminal ; then
if [[ -n "$*" ]] ; then
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
echo ""
echo -e " \033[31m\033[1mScript will be interrupted.\033[m\033[m"
else
echo -e " \033[31m\033[1mFatal error\033[m: \033[1mScript will be interrupted.\033[m"
fi
else
if [[ -n "$*" ]] ; then
echo " [ Fatal ]: $*"
echo ""
echo " Script was terminated.."
else
echo " Fatal error: Script was terminated.."
fi
fi
echo ""
clean_up 1
}
error(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
else
echo " [ Error ]: $*"
fi
echo ""
}
warn (){
if $terminal ; then
echo ""
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
else
echo " [ Warning ]: $*"
fi
}
info (){
if $terminal ; then
echo ""
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
else
echo " [ Info ]: $*"
fi
}
echo_ok() {
if $terminal ; then
echo -e "\033[80G[ \033[32mok\033[m ]"
fi
}
echo_failed(){
if $terminal ; then
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
fi
}
echo_skipped() {
if $terminal ; then
echo -e "\033[80G[ \033[37mskipped\033[m ]"
fi
}
trap clean_up SIGHUP SIGINT SIGTERM
# - Is this script running on terminal ?
# -
if [[ -t 1 ]] ; then
terminal=true
LOGGING=true
terminal=true
else
terminal=false
LOGGING=false
fi
mysql=`which mysql`
@ -14,132 +130,271 @@ if [ -z "$mysql" ]; then
if [ -x "/usr/local/mysql/bin/mysql" ]; then
mysql=/usr/local/mysql/bin/mysql
fi
fatal "No binary 'mysql' found!"
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=/var/run/mysqld/mysqld.sock --user=backup --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
# -------------
# - Load Settings from configuration file
# -------------
_service_extension=PRODUCTION.$$
for db in $DATABASES ; do
if [ "$db" = "nd" ]; then
mv /var/www/html/projekte/nd/htdocs /var/www/html/projekte/nd/htdocs.$_service_extension
ln -s htdocs503 /var/www/html/projekte/nd/htdocs
/usr/local/apache2/bin/apachectl graceful
elif [ "$db" = "nd_archiv" ]; then
mv /var/www/html/projekte/nd-archiv/htdocs /var/www/html/projekte/nd-archiv/htdocs.$_service_extension
ln -s htdocs503 /var/www/html/projekte/nd-archiv/htdocs
/usr/local/apache2/bin/apachectl graceful
fi
[ "$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"`
echo ""
echononl " Loading configuration settings from $(basename ${conf_file}).."
if [[ -f "$conf_file" ]]; then
source "$conf_file" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
TABLES=`$mysql -u$mysql_user -p$mysql_password $db -N -s -e "show tables"`
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_skipped
if $terminal ;then
warn "No Configuration File found. Loading defaults.."
fi
fi
[[ -z "$mysql_credential_args" ]] && mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then
[[ -z "$mysql_credential_args" ]] && mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
mysql_credential_args_arr="default:$mysql_credential_args"
fi
declare -i length_table_name
declare -i number_blank_signd
declare -i index_i
for _val in ${mysql_credential_args_arr[@]} ; do
_all_success=true
IFS=':' read -a _val_arr <<< "${_val}"
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
if $terminal ; then
echo ""
echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: optimize (and repair) tables of databases at host '$(hostname -f)'."
fi
for table in $TABLES ; do
DATABASES=`$mysql $mysql_credential_args -N -s -e "show databases"`
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
length_table_name=0
_service_extension=PRODUCTION.$$
for db in $DATABASES ; do
[ "$db" = "information_schema" ] && continue
[ "$db" = "performance_schema" ] && continue
[ "$db" = "mysql" ] && continue
if $terminal ;then
echo ""
echo -e " [$(date)] Optimize tables in database '${db}'.."
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\`"
_htdocs_nd_moved=false
_htdocs_nd_symlinked=false
_htdocs_nd_archiv_moved=false
_htdocs_nd_archiv_symlinked=false
if [[ "$db" = "nd" ]]; then
if [[ -d "/var/www/html/projekte/nd/htdocs" ]] ; then
if [[ -d "/var/www/html/projekte/nd/htdocs503" ]] ; then
mv /var/www/html/projekte/nd/htdocs /var/www/html/projekte/nd/htdocs.$_service_extension > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while moving '/var/www/html/projekte/nd/htdocs'.\n$(cat $tmp_log_file)"
else
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
_htdocs_nd_moved=true
fi
if $_htdocs_nd_moved ; then
ln -s htdocs503 /var/www/html/projekte/nd/htdocs > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while linking '/var/www/html/projekte/nd/htdocs' --> 'htdocs503'.\n$(cat $tmp_log_file)"
else
_htdocs_nd_symlinked=true
fi
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\`"
/usr/local/apache2/bin/apachectl graceful > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
fi
else
error "Directory '/var/www/html/projekte/nd/htdocs503' not found."
fi
else
error "Directory '/var/www/html/projekte/nd/htdocs503' not found."
fi
elif [[ "$db" = "nd_archiv" ]]; then
if [[ -d "/var/www/html/projekte/nd_archiv/htdocs" ]] ; then
if [[ -d "/var/www/html/projekte/nd/htdocs503" ]] ; then
mv /var/www/html/projekte/nd_archiv/htdocs /var/www/html/projekte/nd_archiv/htdocs.$_service_extension > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while moving '/var/www/html/projekte/nd_archiv/htdocs'.\n$(cat $tmp_log_file)"
else
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
_htdocs_nd_archiv_moved=true
fi
ln -s htdocs503 /var/www/html/projekte/nd/htdocs > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while linking '/var/www/html/projekte/nd_archiv/htdocs' --> 'htdocs503'.\n$(cat $tmp_log_file)"
else
_htdocs_nd_archiv_symlinked=true
fi
/usr/local/apache2/bin/apachectl graceful > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
fi
else
error "Directory '/var/www/html/projekte/nd_archiv/htdocs503' not found."
fi
else
error "Directory '/var/www/html/projekte/nd_archiv/htdocs503' not found."
fi
fi
TABLES=`$mysql $mysql_credential_args $db -N -s -e "show tables"`
for table in $TABLES ; do
if $terminal ; then
blank_signs=""
if [[ $length_table_name -gt ${#table} ]]; then
number_blank_sign=$(expr $length_table_name - ${#table})
index_i=0
while [[ $index_i -lt $number_blank_sign ]] ; do
blank_signs="$blank_signs "
(( index_i++ ))
done
echo -en "\033[1G"
fi
echo -en "\033[1G \033[32mOptimize table \033[1m$table\033[m$blank_signs\033[1G"
fi
length_table_name=${#table}
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
warn "Optimizing table \"${table}\" of database \"$db\" failed. Trying to repair.."
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
_all_success=false
error "Repairing table '$table' failed.\n$(cat "$tmp_log_file")"
error_messages_arr+=("MySQL $mysql_version: Error while repairing table '${table}' of database '$db'.")
else
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$tmp_log_file")"
error_messages_arr+=("MySQL $mysql_version: Error while (re-)optimizing table '${table}' of database '$db'.")
else
info "Reoptimizing table \"${table}\" of database \"$db\" was successfully."
fi
[[ $? -gt 0 ]] && echo -e "\t[ERROR]: Optimizing table \"${table}\" of database \"$db\" failed !!"
fi
echo ""
fi
done
if [[ "$db" = "nd" ]]; then
if $_htdocs_nd_symlinked ; then
rm /var/www/html/projekte/nd/htdocs > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while removing symlink '/var/www/html/projekte/nd/htdocs'.\n$(cat $tmp_log_file)"
fi
fi
if $_htdocs_nd_moved ; then
mv /var/www/html/projekte/nd/htdocs.$_service_extension /var/www/html/projekte/nd/htdocs > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while moving back '/var/www/html/projekte/nd/htdocs'.\n$(cat $tmp_log_file)"
fi
fi
/usr/local/apache2/bin/apachectl graceful > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
fi
elif [[ "$db" = "nd_archiv" ]]; then
if $_htdocs_nd_archiv_symlinked ; then
rm /var/www/html/projekte/nd-archiv/htdocs > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while removing symlink '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $tmp_log_file)"
fi
fi
if $_htdocs_nd_archiv_moved ; then
mv /var/www/html/projekte/nd-archiv/htdocs.$_service_extension /var/www/html/projekte/nd-archiv/htdocs > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Error while moving back '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $tmp_log_file)"
fi
fi
/usr/local/apache2/bin/apachectl graceful > $tmp_log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
fi
fi
if $terminal ;then
echo -en "\033[1G [$(date)] End optimize tables in database '${db}'.."
echo
fi
done
if $LOGGING ;then
echo -e "\n[`date`] End optimize tables in database $db.."
if $_all_success ; then
info_messages_arr+=("MySQL $mysql_version: The optimization of the MySQL tables of all databases was successful.")
fi
if [ "$db" = "nd" ]; then
rm -f /var/www/html/projekte/nd/htdocs
mv /var/www/html/projekte/nd/htdocs.$_service_extension /var/www/html/projekte/nd/htdocs
/usr/local/apache2/bin/apachectl graceful
elif [ "$db" = "nd_archiv" ]; then
rm -f /var/www/html/projekte/nd-archiv/htdocs
mv /var/www/html/projekte/nd-archiv/htdocs.$_service_extension /var/www/html/projekte/nd-archiv/htdocs
/usr/local/apache2/bin/apachectl graceful
if $terminal ; then
echo ""
echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: Finished optimizing MySQL databases at host $(hostname -f)."
echo ""
fi
done
exit 0
if [[ ${#info_messages_arr[@]} -gt 0 ]]; then
for msg in "${info_messages_arr[@]}" ; do
if $terminal ; then
info "$msg"
fi
done
fi
if [[ ${#error_messages_arr[@]} -gt 0 ]]; then
for msg in "${error_messages_arr[@]}" ; do
if $terminal ; then
error "$msg"
fi
done
fi
clean_up 0