optimize_mysql_tables.sh, optimize_mysql_tables-ND.sh: some design changes..
This commit is contained in:
parent
30ca2c1a72
commit
5bdb646de7
@ -5,16 +5,21 @@ working_dir="$(dirname $(realpath $0))"
|
|||||||
|
|
||||||
conf_file="${working_dir}/conf/mysql_credetials.conf"
|
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_MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
|
||||||
DEFAULT_LOG_FILE="/var/log/${script_name%%.*}.log"
|
|
||||||
VERBOSE=false
|
VERBOSE=false
|
||||||
|
|
||||||
|
DEFAULT_to_addresses="argus@oopen.de"
|
||||||
|
|
||||||
|
|
||||||
# -------------
|
# -------------
|
||||||
# --- Some functions
|
# --- Some functions
|
||||||
@ -64,7 +69,14 @@ usage() {
|
|||||||
clean_up() {
|
clean_up() {
|
||||||
|
|
||||||
# Perform program exit housekeeping
|
# 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
|
exit $1
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,6 +171,12 @@ trim() {
|
|||||||
echo -n "$var"
|
echo -n "$var"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
blank_line() {
|
||||||
|
if $terminal ; then
|
||||||
|
echo ""
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
detect_mysql_version () {
|
detect_mysql_version () {
|
||||||
|
|
||||||
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
|
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
|
||||||
@ -182,28 +200,16 @@ detect_mysql_version () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# -------------
|
||||||
trap clean_up SIGHUP SIGINT SIGTERM
|
|
||||||
|
|
||||||
|
|
||||||
# - Is this script running on terminal ?
|
# - Is this script running on terminal ?
|
||||||
# -
|
# -------------
|
||||||
|
|
||||||
if [[ -t 1 ]] ; then
|
if [[ -t 1 ]] ; then
|
||||||
terminal=true
|
terminal=true
|
||||||
else
|
else
|
||||||
terminal=false
|
terminal=false
|
||||||
fi
|
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
|
# - Read Commandline Parameters
|
||||||
@ -230,27 +236,58 @@ if [[ "$(trim $*)" =~ " -h" ]] || [[ "$(trim $*)" =~ " --help" ]] ; then
|
|||||||
fi
|
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
|
# - Load Settings from configuration file
|
||||||
# -------------
|
# -------------
|
||||||
|
|
||||||
if [[ -n "$1 " ]] ; then
|
if [[ -n "$1 " ]] ; then
|
||||||
DATABASES="$1"
|
GIVEN_DATABASE="$1"
|
||||||
else
|
else
|
||||||
DATABASES=""
|
GIVEN_DATABASE=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if $terminal ; then
|
blank_line
|
||||||
echo ""
|
|
||||||
fi
|
|
||||||
echononl " Loading configuration settings from $(basename ${conf_file}).."
|
echononl " Loading configuration settings from $(basename ${conf_file}).."
|
||||||
if [[ -f "$conf_file" ]]; then
|
if [[ -f "$conf_file" ]]; then
|
||||||
source "$conf_file" > $tmp_log_file 2>&1
|
source "$conf_file" > $log_file 2>&1
|
||||||
if [[ $? -eq 0 ]]; then
|
if [[ $? -eq 0 ]]; then
|
||||||
echo_ok
|
echo_ok
|
||||||
else
|
else
|
||||||
echo_failed
|
echo_failed
|
||||||
fatal "$(cat $tmp_log_file)"
|
fatal "$(cat $log_file)"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo_skipped
|
echo_skipped
|
||||||
@ -260,7 +297,23 @@ else
|
|||||||
fi
|
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
|
if [[ -z "$mysql_credential_args" ]]; then
|
||||||
|
|
||||||
@ -298,31 +351,64 @@ if [[ -z "$mysql_credential_args" ]]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[[ -z "$log_file" ]] && log_file="$DEFAULT_LOG_FILE"
|
|
||||||
|
|
||||||
if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then
|
if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then
|
||||||
mysql_credential_args_arr[0]="default:$mysql_credential_args"
|
mysql_credential_args_arr[0]="default:$mysql_credential_args"
|
||||||
fi
|
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 length_table_name
|
||||||
declare -i number_blank_signd
|
declare -i number_blank_signd
|
||||||
declare -i index_i
|
declare -i index_i
|
||||||
for _val in ${mysql_credential_args_arr[@]} ; do
|
declare -i index_arr=0
|
||||||
|
while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
|
||||||
|
|
||||||
_all_success=true
|
_all_success=true
|
||||||
|
|
||||||
IFS=':' read -a _val_arr <<< "${_val}"
|
IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}"
|
||||||
mysql_version="${_val_arr[0]}"
|
mysql_version="${_val_arr[0]}"
|
||||||
mysql_credential_args="${_val_arr[1]}"
|
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
|
|
||||||
echo -e "[ MySQL $mysql_version ]: optimize (and repair) tables of databases at host '$(hostname -f)'." > $log_file
|
|
||||||
|
|
||||||
if [[ -z "$DATABASES" ]] ; then
|
DATABASES="$($mysql $mysql_credential_args -N -s -e "show databases")"
|
||||||
DATABASES=`$mysql $mysql_credential_args -N -s -e "show databases"`
|
|
||||||
|
found=false
|
||||||
|
if [[ -n "$GIVEN_DATABASE" ]] ; then
|
||||||
|
for db in $DATABASES ; do
|
||||||
|
if [[ "$db" = "$GIVEN_DATABASE" ]]; then
|
||||||
|
DATABASES="$GIVEN_DATABASE"
|
||||||
|
if $terminal ; then
|
||||||
|
echo ""
|
||||||
|
echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: optimize (and repair) tables of database '$GIVEN_DATABASE'."
|
||||||
|
fi
|
||||||
|
echo -e "[ MySQL $mysql_version ]: optimize (and repair) tables of database '$GIVEN_DATABASE'." > $log_file
|
||||||
|
found=true
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if ! $found ; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
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
|
||||||
|
echo -e "[ MySQL $mysql_version ]: optimize (and repair) tables of databases at host '$(hostname -f)'." > $log_file
|
||||||
fi
|
fi
|
||||||
|
|
||||||
length_table_name=0
|
length_table_name=0
|
||||||
@ -350,25 +436,25 @@ for _val in ${mysql_credential_args_arr[@]} ; do
|
|||||||
|
|
||||||
if [[ -d "/var/www/html/projekte/nd/htdocs503" ]] ; 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
|
mv /var/www/html/projekte/nd/htdocs /var/www/html/projekte/nd/htdocs.$_service_extension > $log_file 2>&1
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
error "Error while moving '/var/www/html/projekte/nd/htdocs'.\n$(cat $tmp_log_file)"
|
error "Error while moving '/var/www/html/projekte/nd/htdocs'.\n$(cat $log_file)"
|
||||||
else
|
else
|
||||||
_htdocs_nd_moved=true
|
_htdocs_nd_moved=true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if $_htdocs_nd_moved ; then
|
if $_htdocs_nd_moved ; then
|
||||||
ln -s htdocs503 /var/www/html/projekte/nd/htdocs > $tmp_log_file 2>&1
|
ln -s htdocs503 /var/www/html/projekte/nd/htdocs > $log_file 2>&1
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
error "Error while linking '/var/www/html/projekte/nd/htdocs' --> 'htdocs503'.\n$(cat $tmp_log_file)"
|
error "Error while linking '/var/www/html/projekte/nd/htdocs' --> 'htdocs503'.\n$(cat $log_file)"
|
||||||
else
|
else
|
||||||
_htdocs_nd_symlinked=true
|
_htdocs_nd_symlinked=true
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
/usr/local/apache2/bin/apachectl graceful > $tmp_log_file 2>&1
|
/usr/local/apache2/bin/apachectl graceful > $log_file 2>&1
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
|
error "Restarting Apache Webservice failed.\n$(cat $log_file)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
else
|
else
|
||||||
@ -384,23 +470,23 @@ for _val in ${mysql_credential_args_arr[@]} ; do
|
|||||||
|
|
||||||
if [[ -d "/var/www/html/projekte/nd-archiv/htdocs503" ]] ; then
|
if [[ -d "/var/www/html/projekte/nd-archiv/htdocs503" ]] ; then
|
||||||
|
|
||||||
mv /var/www/html/projekte/nd-archiv/htdocs /var/www/html/projekte/nd-archiv/htdocs.$_service_extension > $tmp_log_file 2>&1
|
mv /var/www/html/projekte/nd-archiv/htdocs /var/www/html/projekte/nd-archiv/htdocs.$_service_extension > $log_file 2>&1
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
error "Error while moving '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $tmp_log_file)"
|
error "Error while moving '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $log_file)"
|
||||||
else
|
else
|
||||||
_htdocs_nd_archiv_moved=true
|
_htdocs_nd_archiv_moved=true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
ln -s htdocs503 /var/www/html/projekte/nd-archiv/htdocs > $tmp_log_file 2>&1
|
ln -s htdocs503 /var/www/html/projekte/nd-archiv/htdocs > $log_file 2>&1
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
error "Error while linking '/var/www/html/projekte/nd-archiv/htdocs' --> 'htdocs503'.\n$(cat $tmp_log_file)"
|
error "Error while linking '/var/www/html/projekte/nd-archiv/htdocs' --> 'htdocs503'.\n$(cat $log_file)"
|
||||||
else
|
else
|
||||||
_htdocs_nd_archiv_symlinked=true
|
_htdocs_nd_archiv_symlinked=true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
/usr/local/apache2/bin/apachectl graceful > $tmp_log_file 2>&1
|
/usr/local/apache2/bin/apachectl graceful > $log_file 2>&1
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
|
error "Restarting Apache Webservice failed.\n$(cat $log_file)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
else
|
else
|
||||||
@ -443,7 +529,7 @@ for _val in ${mysql_credential_args_arr[@]} ; do
|
|||||||
echo -e " [$(date)] Optimize table '$table'" >> $log_file
|
echo -e " [$(date)] Optimize table '$table'" >> $log_file
|
||||||
length_table_name=${#table}
|
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
|
if [[ $? -ne 0 ]]; then
|
||||||
@ -454,27 +540,27 @@ for _val in ${mysql_credential_args_arr[@]} ; do
|
|||||||
echo "" >> $log_file
|
echo "" >> $log_file
|
||||||
echo " [$(date)] Repair table '$table'" >> $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
|
if [[ $? -ne 0 ]]; then
|
||||||
|
|
||||||
_all_success=false
|
_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'.")
|
error_messages_arr+=("MySQL $mysql_version: Error while repairing table '${table}' of database '$db'.")
|
||||||
echo "" >> $log_file
|
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
|
echo "" >> $log_file
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|
||||||
echo -e " [$(date)] Optimize table '$table'" >> $log_file
|
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
|
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'.")
|
error_messages_arr+=("MySQL $mysql_version: Error while (re-)optimizing table '${table}' of database '$db'.")
|
||||||
echo "" >> $log_file
|
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
|
echo "" >> $log_file
|
||||||
else
|
else
|
||||||
info "Reoptimizing table \"${table}\" of database \"$db\" was successfully."
|
info "Reoptimizing table \"${table}\" of database \"$db\" was successfully."
|
||||||
@ -492,43 +578,43 @@ for _val in ${mysql_credential_args_arr[@]} ; do
|
|||||||
if [[ "$db" = "nd" ]]; then
|
if [[ "$db" = "nd" ]]; then
|
||||||
|
|
||||||
if $_htdocs_nd_symlinked ; then
|
if $_htdocs_nd_symlinked ; then
|
||||||
rm /var/www/html/projekte/nd/htdocs > $tmp_log_file 2>&1
|
rm /var/www/html/projekte/nd/htdocs > $log_file 2>&1
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
error "Error while removing symlink '/var/www/html/projekte/nd/htdocs'.\n$(cat $tmp_log_file)"
|
error "Error while removing symlink '/var/www/html/projekte/nd/htdocs'.\n$(cat $log_file)"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if $_htdocs_nd_moved ; then
|
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
|
mv /var/www/html/projekte/nd/htdocs.$_service_extension /var/www/html/projekte/nd/htdocs > $log_file 2>&1
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
error "Error while moving back '/var/www/html/projekte/nd/htdocs'.\n$(cat $tmp_log_file)"
|
error "Error while moving back '/var/www/html/projekte/nd/htdocs'.\n$(cat $log_file)"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
/usr/local/apache2/bin/apachectl graceful > $tmp_log_file 2>&1
|
/usr/local/apache2/bin/apachectl graceful > $log_file 2>&1
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
|
error "Restarting Apache Webservice failed.\n$(cat $log_file)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
elif [[ "$db" = "nd_archiv" ]]; then
|
elif [[ "$db" = "nd_archiv" ]]; then
|
||||||
|
|
||||||
if $_htdocs_nd_archiv_symlinked ; then
|
if $_htdocs_nd_archiv_symlinked ; then
|
||||||
rm /var/www/html/projekte/nd-archiv/htdocs > $tmp_log_file 2>&1
|
rm /var/www/html/projekte/nd-archiv/htdocs > $log_file 2>&1
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
error "Error while removing symlink '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $tmp_log_file)"
|
error "Error while removing symlink '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $log_file)"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if $_htdocs_nd_archiv_moved ; then
|
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
|
mv /var/www/html/projekte/nd-archiv/htdocs.$_service_extension /var/www/html/projekte/nd-archiv/htdocs > $log_file 2>&1
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
error "Error while moving back '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $tmp_log_file)"
|
error "Error while moving back '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $log_file)"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
/usr/local/apache2/bin/apachectl graceful > $tmp_log_file 2>&1
|
/usr/local/apache2/bin/apachectl graceful > $log_file 2>&1
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
|
error "Restarting Apache Webservice failed.\n$(cat $log_file)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
fi
|
fi
|
||||||
@ -545,14 +631,28 @@ for _val in ${mysql_credential_args_arr[@]} ; do
|
|||||||
info_messages_arr+=("MySQL $mysql_version: The optimization of the MySQL tables of all databases were successful.")
|
info_messages_arr+=("MySQL $mysql_version: The optimization of the MySQL tables of all databases were successful.")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if $terminal ; then
|
if [[ -n "$GIVEN_DATABASE" ]] ; then
|
||||||
echo ""
|
if $terminal ; then
|
||||||
echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: Finished optimizing MySQL databases at host $(hostname -f)."
|
echo ""
|
||||||
echo ""
|
echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: Finished optimizing MySQL database '$GIVEN_DATABASE'."
|
||||||
|
echo ""
|
||||||
|
fi
|
||||||
|
echo "" >> $log_file
|
||||||
|
echo "[ MySQL $mysql_version ]: Finished optimizing MySQL database '$GIVEN_DATABASE'." >> $log_file
|
||||||
|
echo "" >> $log_file
|
||||||
|
else
|
||||||
|
if $terminal ; then
|
||||||
|
echo ""
|
||||||
|
echo -e "[ \033[37m\033[1mMySQL $mysql_version\033[m ]: Finished optimizing MySQL databases at host $(hostname -f)."
|
||||||
|
echo ""
|
||||||
|
fi
|
||||||
|
echo "" >> $log_file
|
||||||
|
echo "[ MySQL $mysql_version ]: Finished optimizing MySQL databases at host $(hostname -f)." >> $log_file
|
||||||
|
echo "" >> $log_file
|
||||||
fi
|
fi
|
||||||
echo "" >> $log_file
|
|
||||||
echo "[ MySQL $mysql_version ]: Finished optimizing MySQL databases at host $(hostname -f)." >> $log_file
|
(( index_arr++ ))
|
||||||
echo "" >> $log_file
|
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ ${#info_messages_arr[@]} -gt 0 ]]; then
|
if [[ ${#info_messages_arr[@]} -gt 0 ]]; then
|
||||||
|
@ -5,16 +5,21 @@ working_dir="$(dirname $(realpath $0))"
|
|||||||
|
|
||||||
conf_file="${working_dir}/conf/mysql_credetials.conf"
|
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_MYSQL_CREDENTIAL_ARGS="--defaults-file=/usr/local/mysql/sys-maint.cnf"
|
||||||
DEFAULT_LOG_FILE="/var/log/${script_name%%.*}.log"
|
|
||||||
VERBOSE=false
|
VERBOSE=false
|
||||||
|
|
||||||
|
DEFAULT_to_addresses="argus@oopen.de"
|
||||||
|
|
||||||
|
|
||||||
# -------------
|
# -------------
|
||||||
# --- Some functions
|
# --- Some functions
|
||||||
@ -64,7 +69,14 @@ usage() {
|
|||||||
clean_up() {
|
clean_up() {
|
||||||
|
|
||||||
# Perform program exit housekeeping
|
# 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
|
exit $1
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,6 +171,12 @@ trim() {
|
|||||||
echo -n "$var"
|
echo -n "$var"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
blank_line() {
|
||||||
|
if $terminal ; then
|
||||||
|
echo ""
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
detect_mysql_version () {
|
detect_mysql_version () {
|
||||||
|
|
||||||
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
|
_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 ?
|
# - Is this script running on terminal ?
|
||||||
# -
|
# -------------
|
||||||
|
|
||||||
if [[ -t 1 ]] ; then
|
if [[ -t 1 ]] ; then
|
||||||
terminal=true
|
terminal=true
|
||||||
else
|
else
|
||||||
terminal=false
|
terminal=false
|
||||||
fi
|
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
|
# - Read Commandline Parameters
|
||||||
@ -231,6 +236,39 @@ if [[ "$(trim $*)" =~ " -h" ]] || [[ "$(trim $*)" =~ " --help" ]] ; then
|
|||||||
fi
|
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
|
# - Load Settings from configuration file
|
||||||
# -------------
|
# -------------
|
||||||
@ -241,17 +279,15 @@ else
|
|||||||
GIVEN_DATABASE=""
|
GIVEN_DATABASE=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if $terminal ; then
|
blank_line
|
||||||
echo ""
|
|
||||||
fi
|
|
||||||
echononl " Loading configuration settings from $(basename ${conf_file}).."
|
echononl " Loading configuration settings from $(basename ${conf_file}).."
|
||||||
if [[ -f "$conf_file" ]]; then
|
if [[ -f "$conf_file" ]]; then
|
||||||
source "$conf_file" > $tmp_log_file 2>&1
|
source "$conf_file" > $log_file 2>&1
|
||||||
if [[ $? -eq 0 ]]; then
|
if [[ $? -eq 0 ]]; then
|
||||||
echo_ok
|
echo_ok
|
||||||
else
|
else
|
||||||
echo_failed
|
echo_failed
|
||||||
fatal "$(cat $tmp_log_file)"
|
fatal "$(cat $log_file)"
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo_skipped
|
echo_skipped
|
||||||
@ -261,7 +297,23 @@ else
|
|||||||
fi
|
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
|
if [[ -z "$mysql_credential_args" ]]; then
|
||||||
|
|
||||||
@ -299,12 +351,27 @@ if [[ -z "$mysql_credential_args" ]]; then
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[[ -z "$log_file" ]] && log_file="$DEFAULT_LOG_FILE"
|
|
||||||
|
|
||||||
if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then
|
if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then
|
||||||
mysql_credential_args_arr[0]="default:$mysql_credential_args"
|
mysql_credential_args_arr[0]="default:$mysql_credential_args"
|
||||||
fi
|
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 length_table_name
|
||||||
declare -i number_blank_signd
|
declare -i number_blank_signd
|
||||||
declare -i index_i
|
declare -i index_i
|
||||||
@ -392,7 +459,7 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
|
|||||||
|
|
||||||
length_table_name=${#table}
|
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
|
if [[ $? -ne 0 ]]; then
|
||||||
@ -403,27 +470,27 @@ while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
|
|||||||
echo "" >> $log_file
|
echo "" >> $log_file
|
||||||
echo " [$(date)] Repair table '$table'" >> $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
|
if [[ $? -ne 0 ]]; then
|
||||||
|
|
||||||
_all_success=false
|
_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'.")
|
error_messages_arr+=("MySQL $mysql_version: Error while repairing table '${table}' of database '$db'.")
|
||||||
echo "" >> $log_file
|
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
|
echo "" >> $log_file
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|
||||||
echo -e " [$(date)] Optimize table '$table'" >> $log_file
|
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
|
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'.")
|
error_messages_arr+=("MySQL $mysql_version: Error while (re-)optimizing table '${table}' of database '$db'.")
|
||||||
echo "" >> $log_file
|
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
|
echo "" >> $log_file
|
||||||
else
|
else
|
||||||
info "Reoptimizing table \"${table}\" of database \"$db\" was successfully."
|
info "Reoptimizing table \"${table}\" of database \"$db\" was successfully."
|
||||||
|
Loading…
Reference in New Issue
Block a user