optimize_mysql_tables.sh, optimize_mysql_tables-ND.sh: some design changes..

This commit is contained in:
2023-08-09 11:09:11 +02:00
parent 30ca2c1a72
commit 5bdb646de7
2 changed files with 276 additions and 109 deletions

View File

@@ -5,16 +5,21 @@ working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/mysql_credetials.conf"
tmp_log_file="$(mktemp)"
# - Lock directory exists, until the script ends. So
# - we can check, if a previos instanze is already running.
# -
LOCK_DIR="/tmp/${script_name%%.*}.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log"
# -------------
# - Variable settings
# - Variable (default) settings
# -------------
DEFAULT_MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
DEFAULT_LOG_FILE="/var/log/${script_name%%.*}.log"
VERBOSE=false
DEFAULT_to_addresses="argus@oopen.de"
# -------------
# --- Some functions
@@ -64,7 +69,14 @@ usage() {
clean_up() {
# Perform program exit housekeeping
rm -f $tmp_log_file
if [[ ${1} -gt 0 ]] ; then
[[ -f "${log_file}" ]] && cp ${log_file} "/var/log/"
else
rm -f "/var/log/$(basename "${log_file}")"
fi
rm -rf "$LOCK_DIR"
blank_line
exit $1
}
@@ -159,6 +171,12 @@ trim() {
echo -n "$var"
}
blank_line() {
if $terminal ; then
echo ""
fi
}
detect_mysql_version () {
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
@@ -182,29 +200,16 @@ detect_mysql_version () {
}
trap clean_up SIGHUP SIGINT SIGTERM
# -------------
# - Is this script running on terminal ?
# -
# -------------
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
mysql=`which mysql`
if [ -z "$mysql" ]; then
if [ -x "/usr/local/mysql/bin/mysql" ]; then
mysql=/usr/local/mysql/bin/mysql
else
fatal "No binary 'mysql' found!"
fi
fi
# -------------
# - Read Commandline Parameters
@@ -231,6 +236,39 @@ if [[ "$(trim $*)" =~ " -h" ]] || [[ "$(trim $*)" =~ " --help" ]] ; then
fi
# -------------
# - Job is already running?
# -------------
## - If job already runs, stop execution..
## -
if mkdir "$LOCK_DIR" 2> /dev/null ; then
## - Remove lockdir when the script finishes, or when it receives a signal
trap "clean_up 1" SIGHUP SIGINT SIGTERM
else
datum=`date +"%d.%m.%Y"`
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\tExiting now.."
echo ""
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
echo ""
echo -e "\tExiting now.."
echo ""
for _to_address in $to_addresses ; do
echo -e "To:${_to_address}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
| sendmail -F "Error `hostname -f`" -f $from_address $_to_address
done
exit 1
fi
# -------------
# - Load Settings from configuration file
# -------------
@@ -241,17 +279,15 @@ else
GIVEN_DATABASE=""
fi
if $terminal ; then
echo ""
fi
blank_line
echononl " Loading configuration settings from $(basename ${conf_file}).."
if [[ -f "$conf_file" ]]; then
source "$conf_file" > $tmp_log_file 2>&1
source "$conf_file" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal "$(cat $tmp_log_file)"
fatal "$(cat $log_file)"
fi
else
echo_skipped
@@ -261,7 +297,23 @@ else
fi
#[[ -z "$mysql_credential_args" ]] && mysql_credential_args="$DEFAULT_MYSQL_CREDENTIAL_ARGS"
[[ -z "${to_addresses}" ]] && to_addresses="${DEFAULT_to_addresses}"
# -------------
# - Some default values
# -------------
mysql=`which mysql`
if [ -z "$mysql" ]; then
if [ -x "/usr/local/mysql/bin/mysql" ]; then
mysql=/usr/local/mysql/bin/mysql
else
fatal "No binary 'mysql' found!"
fi
fi
if [[ -z "$mysql_credential_args" ]]; then
@@ -299,12 +351,27 @@ if [[ -z "$mysql_credential_args" ]]; then
fi
fi
[[ -z "$log_file" ]] && log_file="$DEFAULT_LOG_FILE"
if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then
mysql_credential_args_arr[0]="default:$mysql_credential_args"
fi
# ==========
# - Begin Main Script
# ==========
# ----------
# - Headline
# ----------
if $terminal ; then
echo ""
echo -e "\033[1m----------\033[m"
echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m"
echo -e "\033[1m----------\033[m"
fi
declare -i length_table_name
declare -i number_blank_signd
declare -i index_i
@@ -392,7 +459,7 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
length_table_name=${#table}
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $tmp_log_file 2>&1
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then
@@ -403,27 +470,27 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
echo "" >> $log_file
echo " [$(date)] Repair table '$table'" >> $log_file
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > $tmp_log_file 2>&1
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then
_all_success=false
error "Repairing table '$table' failed.\n$(cat "$tmp_log_file")"
error "Repairing table '$table' failed.\n$(cat "$log_file")"
error_messages_arr+=("MySQL $mysql_version: Error while repairing table '${table}' of database '$db'.")
echo "" >> $log_file
echo -e " [$(date)] error: Repairing table '$table' failed of database \"$db\" failed..\n$(cat "$tmp_log_file")" >> $log_file
echo -e " [$(date)] error: Repairing table '$table' failed of database \"$db\" failed..\n$(cat "$log_file")" >> $log_file
echo "" >> $log_file
else
echo -e " [$(date)] Optimize table '$table'" >> $log_file
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $tmp_log_file 2>&1
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > $log_file 2>&1
if [[ $? -ne 0 ]]; then
error "Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$tmp_log_file")"
error "Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$log_file")"
error_messages_arr+=("MySQL $mysql_version: Error while (re-)optimizing table '${table}' of database '$db'.")
echo "" >> $log_file
echo -e " [$(date)] error: Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$tmp_log_file")" >> $log_file
echo -e " [$(date)] error: Reoptimizing table \"${table}\" of database \"$db\" failed.\n$(cat "$log_file")" >> $log_file
echo "" >> $log_file
else
info "Reoptimizing table \"${table}\" of database \"$db\" was successfully."