Files
admin-stuff/roundcube-cleandb.sh
T

295 lines
6.1 KiB
Bash
Executable File

#!/usr/bin/env bash
CUR_IFS=$IFS
script_name="$(basename $(realpath $0))"
script_dir="$(dirname $(realpath $0))"
LOCK_DIR="/tmp/${script_name%%.*}.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log"
DEFAULT_WEB_BASE_DIR="/var/www"
DEFAULT_HTTP_USER="www-data"
# =============
# --- Some functions
# =============
usage() {
[[ -n "$1" ]] && error "$1"
[[ $terminal ]] && echo -e "
\033[1mUsage:\033[m
$(basename $0) -w <hostname_webmail> [-b <web_base_dir>] [-u <http_user>]
\033[1mDescription\033[m
Wrapper script that calls the 'cleandb.sh' script located in the 'bin'
directory of a Roundcube installation:
\${WEB_BASE_DIR}/\${HOSTNAME_WEBMAIL}/bin/cleandb.sh
\033[1mOptions\033[m
-w <hostname_webmail>
(mandatory) The hostname of the roundcube webmail instance.
-b <web_base_dir>
(optional) Base directory below which the roundcube instance
is located. Default: '${DEFAULT_WEB_BASE_DIR}'
-u <http_user>
(optional) The user the 'cleandb.sh' script is executed as.
Default: '${DEFAULT_HTTP_USER}'
\033[1mExample:\033[m
$(basename $0) -w webmail.oopen.de
"
clean_up 1
}
clean_up() {
# Perform program exit housekeeping
rm -rf "$LOCK_DIR"
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
}
echo_ok() {
if $terminal ; then
echo -e "\033[75G[ \033[32mok\033[m ]"
fi
}
echo_failed(){
if $terminal ; then
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
fi
}
info (){
if $terminal ; then
echo ""
echo -e " [ \033[32m\033[1mInfo\033[m ] $*"
echo ""
fi
}
fatal (){
echo ""
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mFatal\033[m ]: \033[37m\033[1m$*\033[m"
echo ""
echo -e " \033[31m\033[1mScript will be interrupted..\033[m!"
else
echo " [ Fatal ]: $*" 1>&2
echo "" 1>&2
echo " Script was terminated...." 1>&2
fi
clean_up 1
}
error(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
else
echo " [ Error ]: $*" 1>&2
fi
echo ""
}
blank_line() {
if $terminal ; then
echo ""
fi
}
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
# ----------
# - Jobhandling
# ----------
if pgrep -f "$(basename $0)" | grep -q -v $$ ; then
msg="A previos instance of script \"`basename $0`\" seems already be running."
if $terminal ; then
echo ""
echo -e "[ \033[31m\033[1mFatal\033[m ]: $msg"
echo ""
echo -e " \033[31m\033[1mScript was interupted\033[m!"
else
echo "" 1>&2
echo " [ Fatal ]: $msg" 1>&2
echo "" 1>&2
echo " Script was interupted!" 1>&2
fi
exit 1
else
if [[ -d "$LOCK_DIR" ]] ; then
rm -rf "$LOCK_DIR" 2> /dev/null
fi
fi
# - 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 SIGHUP SIGINT SIGTERM
else
msg="A previos instance of script \"`basename $0`\" seems already be running."
if $terminal ; then
echo ""
echo -e "[ \033[31m\033[1mFatal\033[m ]: $msg"
echo ""
echo -e " \033[31m\033[1mScript was interupted\033[m!"
else
echo "" 1>&2
echo " [ Fatal ]: $msg" 1>&2
echo "" 1>&2
echo " Script was interupted!" 1>&2
fi
exit 1
fi
# -------------
# - Read in Commandline arguments
# -------------
while getopts hw:b:u: opt ; do
case $opt in
h) usage ;;
w) HOSTNAME_WEBMAIL=$OPTARG ;;
b) WEB_BASE_DIR=$OPTARG ;;
u) HTTP_USER=$OPTARG ;;
\?) usage
esac
done
# - Reset IFS
# -
IFS=$CUR_IFS
if [[ -z "$HOSTNAME_WEBMAIL" ]] ; then
usage "Mandatory parameter '-w <hostname_webmail>' is missing."
fi
[[ -n "$WEB_BASE_DIR" ]] || WEB_BASE_DIR=$DEFAULT_WEB_BASE_DIR
[[ -n "$HTTP_USER" ]] || HTTP_USER=$DEFAULT_HTTP_USER
SITE_DIR="${WEB_BASE_DIR}/${HOSTNAME_WEBMAIL}"
if [[ ! -d "$SITE_DIR" ]] ; then
fatal "Site directory '${SITE_DIR}' not found!"
fi
HTDOCS_LINK="${SITE_DIR}/htdocs"
if [[ ! -L "$HTDOCS_LINK" ]] ; then
fatal "Symlink '${HTDOCS_LINK}' not found!"
fi
HTDOCS_TARGET="$(realpath "$HTDOCS_LINK" 2> /dev/null)"
if [[ -z "$HTDOCS_TARGET" ]] || [[ ! -d "$HTDOCS_TARGET" ]] ; then
fatal "Symlink '${HTDOCS_LINK}' points to a non existing directory!"
fi
# - The symlink 'htdocs' either points directly to the roundcube
# installation directory, or to '<installdir>/public_html'.
# -
if [[ "$(basename "$HTDOCS_TARGET")" == "public_html" ]] ; then
INSTALL_DIR="$(dirname "$HTDOCS_TARGET")"
else
INSTALL_DIR="$HTDOCS_TARGET"
fi
CLEANDB_SCRIPT="${INSTALL_DIR}/bin/cleandb.sh"
if [[ ! -f "$CLEANDB_SCRIPT" ]] ; then
fatal "Script '${CLEANDB_SCRIPT}' not found!"
fi
if [[ ! -x "$CLEANDB_SCRIPT" ]] ; then
fatal "Script '${CLEANDB_SCRIPT}' is not executable!"
fi
if $terminal ; then
echo ""
echo -e "\033[32m-----\033[m"
echo -e "Trigger \033[1mcleandb.sh\033[m for roundcube instance \033[1m${HOSTNAME_WEBMAIL}\033[m"
echo -e "\033[32m-----\033[m"
echo ""
echo -e " Hostname webmail......................: $HOSTNAME_WEBMAIL"
echo -e " Web base directory....................: $WEB_BASE_DIR"
echo -e " Install directory (via htdocs).......: $INSTALL_DIR"
echo -e " Script to be run......................: $CLEANDB_SCRIPT"
echo -e " Executing user........................: $HTTP_USER"
echo ""
fi
# -----
# - Main part of the script
# -----
if $terminal ; then
echo -e "\033[37m\033[1mMain part of the script\033[m"
echo ""
fi
echononl " Running 'cleandb.sh' for '${HOSTNAME_WEBMAIL}'.."
su -c "$CLEANDB_SCRIPT" -s /bin/bash $HTTP_USER > $log_file 2>&1
RC=$?
if [[ $RC -eq 0 ]]; then
echo_ok
info "\n\n$(cat "$log_file")"
else
echo_failed
error "$(cat "$log_file")"
fi
blank_line
clean_up $RC