This commit is contained in:
Christoph 2023-08-13 16:28:52 +02:00
commit d0d7c01ba3
5 changed files with 593 additions and 184 deletions

View File

@ -488,16 +488,16 @@ if ! $NON_INTERACTIVE_MODE ; then
echo " utf8"
echo ""
if [ -z "$DATABASE_CHARACTER_SET" ]; then
echononl "Database user for database '${DATABASE_NAME}': "
echononl "Insert character_set for database '${DATABASE_NAME}': "
read DATABASE_CHARACTER_SET
while [ "X$DATABASE_CHARACTER_SET" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Database user for database '${DATABASE_NAME}': "
echononl "insert character_set for database '${DATABASE_NAME}': "
read DATABASE_CHARACTER_SET
done
else
_DATABASE_CHARACTER_SET="$DATABASE_CHARACTER_SET"
echononl "Database user for database '${DATABASE_NAME}' [${_DATABASE_CHARACTER_SET}]: "
echononl "Insert character_set for database '${DATABASE_NAME}' [${_DATABASE_CHARACTER_SET}]: "
read DATABASE_CHARACTER_SET
if [[ "X$DATABASE_CHARACTER_SET" = "X" ]]; then
DATABASE_CHARACTER_SET=$_DATABASE_CHARACTER_SET
@ -523,16 +523,16 @@ if ! $NON_INTERACTIVE_MODE ; then
echo " utf8_bin"
echo ""
if [ -z "$DATABASE_COLLATION" ]; then
echononl "Database user for database '${DATABASE_NAME}': "
echononl "Insert collation for database '${DATABASE_NAME}': "
read DATABASE_COLLATION
while [ "X$DATABASE_COLLATION" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Database user for database '${DATABASE_NAME}': "
echononl "Insert collation for database '${DATABASE_NAME}': "
read DATABASE_COLLATION
done
else
_DATABASE_COLLATION="$DATABASE_COLLATION"
echononl "Database user for database '${DATABASE_NAME}' [${_DATABASE_COLLATION}]: "
echononl "Insert collation for database '${DATABASE_NAME}' [${_DATABASE_COLLATION}]: "
read DATABASE_COLLATION
if [[ "X$DATABASE_COLLATION" = "X" ]]; then
DATABASE_COLLATION=$_DATABASE_COLLATION

View File

@ -3,6 +3,8 @@
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/mysql_credetials.conf"
tmp_log_file="$(mktemp)"
#LOGGING=true
LOGGING=false
@ -11,6 +13,27 @@ LOGGING=false
# --- Some functions
# -------------
clean_up() {
# Perform program exit housekeeping
rm -f $tmp_log_file
blank_line
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
@ -34,6 +57,65 @@ fatal(){
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
}
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
blank_line() {
if $terminal ; then
echo ""
fi
}
detect_mysql_version () {
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
@ -87,16 +169,23 @@ fi
#-----------------------------
#---------------------------------------
blank_line
echononl " Loading configuration settings from $(basename ${conf_file}).."
if [[ -f "$conf_file" ]]; then
source "$conf_file"
source "$conf_file" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
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 $LOGGING ;then
echo -e "\n[ `date` ] Going to flush host cache.."
fi
if [[ -z "$mysql_credential_args" ]]; then
@ -135,14 +224,30 @@ if [[ -z "$mysql_credential_args" ]]; then
fi
# Flush host cache
#
$mysqladmin $mysql_credential_args flush-hosts
[[ $? -gt 0 ]] && echo -e "\t[ Error ]: Flushing host cache failed !!"
if $LOGGING ;then
echo -e "\n[ `date` ] End flushing host cache"
if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then
mysql_credential_args_arr[0]="default:$mysql_credential_args"
fi
exit 0
blank_line
declare -i index_arr=0
while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}"
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
echononl " [ ${mysql_version} ]: Flush host cache.."
$mysqladmin $mysql_credential_args flush-hosts
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
fi
(( index_arr++ ))
done
clean_up 0

View File

@ -3,8 +3,151 @@
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/mysql_credetials.conf"
#LOGGING=true
LOGGING=false
tmp_log_file="$(mktemp)"
# -------------
# --- Some functions
# -------------
clean_up() {
# Perform program exit housekeeping
rm -f $tmp_log_file
blank_line
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
}
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
blank_line() {
if $terminal ; then
echo ""
fi
}
detect_mysql_version () {
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
if [[ -z "$_MYSQLD_VERSION" ]]; then
fatal "No installed MySQL server or distribution found!"
elif [[ "$_MYSQLD_VERSION" =~ MariaDB ]]; then
MYSQL_CUR_DISTRIBUTION="MariaDB"
elif [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ percona- ]]; then
MYSQL_CUR_DISTRIBUTION="Percona"
elif [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ mysql- ]]; then
MYSQL_CUR_DISTRIBUTION="MySQL"
fi
MYSQL_VERSION="$(echo $_MYSQLD_VERSION | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?" | head -n 1)"
MYSQL_MAJOR_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f1)"
MYSQL_MINOR_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f2)"
MYSQL_PATCH_LEVEL="$(echo $MYSQL_VERSION | cut -d '.' -f3)"
MYSQL_MAIN_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f1,2)"
}
# - Is this script running on terminal ?
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
mysql=`realpath $(which mysql) 2>/dev/null`
if [ -z "$mysql" ]; then
@ -18,41 +161,6 @@ if [ -z "$mysql" ]; then
fi
fi
# Get current version
#
_MYSQLD_VERSION="$(mysqld -V 2>/dev/null)"
CURRENT_VERSION="$(echo $_MYSQLD_VERSION | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?" | head -n 1)"
MYSQL_MAIN_VERSION=`echo $CURRENT_VERSION | cut -d '.' -f1,2`
MYSQL_MAJOR_VERSION=`echo $CURRENT_VERSION | cut -d '.' -f1`
MYSQL_MINOR_VERSION=`echo $CURRENT_VERSION | cut -d '.' -f2`
MYSQL_PATCH_LEVEL=`echo $CURRENT_VERSION | cut -d '.' -f3`
if [[ -z "$_MYSQLD_VERSION" ]]; then
echo ""
echo -e "\t[ Error ]: No installed MySQL server or distribution found!"
echo ""
elif [[ "$_MYSQLD_VERSION" =~ MariaDB ]]; then
MYSQL_CUR_DISTRIBUTION="MariaDB"
elif [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ percona- ]]; then
MYSQL_CUR_DISTRIBUTION="Percona"
elif [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ mysql- ]]; then
MYSQL_CUR_DISTRIBUTION="MySQL"
fi
if [[ "$MYSQL_CUR_DISTRIBUTION" = "MySQL" ]]; then
if [[ "$MYSQL_MAJOR_VERSION" -gt 8 ]] \
|| ( [[ "$MYSQL_MAJOR_VERSION" -eq 8 ]] && [[ "$MYSQL_MINOR_VERSION" -gt 0 ]] ) \
|| ( [[ "$MYSQL_MAJOR_VERSION" -eq 8 ]] && [[ "$MYSQL_MINOR_VERSION" -eq 0 ]] \
&& [[ $MYSQL_PATCH_LEVEL -ge 3 ]] ); then
echo ""
echo -e "\t[ Error ]: Query cache is no longer supported since (MySQL 8.0.3)"
echo ""
exit
fi
fi
#---------------------------------------
#-----------------------------
@ -60,16 +168,35 @@ fi
#-----------------------------
#---------------------------------------
blank_line
echononl " Loading configuration settings from $(basename ${conf_file}).."
if [[ -f "$conf_file" ]]; then
source "$conf_file"
source "$conf_file" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
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 [[ -z "$mysql_credential_args" ]]; then
if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MYSQL_MAJOR_VERSION -gt 10 ]] \
|| ( [[ $MYSQL_MAJOR_VERSION -eq 10 ]] && [[ $MYSQL_MINOR_VERSION -gt 3 ]] )) ; then
detect_mysql_version
MAJOR_VERSION="$MYSQL_MAJOR_VERSION"
MINOR_VERSION="$MYSQL_MINOR_VERSION"
PATCH_LEVEL="$MYSQL_PATCH_LEVEL"
if [[ "$MYSQL_CUR_DISTRIBUTION" = "MariaDB" ]] && ([[ $MAJOR_VERSION -gt 10 ]] \
|| ( [[ $MAJOR_VERSION -eq 10 ]] && [[ $MINOR_VERSION -gt 3 ]] )) ; then
if [[ -S "/tmp/mysql.sock" ]]; then
mysql_credential_args="-u root -S /tmp/mysql.sock"
elif [[ -S "/run/mysqld/mysqld.sock" ]]; then
@ -94,24 +221,34 @@ if [[ -z "$mysql_credential_args" ]]; then
parameter manually."
fi
fi
fi
if $LOGGING ;then
echo -e "\n[ `date` ] Going to flush query cache.."
if [[ ${#mysql_credential_args_arr[@]} -eq 0 ]]; then
mysql_credential_args_arr[0]="default:$mysql_credential_args"
fi
# Flush Query Cache
#
$mysql $mysql_credential_args -N -s -e "FLUSH QUERY CACHE"
blank_line
declare -i index_arr=0
while [[ $index_arr -lt ${#mysql_credential_args_arr[@]} ]] ; do
[[ $? -gt 0 ]] && echo -e "\t[ Error ]: Flushing query cache failed !!"
IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}"
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
if $LOGGING ;then
echo -e "\n[ `date` ] End flushing query cache"
fi
echononl " [ ${mysql_version} ]: Flush query cache.."
$mysql $mysql_credential_args -N -s -e "FLUSH QUERY CACHE" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
fi
exit 0
(( index_arr++ ))
done
clean_up 0

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,28 +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
@ -230,27 +236,58 @@ 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
# -------------
if [[ -n "$1 " ]] ; then
DATABASES="$1"
GIVEN_DATABASE="$1"
else
DATABASES=""
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
@ -260,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
@ -298,31 +351,64 @@ 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
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
IFS=':' read -a _val_arr <<< "${_val}"
IFS=':' read -a _val_arr <<< "${mysql_credential_args_arr[$index_arr]}"
mysql_version="${_val_arr[0]}"
mysql_credential_args="${_val_arr[1]}"
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
if [[ -z "$DATABASES" ]] ; then
DATABASES=`$mysql $mysql_credential_args -N -s -e "show databases"`
fi
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
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
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
_htdocs_nd_moved=true
fi
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
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
_htdocs_nd_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
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
error "Restarting Apache Webservice failed.\n$(cat $log_file)"
fi
else
@ -384,23 +470,23 @@ for _val in ${mysql_credential_args_arr[@]} ; do
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
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
_htdocs_nd_archiv_moved=true
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
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
_htdocs_nd_archiv_symlinked=true
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
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
error "Restarting Apache Webservice failed.\n$(cat $log_file)"
fi
else
@ -443,7 +529,7 @@ for _val in ${mysql_credential_args_arr[@]} ; do
echo -e " [$(date)] Optimize table '$table'" >> $log_file
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
@ -454,27 +540,27 @@ for _val in ${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."
@ -492,43 +578,43 @@ for _val in ${mysql_credential_args_arr[@]} ; do
if [[ "$db" = "nd" ]]; 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
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
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
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
/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
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
error "Restarting Apache Webservice failed.\n$(cat $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
rm /var/www/html/projekte/nd-archiv/htdocs > $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)"
error "Error while removing symlink '/var/www/html/projekte/nd-archiv/htdocs'.\n$(cat $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
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
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
/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
error "Restarting Apache Webservice failed.\n$(cat $tmp_log_file)"
error "Restarting Apache Webservice failed.\n$(cat $log_file)"
fi
fi
@ -545,6 +631,16 @@ 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.")
fi
if [[ -n "$GIVEN_DATABASE" ]] ; then
if $terminal ; then
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)."
@ -553,6 +649,10 @@ for _val in ${mysql_credential_args_arr[@]} ; do
echo "" >> $log_file
echo "[ MySQL $mysql_version ]: Finished optimizing MySQL databases at host $(hostname -f)." >> $log_file
echo "" >> $log_file
fi
(( index_arr++ ))
done
if [[ ${#info_messages_arr[@]} -gt 0 ]]; then

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."