apache2/handle_domain_on_webserver.sh

2118 lines
65 KiB
Bash
Executable File

#!/usr/bin/env bash
# -------------
# - TODO:
# - - Support postgres databases
# - - Deleting maildomain on mailserver
# -------------
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/handle_domain_on_webserver.conf"
log_dir="${working_dir}/log"
tmp_log_file="$(mktemp)"
backup_date="$(date +%Y-%m-%d-%H%M)"
delete_mode=false
only_show=false
only_check=false
print_warnings=false
# -------------
# - Variable settings
# -------------
# -------------
# --- Some functions
# -------------
clean_up() {
# Perform program exit housekeeping
rm -f $tmp_log_file
exit $1
}
# - Check if a given array (parameter 2) contains a given string (parameter 1)
# -
containsElement () {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
stringContain() {
[ -z "${2##*$1*}" ] && [ -z "$1" -o -n "$2" ]
}
# - Remove leading/trailling whitespaces
# -
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
echononl(){
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$$
}
fatal(){
echo ""
if [[ -n "$*" ]] ; then
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
echo ""
fi
echo -e " \033[31m\033[1mScript will be interrupted.\033[m\033[m"
echo ""
clean_up 1
}
error(){
echo ""
echo -e " [ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
}
warn (){
echo ""
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
echo ""
}
echo_done() {
echo -e "\033[80G[ \033[32mdone\033[m ]"
}
echo_ok() {
echo -e "\033[80G[ \033[32mok\033[m ]"
}
echo_warning() {
echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]"
}
echo_failed(){
echo -e "\033[80G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[80G[ \033[37mskipped\033[m ]"
}
trap clean_up SIGHUP SIGINT SIGTERM
# -------------
# - Some checks
# -------------
if [[ "$(basename $0)" = "show_domain_on_webserver.sh" ]] ; then
only_show=true
elif [[ "$(basename $0)" = "check_domain_on_webserver.sh" ]] ; then
only_check=true
print_warnings=true
elif [[ "$(basename $0)" = "delete_domain_related_configurations.sh" ]] ; then
delete_mode=true
print_warnings=true
else
fatal "Run one of the related scripts \033[1mshow_domain_on_webserver.sh\033[m,\n \033[1mcheck_domain_on_webserver.sh\\033[m or \033[1mdelete_domain_related_configurations.sh\033[m."
fi
# - Is 'systemd' supported on this system
# -
if [ "X`which systemd`" = "X" ]; then
systemd_exists=false
else
systemd_exists=true
fi
[[ -n "$*" ]] && _domains="$*"
# -
# - grep -i -d skip -h -E "^\s*include\s+.*\.conf" $_apache_vhost_base_dir | awk '{print$2}'
# -
# - Try to find apache vhost configuration directory
# -
if [[ -d "/usr/local/apache2/conf/vhosts" ]]; then
if [[ -d "/usr/local/apache2/conf/vhosts/0" ]]; then
_apache_vhost_dir="/usr/local/apache2/conf/vhosts/0"
else
_apache_vhost_dir="/usr/local/apache2/conf/vhosts"
fi
elif [[ -d "/etc/apache2/sites-enabled" ]]; then
_apache_vhost_dir="/etc/apache2/sites-enabled"
fi
# - Try to find apache websites base directory
# -
if [[ -d "/var/www/html/projekte" ]]; then
_apache_web_base_dir="/var/www/html/projekte"
elif [[ -d "/var/www/html" ]]; then
_apache_web_base_dir="/var/www/html"
elif [[ -d "/var/www" ]]; then
_apache_web_base_dir="/var/www"
fi
clear
echo ""
echo -e "\033[32mRunning script \033[1m"$(basename $0)"\033[m .."
echo ""
# -------------
# - Load Settings from configuration file handle_domain_on_webserver.conf
# -------------
if $delete_mode ; then
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
echo_failed
fatal "$(cat $tmp_log_file)"
fi
else
echo_failed
fatal "Configuration file \033[1m$conf_file\033[m not found!"
fi
[[ -n "$DNS_MASTER_SERVER" ]] || fatal "Master Nameserver ist not set (Parameter DNS_MASTER_SERVER)!"
include_slave_dns=true
[[ -n "$DNS_SLAVE_SERVER" ]] || include_slave_dns=false
[[ -n "$DNS_SSH_USER" ]] || DNS_SSH_USER="manage-bind"
[[ -n "$DNS_SSH_PORT" ]] || DNS_SSH_PORT="22"
[[ -n "$DNS_SSH_KEY" ]] || DNS_SSH_KEY="/root/.ssh/id_rsa-dns1"
[[ -n "$SCRIPT_get_domain_by_hostname" ]] || SCRIPT_get_domain_by_hostname="/root/bin/bind/bind_get_domain_by_hostname.sh"
[[ -n "$SCRIPT_remove_master_domain" ]] || SCRIPT_remove_master_domain="/root/bin/bind/bind_remove_domain_on_master.sh"
[[ -n "$SCRIPT_remove_slave_domain" ]] || SCRIPT_remove_slave_domain="/root/bin/bind/bind_remove_domain_on_slave.sh"
[[ -n "$MYSQL_CREDENTIAL_ARGS" ]] || MYSQL_CREDENTIAL_ARGS="--login-path=local"
fi
# -------------
# - Some prior checks
# -------------
if $delete_mode ; then
ssh -q -p $DNS_SSH_PORT \
-o BatchMode=yes \
-o StrictHostKeyChecking=no \
-i $DNS_SSH_KEY \
-l $DNS_SSH_USER \
$DNS_MASTER_SERVER "ls" > /dev/null 2>&1
if [[ $? -gt 0 ]] ;then
fatal "Nameserver \"$DNS_MASTER_SERVER\" is not reachable vis ssh!"
fi
ssh -q -p $DNS_SSH_PORT \
-o BatchMode=yes \
-o StrictHostKeyChecking=no \
-i $DNS_SSH_KEY \
-l $DNS_SSH_USER \
$DNS_MASTER_SERVER "sudo $SCRIPT_get_domain_by_hostname check" > /dev/null 2>&1
if [[ $? -gt 0 ]] ;then
fatal "Failed to access \033[1m$SCRIPT_get_domain_by_hostname\033[m on Nameserver \"$DNS_MASTER_SERVER\"!"
fi
ssh -q -p $DNS_SSH_PORT \
-o BatchMode=yes \
-o StrictHostKeyChecking=no \
-i $DNS_SSH_KEY \
-l $DNS_SSH_USER \
$DNS_MASTER_SERVER "sudo $SCRIPT_remove_master_domain check" > /dev/null 2>&1
if [[ $? -gt 0 ]] ;then
fatal "Failed to access \033[1m$SCRIPT_remove_master_domain\033[m on Nameserver \"$DNS_MASTER_SERVER\"!"
fi
if $include_slave_dns ; then
ssh -q -p $DNS_SSH_PORT \
-o BatchMode=yes \
-o StrictHostKeyChecking=no \
-i $DNS_SSH_KEY \
-l $DNS_SSH_USER \
$DNS_SLAVE_SERVER "ls" > /dev/null 2>&1
if [[ $? -gt 0 ]] ;then
fatal "Nameserver \"$DNS_SLAVE_SERVER\" is not reachable vis ssh!"
fi
ssh -q -p $DNS_SSH_PORT \
-o BatchMode=yes \
-o StrictHostKeyChecking=no \
-i $DNS_SSH_KEY \
-l $DNS_SSH_USER \
$DNS_SLAVE_SERVER "sudo $SCRIPT_remove_slave_domain check" > /dev/null 2>&1
if [[ $? -gt 0 ]] ;then
fatal "Failed to access \033[1m$SCRIPT_remove_slave_domain\033[m on Nameserver \"$DNS_SLAVE_SERVER\"!"
fi
fi
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert domain(s), you wish to delete database, webspace and webserver configurations for.."
echo ""
echo " Multi domains are supported - give a blank separated list"
echo ""
echo ""
domains=
if [ -z "$_domains" ]; then
echononl "Domain: "
read domains
while [ "X$domains" = "X" ] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Domain: "
read domains
done
else
echononl "Domain [${_domains}]: "
read domains
if [[ "X$domains" = "X" ]]; then
domains=$_domains
fi
fi
declare -a domain_req_for_del_arr
declare -a domain_marked_for_del_arr
for _domain in $domains ; do
domain_req_for_del_arr+=("$_domain")
done
echo ""
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert Apache VHost directory.."
echo ""
echo ""
apache_vhost_dir=
if [ -z "$_apache_vhost_dir" ]; then
echononl "Apache VHost directory: "
read apache_vhost_dir
while [[ "X$apache_vhost_dir" = "X" ]] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Apache VHost directory: "
read apache_vhost_dir
done
else
echononl "Apache VHost directory [${_apache_vhost_dir}]: "
read apache_vhost_dir
if [[ "X$apache_vhost_dir" = "X" ]] ; then
apache_vhost_dir="$_apache_vhost_dir"
fi
fi
if [[ "$(basename $apache_vhost_dir)" = 'sites-enabled' ]]; then
apache_vhost_base_dir="$(dirname $apache_vhost_dir)/sites-available"
elif [[ "$(basename $apache_vhost_dir)" =~ vhost ]]; then
apache_vhost_base_dir="$apache_vhost_dir"
elif [[ "$(basename $(dirname $apache_vhost_dir))" =~ vhost ]]; then
apache_vhost_base_dir="$(dirname $apache_vhost_dir)"
fi
echo ""
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo "Insert Base Directory Apache Websites"
echo ""
echo ""
apache_web_base_dir=
if [ -z "$_apache_web_base_dir" ]; then
echononl "Base Directory Apache Websites: "
read apache_web_base_dir
while [[ "X$apache_web_base_dir" = "X" ]] ; do
echo -e "\n\t\033[33m\033[1mEingabe erforderlich.\033[m\n"
echononl "Base Directory Apache Websites: "
read apache_web_base_dir
done
else
echononl "Base Directory Apache Websites [${_apache_web_base_dir}]: "
read apache_web_base_dir
if [[ "X$apache_web_base_dir" = "X" ]] ; then
apache_web_base_dir="$_apache_web_base_dir"
fi
fi
echo ""
echo ""
echo -e "\033[32m\033[1m====================\033[m"
echo ""
echo -n " Domain(s)..............................: "
declare -i counter=1
for _domain in "${domain_req_for_del_arr[@]}" ; do
if [[ $counter -eq 1 ]] ; then
echo -e "\033[1m$_domain\033[m"
else
echo -e " \033[1m$_domain\033[m"
fi
counter=$((counter+1))
done
unset counter
echo ""
echo -e " Apache Vhosts Directory................: \033[1m$apache_vhost_dir\033[m"
echo -e " Base Directory Apache Websites.........: \033[1m$apache_web_base_dir\033[m"
if $delete_mode ; then
echo ""
echo -e " Master Nameserver supporting websites..: \033[1m$DNS_MASTER_SERVER\033[m"
echo -e " Slave Nameserver supporting websites...: \033[1m$DNS_SLAVE_SERVER\033[m"
echo ""
echo -e " SSH User to access Nameserver..........: \033[1m$DNS_SSH_USER\033[m"
echo -e " SSH Port to access Nameserver..........: \033[1m$DNS_SSH_PORT\033[m"
echo -e " SSH Key to access Nameserver...........: \033[1m$DNS_SSH_KEY\033[m"
echo ""
echo -e " Remote Script to get zone..............: \033[1m$SCRIPT_get_domain_by_hostname\033[m"
echo -e " Remote Script to remove master zone....: \033[1m$SCRIPT_remove_master_domain\033[m"
echo -e " Remote Script to remove slave zone.....: \033[1m$SCRIPT_remove_slave_domain\033[m"
fi
echo ""
echo ""
#warn "To continue with te above Setting type uppercase 'YES'."
echo -e -n "\033[1mContinue with above settings? [y/n]:\033[m "
read OK
while [[ "X${OK}X" = "XX" ]] ; do
echo ""
echo -e -n "\033[1mContinue with above settings? [y/n]:\033[m "
read OK
done
if [[ "${OK,,}" != 'yes' ]] && [[ "${OK,,}" != 'y' ]]; then
fatal "Abort by user request."
fi
if $delete_mode ; then
# - Create Log directory for permanent log files
# -
echo ""
echononl " Create Log directory '$log_dir' for permanent log files"
if [[ ! -d "$log_dir" ]]; then
mkdir "$log_dir" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fatal $(cat $tmp_log_file)
fi
else
echo_skipped
fi
main_log=${log_dir}/main-${backup_date}.log
> $main_log
echo "# ==========" >> $main_log
echo "# - Start Script $(basename $0) with following Parameters" >> $main_log
echo "# ==========" >> $main_log
echo "" >> $main_log
declare -i counter=1
for _domain in "${domain_req_for_del_arr[@]}" ; do
if [[ $counter -eq 1 ]] ; then
echo -e "# - Domain(s)................................: $_domain" >> $main_log
else
echo -e "# - $_domain" >> $main_log
fi
counter=$((counter+1))
done
unset counter
echo "# -" >> $main_log
echo "# - Apache Vhosts Directory..................: $apache_vhost_dir" >> $main_log
echo "# - Base directory apache websites...........: $apache_web_base_dir" >> $main_log
echo "# -" >> $main_log
echo ""
echo "# - Master Nameserver supporting websites....: $DNS_MASTER_SERVER" >> $main_log
echo "# - Slave Nameserver supporting websites.....: $DNS_SLAVE_SERVER" >> $main_log
echo "# -" >> $main_log
echo "# - SSH User to access Nameserver............: $DNS_SSH_USER" >> $main_log
echo "# - SSH Port to access Nameserver............: $DNS_SSH_PORT" >> $main_log
echo "# - SSH Key to access Nameserver.............: $DNS_SSH_KEY" >> $main_log
echo "# -" >> $main_log
echo "# - Remote Script to get zone................: $SCRIPT_get_domain_by_hostname" >> $main_log
echo "# - Remote Script to remove master zone......: $SCRIPT_remove_master_domain" >> $main_log
echo "# - Remote Script to remove slave zone.......: $SCRIPT_remove_slave_domain" >> $main_log
#echo "" >> $main_log
#echo "apache_vhost_dir=\"$apache_vhost_dir\"" >> $main_log
#echo "DNS_MASTER_SERVER=\"$DNS_MASTER_SERVER\"" >> $main_log
#echo "DNS_SLAVE_SERVER=\"$DNS_SLAVE_SERVER\"" >> $main_log
#echo "DNS_SSH_USER=\"$DNS_SSH_USER\"" >> $main_log
#echo "DNS_SSH_PORT=\"$DNS_SSH_PORT\"" >> $main_log
#echo "DNS_SSH_KEY=\"$DNS_SSH_KEY\"" >> $main_log
#echo "SCRIPT_get_domain_by_hostname=\"$SCRIPT_get_domain_by_hostname\"" >> $main_log
#echo "SCRIPT_remove_master_domain=\"$SCRIPT_remove_master_domain\"" >> $main_log
fi
# -----
# - Gathering global settings
# -----
# - Check if mod_php is installed.
# -
mod_php_installed=false
if $(httpd -M | grep -q -E "php[5-9]?_module" 2>/dev/null) ; then
mod_php_installed=true
if $(apachectl status | grep -q "PHP/" 2>/dev/null) ; then
MOD_PHP_VERSION="$(trim $(apachectl status | grep "PHP/" | cut -d '/' -f2))"
else
MOD_PHP_VERSION=" UNKNOWN"
warn "'mod_php' ist running, but i cannot dermin the PHP Version.\n 'apachectl status' does not work on this server."
fi
fi
#declare -a _all_vhost_files_arr
#declare -a _all_document_root_dirs_arr
#while IFS='' read -r -d '' filename ; do
# if ! containsElement "$(realpath $filename)" "${_all_vhost_files_arr[@]}" ; then
# _all_vhost_files_arr+=("$(realpath $filename)")
# fi
#done < <(find $apache_vhost_dir -mindepth 1 -maxdepth 1 -type f -print0)
#
#for _vhost_file in ${_all_vhost_files_arr[@]} ; do
# _doc_root="$(grep -E "\s*DocumentRoot\s+" $_vhost_file 2> /dev/null \
# | awk '{print$2}' | sed 's/"//g' | sed 's/\/$//' | sort -u )"
# if [[ -n "${_doc_root}" ]]; then
# _all_document_root_dirs_arr+=("${_doc_root}:${_vhost_file}")
# fi
#done
# - Associative Array(s)
# -
declare -A vhost_file_domain_arr
# - Non associative Array(s)
# -
declare -a vhost_file_req_for_del_arr
declare -a vhost_file_marked_for_del_arr
declare -a no_vhost_config_found
declare -a docroot_req_for_del_arr
declare -a docroot_marked_for_del_arr
declare -a database_req_for_del_arr
declare -a database_marked_for_del_arr
declare -a A_record_marked_for_del_arr
declare -a AAAA_record_marked_for_del_arr
declare -a hostname_in_use_arr
declare -a msg_exclude_vhost_file_from_del_arr
declare -a msg_exclude_doc_root_from_del_arr
declare -a msg_exclude_domain_from_del_arr
declare -a msg_exclude_db_from_del_arr
for domain in "${domain_req_for_del_arr[@]}" ; do
echo ""
echo -e "\033[32mGoing to inspect domain \033[1m${domain}\033[m:"
# - Empty domain concerning arrays
# -
hostname_arr=()
vhost_file_arr=()
symlink_file_arr=()
activ_file_arr=()
inactiv_file_arr=()
documentroot_arr=()
database_arr=()
site_cms_arr=()
msg_missing_database_arr=()
msg_hostname_in_use_arr=()
php_version_arr=()
php_version_active_arr=()
# -----
# - Get Vhost configuration for current domain
# -----
_vhost_files="$(grep -l -d skip -E "^\s*(ServerName|ServerAlias)\s+.*${domain}" $apache_vhost_dir/* 2> /dev/null)"
if [[ -z "$_vhost_files" ]]; then
error "No VHost Configuration found for domain \033[37m\033[1m${domain}\033[m."
# - Delete current domain from array 'domain_req_for_del_arr'
# -
no_vhost_config_found+=("${domain}")
continue
fi
echo ""
for _vhost_file in $_vhost_files ; do
_basename_vhost_file="$(basename $_vhost_file)"
containsElement "$_basename_vhost_file" "${vhost_file_arr[@]}" && continue
vhost_file_arr+=("$_basename_vhost_file")
if [[ -z "${vhost_file_domain_arr["$domain"]}" ]] ; then
vhost_file_domain_arr["$domain"]="$_basename_vhost_file"
else
vhost_file_domain_arr["$domain"]="${vhost_file_domain_arr["$domain"]}:$_basename_vhost_file"
fi
if ! containsElement "$_basename_vhost_file" "${vhost_file_req_for_del_arr[@]}" ; then
vhost_file_req_for_del_arr+=("$_basename_vhost_file")
fi
if [[ "${_vhost_file##*.}" = "conf" ]]; then
if [[ -L "$_vhost_file" ]] ; then
symlink_file_arr+=("$_basename_vhost_file")
_vhost_file="$(realpath $_vhost_file)"
activ_file_arr+=("$(basename $(realpath $_vhost_file))")
# - If this file was (wrongly) written befor into array 'inactiv_file_arr' (see below)
# - we will delete it from array here.
# -
if containsElement "$_basename_vhost_file" "${inactiv_file_arr[@]}" ; then
new_array=()
for value in "${inactiv_file_arr[@]}" ; do
[[ $value != "$_basename_vhost_file" ]] && new_array+=($value)
done
inactiv_file_arr=("${new_array[@]}")
unset new_array
fi
else
activ_file_arr+=("$_basename_vhost_file")
fi
else
# - If an activ configuration file will be added here. We
# - correct it some loops later (see above)
# -
inactiv_file_arr+=("$_basename_vhost_file")
fi
# -----
# - Get hostnames of current vhost configuration file
# -----
# - Get Server Name
# -
_server_names=$(grep -i -E "^\s*ServerName" $_vhost_file | sed -e "s/ServerName//" | sed "s/^\s*//" | sed "s/\s*$//" | sort -u)
if [[ -n "$_server_names" ]] ; then
for _server_name in $_server_names ; do
if [[ ${#hostname_arr[@]} -eq 0 ]] ; then
hostname_arr+=("$_server_name")
else
containsElement "$_server_name" "${hostname_arr[@]}" && continue
hostname_arr+=("$_server_name")
fi
done
fi
# - Get Server Aliase
# -
_server_aliases=$(grep -i -E "^\s*ServerAlias" $_vhost_file | sed -e "s/ServerAlias//" | sed "s/^\s*//" | sed "s/\s*$//" | sort -u)
if [[ -n "$_server_aliases" ]] ; then
for _server_alias in $_server_aliases ; do
if [[ ${#hostname_arr[@]} -eq 0 ]] ; then
hostname_arr+=("$_server_alias")
else
containsElement "$_server_alias" "${hostname_arr[@]}" && continue
hostname_arr+=("$_server_alias")
fi
done
fi
# - Are there any hostnames NOT concerning to domains, which are requested
# - to delete?
# -
__msg=""
for hostname in ${hostname_arr[@]} ; do
_matched_domain=false
for _other_domain in ${domain_req_for_del_arr[@]} ; do
if [[ "$hostname" =~ ${_other_domain}$ ]] ; then
_matched_domain=true
break
fi
done
if ! $_matched_domain ; then
_tld="${hostname##*.}"
_tmp_str="${hostname%.*}"
_maindomain="${_tmp_str##*.}"
if ! containsElement "$_basename_vhost_file:${hostname}:${_maindomain}.${_tld}" "${hostname_in_use_arr[@]}" ; then
__msg="The Vhost configuration file \033[37m\033[1m$_basename_vhost_file\033[m contains
also hostname \033[33m${hostname}\033[m. But the concerning domain \033[33m${_maindomain}.${_tld}\033[m
is not requested for deletion."
msg_hostname_in_use_arr[${#msg_hostname_in_use_arr[@]}]="$__msg"
hostname_in_use_arr+=("$_basename_vhost_file:${hostname}:${_maindomain}.${_tld}")
fi
fi
done
# -----
# - Get documetroot of current VHost configuration file
# ----
_documentroot=$(grep -E "^\s*DocumentRoot" $_vhost_file 2> /dev/null | awk '{print$2}' | sed 's/"//g' | sed 's/\/$//' | sort -u)
if ! containsElement "$_documentroot" "${documentroot_arr[@]}" ; then
documentroot_arr+=("$_documentroot")
if ! containsElement "$_documentroot" "${docroot_req_for_del_arr[@]}" ; then
docroot_req_for_del_arr+=("$_documentroot")
fi
fi
done # for _vhost_file in $_vhost_files
# ---
# - Get databases amd type of CMS
# ---
for _doc_root in "${documentroot_arr[@]}" ; do
site_cms="Unknown"
file_to_check="$(dirname $_doc_root)/db.inc.php"
file_to_check_mediawiki="$(dirname $_doc_root)/htdocs/LocalSettings.php"
_found=false
if [[ -r "$file_to_check_mediawiki" ]] && [[ ! -d "$file_to_check_mediawiki" ]] ; then
db_search_string='\$wgDBname'
#db_search_string='\$wgEnableUserEmail'
_found=false
if grep -i -q "$db_search_string" $file_to_check_mediawiki > /dev/null 2>&1 ; then
database="$(grep -i "$db_search_string" $file_to_check_mediawiki 2> /dev/null \
| awk -F '=' '{print$2}' \
| awk -F ';' '{print$1}'\
| tail -1)"
# - Remove leading whitespace characters
database="${database#"${database%%[![:space:]]*}"}"
# - Remove trailing whitespace characters
database="${database%"${database##*[![:space:]]}"}"
# - Remove leading single quote
database="${database#"${database%%[!\']*}"}"
# - Remove trailing single quote
database="${database%"${database##*[!\']}"}"
# - Remove leading double quote
database="${database#"${database%%[!\"]*}"}"
# - Remove trailing double quote
database="${database%"${database##*[!\"]}"}"
_found=true
fi
site_cms="MediaWiki"
elif [[ -r "$file_to_check" ]] && [[ ! -d "$file_to_check" ]]; then
db_search_strings='\$db_name \$mysql_db'
_found=false
for _string in $db_search_strings ; do
if grep -i -q -E "^\s*$_string" $file_to_check > /dev/null 2>&1 ; then
database="$(grep -i -E "^\s*$_string" $file_to_check 2> /dev/null | awk -F '=' '{print$2}' | tail -1 )"
# - Remove leading whitespace characters
database="${database#"${database%%[![:space:]]*}"}"
# - Remove trailing whitespace characters
database="${database%"${database##*[![:space:]]}"}"
# - Rmove trailing semicolon characters
database="${database%"${database##*[!;]}"}"
# - Remove trailing whitespace characters
database="${database#"${database%%[!\']*}"}"
# - Remove trailing inverted comma characters
database="${database%"${database##*[!\']}"}"
_found=true
break
fi
done
if ! $_found ; then
file_to_check="${_doc_root}/includes/config.inc.php"
if [[ -f "$file_to_check" ]]; then
if grep -i -q -E "^\s*define\('DB_DATABASE'" $file_to_check > /dev/null 2>&1 ; then
database="$(grep -i -m 1 -E "^\s*define\('DB_DATABASE'" $file_to_check 2> /dev/null \
| awk -F ',' '{print$2}' \
| awk -F ')' '{print$1}' \
| tail -1 )"
# - Remove leading whitespace characters
database="${database#"${database%%[![:space:]]*}"}"
# - Remove trailing whitespace characters
database="${database%"${database##*[![:space:]]}"}"
# - Remove leading single quote
database="${database#"${database%%[!\']*}"}"
# - Remove trailing single quote
database="${database%"${database##*[!\']}"}"
# - Remove leading double quote
database="${database#"${database%%[!\"]*}"}"
# - Remove trailing double quote
database="${database%"${database##*[!\"]}"}"
_found=true
fi
fi
fi
fi
if $_found ; then
database_arr+=("${database}:${_doc_root}")
#if ! containsElement "$database" "${database_req_for_del_arr[@]}" ; then
# database_req_for_del_arr+=("${database}")
#fi
if ! containsElement "${database}:${_doc_root}" "${database_req_for_del_arr[@]}" ; then
database_req_for_del_arr+=("${database}:${_doc_root}")
fi
site_cms="Kontext"
else
__msg="No database found for documentroot directory ${_doc_root}"
msg_missing_database_arr+=("$__msg")
fi
[[ -n "$site_cms" ]] && site_cms_arr+=("${site_cms}:${_doc_root}")
done
# -----
# - Get PHP version of current vhost configuration file
# -----
for _file in ${vhost_file_arr[@]} ; do
_php_engine=""
_php_version=""
CUR_IFS=$IFS
IFS=''
if grep -q -E "^\s*(ProxyPassMatch|SetHandler).*php-[5-9]\.[0-9]+-" $apache_vhost_dir/$_file 2> /dev/null ; then
_php_engine="PHP FPM/FastCGI"
if grep -q -E "^\s*(SetHandler).*php-[5-9]\.[0-9]+-" $apache_vhost_dir/$_file 2> /dev/null ; then
_php_fpm_socket="$(grep -E "^\s*(SetHandler).*php-[5-9]\.[0-9]+-" $apache_vhost_dir/$_file 2> /dev/null \
| head -1 | cut -d ':' -f3 | cut -d'|' -f1)"
else
_php_fpm_socket=
fi
if [[ -S "$_php_fpm_socket" ]] ; then
_php_version="$(exec /usr/local/php-$(echo "$_php_fpm_socket" | grep -o -E "[5-9]\.[0-9]")/bin/php-cgi -v \
| head -1 | awk '{print$2}')"
else
_php_version=" UNKNOWN"
fi
elif grep -d skip -q -E "^\s*FCGIWrapper\s+.*\.php" $apache_vhost_dir/$_file 2> /dev/null ; then
_php_engine="PHP CGI/FastCGI"
_fcgid_file="$(grep -E "^\s*FCGIWrapper\s+.*\.php" $apache_vhost_dir/$_file 2> /dev/null \
| head -1 | awk '{print$2}')"
if [[ -f "$_fcgid_file" ]] ; then
_php_version="$($(grep "exec" $_fcgid_file | awk '{print$2}') -v | head -1 | awk '{print$2}')"
[[ -z "$_php_version" ]] && _php_version="unknown Version"
else
_php_version=" UNKNOWN"
fi
elif $mod_php_installed ; then
#_php_engine="Apache 2.0 Handler"
_php_engine="Apache Handler"
_php_version="$MOD_PHP_VERSION"
else
_php_engine="No PHP assigned"
fi
if ! containsElement "$_file:${_php_engine}:$_php_version" "${php_version_arr[@]}" ; then
php_version_arr+=("$_file:${_php_engine}:$_php_version")
fi
if containsElement "$_file" "${activ_file_arr[@]}" ; then
php_version_active_arr+=("$_file:${_php_engine}:$_php_version")
fi
IFS=$CUR_IFS
done
# -----
# - Summary current domain
# -----
#echo -e " \033[32mSummary domain \033[1m${domain}\033[m:\n"
echo " VHost files:"
for _file in ${vhost_file_arr[@]} ; do
echo -n " $_file"
if containsElement "$_file" "${symlink_file_arr[@]}" ; then
echo " (Symlink)"
elif containsElement "$_file" "${activ_file_arr[@]}" ; then
echo " (active)"
else
echo " (inactive)"
fi
done
echo ""
CUR_IFS=$IFS
IFS=''
echo " PHP Versions (active):"
for _val in ${php_version_active_arr[@]} ; do
IFS=':' read -a _val_arr <<< "$_val"
echo -e " ${_val_arr[0]}:\033[55G\033[33m${_val_arr[1]} ${_val_arr[2]}\033[m"
done
echo ""
echo " PHP Versions (all):"
for _val in ${php_version_arr[@]} ; do
IFS=':' read -a _val_arr <<< "$_val"
echo -e " ${_val_arr[0]}:\033[55G${_val_arr[1]} ${_val_arr[2]}"
done
echo ""
IFS=$CUR_IFS
echo " Hostnames: "
for _hostname in ${hostname_arr[@]} ; do
echo " $_hostname"
done
echo ""
echo " DocumentRoot directories: "
for _doc_root in ${documentroot_arr[@]} ; do
echo " $_doc_root"
done
echo ""
echo " Database(s):"
for _val in "${database_arr[@]}" ; do
IFS=':' read -a _val_arr <<< "$_val"
echo " ${_val_arr[0]} - ${_val_arr[1]}"
done
echo ""
echo " Type of CMS:"
for _val in "${site_cms_arr[@]}" ; do
IFS=':' read -a _val_arr <<< "$_val"
echo " ${_val_arr[0]} - ${_val_arr[1]}"
done
echo ""
if $print_warnings ; then
# -----
# - Warnings current documentroot
# -----
CUR_IFS=$IFS
IFS=''
msg=""
declare -i _counter=1
if [[ ${#msg_hostname_in_use_arr[@]} -gt 0 ]] ; then
for _msg in ${msg_hostname_in_use_arr[@]} ; do
if [[ $_counter -eq 1 ]]; then
msg="$_msg"
else
msg="$msg\n\n $_msg"
fi
_counter=$((_counter+1))
done
warn "$msg"
fi
IFS=$CUR_IFS
# -----
# - Warnings current database(s)
# -----
CUR_IFS=$IFS
IFS=''
msg=""
declare -i _counter=1
if [[ ${#msg_missing_database_arr[@]} -gt 0 ]] ; then
for _msg in ${msg_missing_database_arr[@]} ; do
if [[ $_counter -eq 1 ]]; then
msg="$_msg"
else
msg="$msg\n\n $_msg"
fi
_counter=$((_counter+1))
done
warn "$msg"
fi
IFS=$CUR_IFS
fi # if $print_warnings
done
if $only_check || $only_show ; then
rm -f $tmp_log_file
echo ""
clean_up
fi
echo ""
echo -e "\033[32m------\033[m"
echo -e "Summary VHost Configuration files"
echo -e "\033[32m------\033[m"
# ==========
# - Vhost configuration files
# ==========
for _vhost_file in ${vhost_file_req_for_del_arr[@]}; do
_found=false
for _val in ${hostname_in_use_arr[@]} ; do
IFS=':' read -a _val_arr <<< "${_val}"
if [[ "${_val_arr[0]}" = "$_vhost_file" ]] ; then
_found=true
__msg="The VHost configuration file \033[37m\033[1m${_vhost_file}\033[m is also
used at least by domain \033[33m${_val_arr[1]}\033[m.
But that domain isn't marked for deletion."
msg_exclude_vhost_file_from_del_arr[${#msg_exclude_vhost_file_from_del_arr[@]}]="$__msg"
break
fi
done
if ! $_found ; then
vhost_file_marked_for_del_arr+=("$_vhost_file")
fi
done
# - VHost configuration files requested for deletion
# -
echo ""
echo " VHost configuration files requested for deletion"
if [[ ${#vhost_file_req_for_del_arr[@]} -gt 0 ]]; then
for _vhost_file in ${vhost_file_req_for_del_arr[@]} ; do
echo " $_vhost_file"
done
else
echo -e " *** \033[33mNo VHost configuration files are requested for deletion\033[m ***"
fi
# - VHost configurtion files marked for deletion
# -
echo ""
echo " VHost configuration files marked for deletion"
if [[ ${#vhost_file_marked_for_del_arr[@]} -gt 0 ]]; then
for _vhost_file in ${vhost_file_marked_for_del_arr[@]} ; do
echo " $_vhost_file"
done
else
echo -e " *** \033[33mNo VHost configuration files are marked for deletion\033[m ***"
fi
# -----
# - Warn about VHost configurtion files not be marked for deletion.
# -----
CUR_IFS=$IFS
IFS=''
msg=""
declare -i _counter=1
if [[ ${#msg_exclude_vhost_file_from_del_arr[@]} -gt 0 ]] ; then
for _msg in ${msg_exclude_vhost_file_from_del_arr[@]} ; do
if [[ $_counter -eq 1 ]]; then
msg="$_msg"
else
msg="$msg\n\n $_msg"
fi
_counter=$((_counter+1))
done
warn "$msg"
fi
IFS=$CUR_IFS
echo ""
echo -e "\033[32m------\033[m"
echo -e "Summary DocumentRoot directories - This may take some time"
echo -e "\033[32m------\033[m"
# ==========
# - DocumentRoot directories
# ==========
# - Check for DocumetRoot directories that are in use by VHost configuration files NOT
# - marked for deletion. If found, do also NOT mark that DocumentRoot directory for
# - deletion.
# -
declare -a _all_vhost_files_arr
declare -a _all_document_root_dirs_arr
while IFS='' read -r -d '' filename ; do
if ! containsElement "$(realpath $filename)" "${_all_vhost_files_arr[@]}" ; then
_all_vhost_files_arr+=("$(realpath $filename)")
fi
done < <(find $apache_vhost_dir -mindepth 1 -maxdepth 1 -type f -print0)
for _vhost_file in ${_all_vhost_files_arr[@]} ; do
_doc_root="$(grep -E "\s*DocumentRoot\s+" $_vhost_file 2> /dev/null \
| awk '{print$2}' | sed 's/"//g' | sed 's/\/$//' | sort -u )"
if [[ -n "${_doc_root}" ]]; then
_all_document_root_dirs_arr+=("$(realpath ${_doc_root}):${_vhost_file}")
fi
done
for _doc_root in ${docroot_req_for_del_arr[@]} ; do
_not_in_use_by_others=true
_realpath_doc_root="$(realpath $_doc_root)"
for _val in ${_all_document_root_dirs_arr[@]} ; do
IFS=':' read -a _val_arr <<< "$_val"
if [[ "${_val_arr[0]}" = "$_realpath_doc_root" ]] ; then
if ! containsElement "$(basename ${_val_arr[1]})" "${vhost_file_marked_for_del_arr[@]}" ; then
_not_in_use_by_others=false
__msg="The DocumentRoot directory \033[1m${_doc_root}\033[m is also used
by vhost configuration file \033[1m$(basename ${_val_arr[1]})\033[m. But this file
isn't marked for deletion. So also the DocumentRoot will not marked for deletion."
msg_exclude_doc_root_from_del_arr[${#msg_exclude_doc_root_from_del_arr[@]}]="$__msg"
break
fi
fi
done
if $_not_in_use_by_others ; then
if ! containsElement "$_doc_root" "${docroot_marked_for_del_arr[@]}" ; then
docroot_marked_for_del_arr+=("${_doc_root}")
fi
fi
done
#for _doc_root in ${docroot_req_for_del_arr[@]} ; do
# _tmp_files="$(grep -l -d skip -E "\s*DocumentRoot\s+\"?${_doc_root}" $apache_vhost_dir/* 2> /dev/null)"
# _not_in_use_by_others=true
# for _tmp_file in $_tmp_files ; do
# if ! containsElement "$(basename $_tmp_file)" "${vhost_file_marked_for_del_arr[@]}" ; then
# _not_in_use_by_others=false
#
# __msg="The DocumentRoot directory \033[37m\033[1m${_doc_root}\033[m is also
# used by vhost configuration file \033[33m${_tmp_file}\033[m.
# But this file isn't marked for deletion."
# msg_exclude_doc_root_from_del_arr[${#msg_exclude_doc_root_from_del_arr[@]}]="$__msg"
#
#
# break
# fi
# done
#
# if $_not_in_use_by_others ; then
# if ! containsElement "$_doc_root" "${docroot_marked_for_del_arr[@]}" ; then
# docroot_marked_for_del_arr+=("${_doc_root}")
# fi
# fi
##done
# - DocumentRoot directories requested for deletion
# -
echo ""
echo " DocumentRoot directories requested for deletion"
if [[ ${#docroot_req_for_del_arr[@]} -gt 0 ]]; then
for _doc_root in ${docroot_req_for_del_arr[@]} ; do
echo " $_doc_root"
done
else
echo -e " *** \033[33mNo DocumentRoot directories are requested for deletion\033[m ***"
fi
# - DocumentRoot directories marked for deletion
# -
echo ""
echo " DocumentRoot directories marked for deletion"
if [[ ${#docroot_marked_for_del_arr[@]} -gt 0 ]]; then
for _doc_root in ${docroot_marked_for_del_arr[@]} ; do
echo " $_doc_root"
done
else
echo -e " *** \033[33mNo DocumentRoot directories are marked for deletion\033[m ***"
fi
# -----
# - Warn about DocumentRoot directories that connot be deleted it is referenced
# - by vhost configuration files NOT marked for deletion.
# -----
CUR_IFS=$IFS
IFS=''
msg=""
declare -i _counter=1
if [[ ${#msg_exclude_doc_root_from_del_arr[@]} -gt 0 ]] ; then
for _msg in ${msg_exclude_doc_root_from_del_arr[@]} ; do
if [[ $_counter -eq 1 ]]; then
msg="$_msg"
else
msg="$msg\n\n $_msg"
fi
_counter=$((_counter+1))
done
warn "$msg"
fi
IFS=$CUR_IFS
echo ""
echo -e "\033[32m\033[1m------\033[m"
echo -e "Summary Databases"
echo -e "\033[32m\033[1m------\033[m"
# ==========
# - Databses
# ==========
# - Check if database can savely be marked for deletion. That is, if documentroot
# - is alaos marked for deletion.
# -
if [[ ${#database_req_for_del_arr[@]} -gt 0 ]]; then
_tmp_arr=()
for _val in "${database_req_for_del_arr[@]}" ; do
IFS=':' read -a _val_arr <<< "${_val}"
if containsElement "${_val_arr[1]}" "${docroot_marked_for_del_arr[@]}" ; then
if ! containsElement "${_val_arr[0]}" "${_tmp_arr[@]}" ; then
#if ! containsElement "${_val_arr[0]}:${_val_arr[1]}" "${database_marked_for_del_arr[@]}" ; then
database_marked_for_del_arr+=("${_val_arr[0]}:${_val_arr[1]}")
_tmp_arr+=("${_val_arr[0]}")
fi
else
__msg="Database \033[1m${_val_arr[0]}\033[m is in use by DocumentRoot directory \033[1m${_val_arr[1]}\033[m,
but this directory is NOT marked for deletion. So also the database is not marked for deletion"
msg_exclude_db_from_del_arr[${#msg_exclude_db_from_del_arr[@]}]="$__msg"
fi
done
fi
# - Databases(s) requested for deletion
# -
echo ""
echo " Databases(s) requested for deletion"
_printed_arr=()
if [[ ${#database_req_for_del_arr[@]} -gt 0 ]]; then
for _val in "${database_req_for_del_arr[@]}" ; do
IFS=':' read -a _val_arr <<< "${_val}"
if ! containsElement "${_val_arr[0]}" "${_printed_arr[@]}" ; then
echo " ${_val_arr[0]}"
fi
_printed_arr+=("${_val_arr[0]}")
done
else
echo -e " *** \033[33mNo Database is requested for deletion\033[m ***"
fi
# - Databases marked for deletion
# -
echo ""
echo " Database(s) marked for deletion"
if [[ ${#database_marked_for_del_arr[@]} -gt 0 ]]; then
for _val in "${database_marked_for_del_arr[@]}" ; do
IFS=':' read -a _val_arr <<< "${_val}"
echo " ${_val_arr[0]}"
done
else
echo -e " *** \033[33mNo Database is marked for deletion\033[m ***"
fi
# -----
# - Warn about Databases, which will not be marked for deletion.
# -----
CUR_IFS=$IFS
IFS=''
msg=""
declare -i _counter=1
if [[ ${#msg_exclude_db_from_del_arr[@]} -gt 0 ]] ; then
for _msg in ${msg_exclude_db_from_del_arr[@]} ; do
if [[ $_counter -eq 1 ]]; then
msg="$_msg"
else
msg="$msg\n\n $_msg"
fi
_counter=$((_counter+1))
done
warn "$msg"
fi
IFS=$CUR_IFS
echo ""
echo -e "\033[32m\033[1m------\033[m"
echo -e "Summary Domains - This may take some time, perhaps some minutes.."
echo -e "\033[32m\033[1m------\033[m"
# ==========
# - Domains
# ==========
# - Check if domain can savely be marked for deletion. That is, if no remaining
# - VHost configuration files contains hostname concerning that domain
# -
for domain in "${domain_req_for_del_arr[@]}" ; do
if containsElement "$domain" "${no_vhost_config_found[@]}" ; then
__msg="No VHost configuration present for domain \033[37m\033[1m${domain}\033[m. The domain is not
marked for deletion."
msg_exclude_domain_from_del_arr[${#msg_exclude_domain_from_del_arr[@]}]="$__msg"
continue
fi
_not_found=false
IFS=':' read -a _val_arr <<< "${vhost_file_domain_arr["$domain"]}"
for _vhost_file in "${_val_arr[@]}" ; do
if ! containsElement "$_vhost_file" "${vhost_file_marked_for_del_arr[@]}" ; then
_not_found=true
fi
done
if $_not_found ; then
__msg="At least VHost configuration File \033[33m${_vhost_file}\033[m for
domain \033[37m\033[1m${domain}\033[m is excluded from deletion. So also the domain
itself will not marked for deletion."
msg_exclude_domain_from_del_arr[${#msg_exclude_domain_from_del_arr[@]}]="$__msg"
else
sleep 1
zone="$(ssh -q -p $DNS_SSH_PORT \
-o BatchMode=yes \
-o StrictHostKeyChecking=no \
-i $DNS_SSH_KEY \
-l $DNS_SSH_USER \
$DNS_MASTER_SERVER "sudo $SCRIPT_get_domain_by_hostname $domain" 2> /dev/null)"
if [[ $? -gt 0 ]] ;then
if [[ $? -eq 10 ]] ; then
error "\033[1m$domain\033[m is not a valid hostname/domain"
elif [[ $? -eq 11 ]]; then
error "\033[1m$domain\033[m is not supported by \033[1m$domain\033[m."
elif [[ $? -eq 12 ]] ; then
error "\033[1m$domain\033[m is supported but \033[1m$domain\033[m is not the master."
else
error "Unknow error on accessing \033[1m$SCRIPT_get_domain_by_hostname\033[m on Nameserver \"$DNS_MASTER_SERVER\"!"
fi
fi
if [[ "$zone" = "$domain" ]]; then
domain_marked_for_del_arr+=("$domain")
else
_a_record="$(dig +short \@$DNS_MASTER_SERVER $domain A)"
_aaaa_record="$(dig +short \@$DNS_MASTER_SERVER $domain AAAA)"
if [[ -n "$_a_record" ]] || [[ -n "$_aaaa_record" ]] ; then
__msg="\033[37m\033[1m$domain\033[m itself is not a zone, but has an 'A' or 'AAAA' record on
Nameserver \033[37m\033[1m$DNS_MASTER_SERVER\033[m. $domain is not marked for deletion."
if [[ -n "$_a_record" ]]; then
A_record_marked_for_del_arr+=("$domain")
fi
if [[ -n "$_aaaa_record" ]]; then
AAAA_record_marked_for_del_arr+=("$domain")
fi
else
_a_record="$(dig +short $domain A)"
_aaaa_record="$(dig +short $domain AAAA)"
if [[ -n "$_a_record" ]] || [[ -n "$_aaaa_record" ]] ; then
__msg="\033[37m\033[1m$domain\033[m itself is not a zone, also no 'A' or 'AAAA' record was found
on Nameserver \033[37m\033[1m$DNS_MASTER_SERVER\033[m, but exist on an other another Nameserver.
$domain is not marked for deletion."
else
__msg="\033[37m\033[1m$domain\033[m itself is not a zone, also no 'A' or 'AAAA' record was found
on Nameserver \033[37m\033[1m$DNS_MASTER_SERVER\033[m. $domain is not marked for deletion."
fi
fi
msg_exclude_domain_from_del_arr[${#msg_exclude_domain_from_del_arr[@]}]="$__msg"
fi
fi
done
# - Domain(s) files requested for deletion
# -
echo ""
echo " Domain(s) requested for deletion"
if [[ ${#domain_req_for_del_arr[@]} -gt 0 ]]; then
for _domain in "${domain_req_for_del_arr[@]}" ; do
echo " $_domain"
done
else
echo -e " *** \033[33mNo Domain is requested for deletion\033[m ***"
fi
# - Domain(s) marked for deletion
# -
echo ""
echo " Domain(s) marked for deletion"
if [[ ${#domain_marked_for_del_arr[@]} -gt 0 ]]; then
for _domain in "${domain_marked_for_del_arr[@]}" ; do
echo " $_domain"
done
else
echo -e " *** \033[33mNo Domain is marked for deletion\033[m ***"
fi
# -----
# - Warn about Domains, which will not be marked for deletion.
# -----
CUR_IFS=$IFS
IFS=''
msg=""
declare -i _counter=1
if [[ ${#msg_exclude_domain_from_del_arr[@]} -gt 0 ]] ; then
for _msg in ${msg_exclude_domain_from_del_arr[@]} ; do
if [[ $_counter -eq 1 ]]; then
msg="$_msg"
else
msg="$msg\n\n $_msg"
fi
_counter=$((_counter+1))
done
warn "$msg"
fi
IFS=$CUR_IFS
echo ""
echo -e "\033[1m====================\033[m"
info "For each 'Summary' kategory, you will be requested to confirm deletions.
Type uppercase 'YES' to continue."
echo -e "\033[1m====================\033[m"
echo ""
echo -e -n "\033[1mContinue:\033[m "
read OK
echo ""
if [[ "$OK" != "YES" ]] ; then
fatal "Abort by user request - Answer as not 'YES'"
fi
apache_needs_restart=false
if [[ ${#vhost_file_marked_for_del_arr[@]} -gt 0 ]]; then
echo ""
echo -e "\033[32m------\033[m"
echo -e "Remove VHost Configuration from Webservice"
echo -e "\033[32m------\033[m"
for _vhost_file in ${vhost_file_marked_for_del_arr[@]} ; do
echo " $_vhost_file"
done
apache_needs_restart=true
OK=""
echo ""
echo "Type 'y' to continue 'n' or any other key to skip removal of VHost configurations"
echo
echo -e -n "\033[1mContinue removing VHost Configurations [y/n]:\033[m "
read OK
while [[ "X${OK}X" = "XX" ]] ; do
echo ""
echo -e -n "\033[1mContinue removing VHost Configurations [y/n]:\033[m "
read OK
done
if [[ "${OK,,}" = 'yes' ]] || [[ "${OK,,}" = 'y' ]]; then
echo ""
echo "" >> $main_log
echo "" >> $main_log
echo "# ======" >> $main_log
echo "# - Remove VHost Configuration from Webservice" >> $main_log
echo "# -">> $main_log
for _vhost_file in ${vhost_file_marked_for_del_arr[@]} ; do
echo "# - $_vhost_file" >> $main_log
done
echo "# ======" >> $main_log
echononl " Backup existing VHost Configuration directory"
if [[ -d "$apache_vhost_base_dir" ]]; then
echo "" >> $main_log
echo "# - Backup existing VHost Configuration directory" >> $main_log
echo "# -" >> $main_log
echo "cp -a \"$apache_vhost_base_dir" "${apache_vhost_base_dir}.$backup_date\"" >> $main_log
cp -a "$apache_vhost_base_dir" "${apache_vhost_base_dir}.$backup_date" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
fi
else
echo_skipped
fi
# - Respect apache2 from debian package system
# -
if [[ "$(basename $apache_vhost_base_dir)" = "sites-available" ]]; then
_delete_dir="${apache_vhost_base_dir}/DELETED"
else
_delete_dir="${apache_vhost_dir}/DELETED"
fi
echononl " Create sub-directory 'DELETED'"
if [[ ! -d "$_delete_dir" ]] ; then
echo "" >> $main_log
echo "# - Create sub-directory 'DELETED'" >> $main_log
echo "# -" >> $main_log
echo "mkdir \"$_delete_dir\"" >> $main_log
mkdir "$_delete_dir" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
else
echo_skipped
fi
echo ""
for _vhost_file in ${vhost_file_marked_for_del_arr[@]} ; do
# - Respect apache2 from debian package system
# -
_vhost_file_to_move="${apache_vhost_dir}/$_vhost_file"
if [[ "$(basename $apache_vhost_dir)" = "sites-enabled" ]]; then
_vhost_file_to_move="$(realpath ${apache_vhost_dir}/$_vhost_file)"
echo "" >> $main_log
echo "# - Delete Symlink '${apache_vhost_dir}/$_vhost_file'" >> $main_log
echo "# -" >> $main_log
echo "rm \"${apache_vhost_dir}/$_vhost_file\"" >> $main_log
echononl " Delete Symlink '${apache_vhost_dir}/$_vhost_file'"
rm "${apache_vhost_dir}/$_vhost_file" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
fi
echo "" >> $main_log
echo "# - Removing VHost configuration '$(basename $_vhost_file_to_move)'" >> $main_log
echo "# -" >> $main_log
echo "mv \"$_vhost_file_to_move\" \"$_delete_dir\"" >> $main_log
echononl " Removing VHost configuration \033[1m$(basename $_vhost_file_to_move)\033[m"
mv "$_vhost_file_to_move" "$_delete_dir" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
done # for _vhost_file in ${vhost_file_marked_for_del_arr[@]}
echo ""
fi
else
echo ""
echo -e "\033[32m------\033[m"
echo -e "*** \033[33mNo VHost configuration files are marked for deletion\033[m ***"
echo -e "\033[32m------\033[m"
fi
if [[ ${#docroot_marked_for_del_arr[@]} -gt 0 ]]; then
echo ""
echo -e "\033[32m------\033[m"
echo -e "Remove DocumentRoot directories"
echo -e "\033[32m------\033[m"
for _doc_root in ${docroot_marked_for_del_arr[@]} ; do
echo " $_doc_root"
done
OK=""
echo ""
echo "Type 'y' to continue 'n' or any other key to skip removal of DocumentRoot directories"
echo
echo -e -n "\033[1mContinue removing DocumentRoot directories [y/n]:\033[m "
read OK
while [[ "X${OK}X" = "XX" ]] ; do
echo ""
echo -e -n "\033[1mContinue removing DocumentRoot directories [y/n]:\033[m "
read OK
done
if [[ "${OK,,}" = 'yes' ]] || [[ "${OK,,}" = 'y' ]]; then
echo ""
echo "" >> $main_log
echo "" >> $main_log
echo "# ======" >> $main_log
echo "# - Remove DocumentRoot directories" >> $main_log
echo "# -">> $main_log
for _doc_root in ${docroot_marked_for_del_arr[@]} ; do
echo "# - $_doc_root" >> $main_log
done
echo "# ======" >> $main_log
_delete_dir="${apache_web_base_dir}/DELETED"
echononl " Create sub-directory 'DELETED'"
if [[ ! -d "$_delete_dir" ]] ; then
echo "" >> $main_log
echo "# - Create sub-directory 'DELETED'" >> $main_log
echo "# -" >> $main_log
echo "mkdir \"$_delete_dir\"" >> $main_log
mkdir "$_delete_dir" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
else
echo_skipped
fi
for _doc_root in ${docroot_marked_for_del_arr[@]} ; do
_dir_to_remove="$(dirname $_doc_root)"
# if [[ -h "$_dir_to_remove" ]]; then
#
# _symlink="$_dir_to_remove"
# _dir_to_remove="$(realpath $_dir_to_remove)"
#
# echo "" >> $main_log
# echo "# - Delete Symlink '${_symlink}'" >> $main_log
# echo "# -" >> $main_log
# echo "rm \"${_symlink}\"" >> $main_log
#
# echononl " Delete Symlink '${_symlink}'"
# rm "${_symlink}" > $tmp_log_file 2>&1
# if [[ $? -eq 0 ]]; then
# echo_ok
# else
# echo_failed
# error "$(cat $tmp_log_file)"
# echo "Error: $(cat $tmp_log_file)" >> $main_log
# fi
#
# fi
if [[ -e "${_delete_dir}/$(basename $_dir_to_remove)" ]] ; then
echo "" >> $main_log
echo "# - Backup existing file '${_delete_dir}/$(basename $_dir_to_remove)'" >> $main_log
echo "# -" >> $main_log
echo "mv \"${_delete_dir}/$(basename $_dir_to_remove)\" \"${_delete_dir}/$(basename $_dir_to_remove).$backup_date\"" >> $main_log
echononl " Backup existing file '${_delete_dir}/$(basename $_dir_to_remove)'"
mv "${_delete_dir}/$(basename $_dir_to_remove)" "${_delete_dir}/$(basename $_dir_to_remove).$backup_date" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
fi
echo "" >> $main_log
echo "# - Removing DocumentRoot directory '$(basename $_dir_to_remove)'" >> $main_log
echo "# -" >> $main_log
echo "mv \"$_dir_to_remove\" \"$_delete_dir\"" >> $main_log
echononl " Removing DocumentRoot directory \033[1m$(basename $_dir_to_remove)\033[m"
mv "$_dir_to_remove" "$_delete_dir" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
# if $(grep -q "$_dir_to_remove" $(apachectl configtest) 2> /dev/null) ; then
#
# error "Directory \033[1m$_dir_to_remove\033[m is used by another Website. So, moving
# directory \033[1m$(basename $_dir_to_remove)\033[m back to \033[1m$(dirname $_dir_to_remove)\033[m."
#
# echo "" >> $main_log
# echo "Error: Directory '$_dir_to_remove' is used by another Website." >> $main_log
#
# echo "" >> $main_log
# echo "# - Moving directory '$(basename $_dir_to_remove)' back to '$(dirname $_dir_to_remove)'" >> $main_log
# echo "# -" >> $main_log
# echo "mv \"${_delete_dir}/$(basename $_dir_to_remove)\" \"$(dirname $_dir_to_remove)\"" >> $main_log
# echononl " Moving directory \033[1m$(basename $_dir_to_remove)\033[m back."
# mv "${_delete_dir}/$(basename $_dir_to_remove)" "$(dirname $_dir_to_remove)" > $tmp_log_file 2>&1
# if [[ $? -eq 0 ]]; then
# echo_ok
# else
# echo_failed
# error "$(cat $tmp_log_file)"
# echo "Error: $(cat $tmp_log_file)" >> $main_log
# fi
#
# fi
done
fi
else
echo ""
echo -e "\033[32m------\033[m"
echo -e "*** \033[33mNo DocumentRoot directories are marked for deletion\033[m ***"
echo -e "\033[32m------\033[m"
fi
if [[ ${#database_marked_for_del_arr[@]} -gt 0 ]]; then
echo ""
echo -e "\033[32m------\033[m"
echo -e "Remove (MySQL) Databases"
echo -e "\033[32m------\033[m"
for _val in "${database_marked_for_del_arr[@]}" ; do
IFS=':' read -a _val_arr <<< "${_val}"
echo " ${_val_arr[0]}"
done
OK=""
echo ""
echo "Type 'y' to continue 'n' or any other key to skip removal of Databases"
echo
echo -e -n "\033[1mContinue removing Databases [y/n]:\033[m "
read OK
while [[ "X${OK}X" = "XX" ]] ; do
echo ""
echo -e -n "\033[1mContinue removing Databases [y/n]:\033[m "
read OK
done
if [[ "${OK,,}" = 'yes' ]] || [[ "${OK,,}" = 'y' ]]; then
echo ""
echo "" >> $main_log
echo "" >> $main_log
echo "# ======" >> $main_log
echo "# - Remove (MySQL) Databases" >> $main_log
echo "# -">> $main_log
for _val in "${database_marked_for_del_arr[@]}" ; do
IFS=':' read -a _val_arr <<< "${_val}"
echo "# - ${_val_arr[0]}" >> $main_log
done
echo "# ======" >> $main_log
_backup_dir="${apache_web_base_dir}/DELETED/$(basename $(dirname ${_val_arr[1]}))"
echononl " Create Backup directory '$_backup_dir'.."
if [[ ! -d "$_backup_dir" ]]; then
echo "" >> $main_log
echo "# - Create '$_backup_dir'" >> $main_log
echo "# -" >> $main_log
echo "mkdir \"$_backup_dir\"" >> $main_log
mkdir "$_backup_dir" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
else
echo_skipped
fi
for _val in "${database_marked_for_del_arr[@]}" ; do
IFS=':' read -a _val_arr <<< "${_val}"
echo "" >> $main_log
echo "# - Backup Backup Database '${_val_arr[0]}'" >> $main_log
echo "# -" >> $main_log
echo "mysqldump $MYSQL_CREDENTIAL_ARGS --opt ${_val_arr[0]} | gzip > \"${_backup_dir}/${_val_arr[0]}-${backup_date}.sql.gz\"" >> $main_log
echononl " Backup Database '${_val_arr[0]}'.."
mysqldump $MYSQL_CREDENTIAL_ARGS --opt ${_val_arr[0]} 2> $tmp_log_file | gzip > "${_backup_dir}/${_val_arr[0]}-${backup_date}.sql.gz" 2>> $tmp_log_file
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
echo "" >> $main_log
echo "# - Remove Database '${_val_arr[0]}'" >> $main_log
echo "# -" >> $main_log
echo "mysql $MYSQL_CREDENTIAL_ARGS -N -s -A -e \"DROP DATABASE ${_val_arr[0]}\"" >> $main_log
echononl " Remove Database '${_val_arr[0]}'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -A -e "DROP DATABASE ${_val_arr[0]}" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
echo "" >> $main_log
echo "# - Cleanup table mysql.db from Database '${_val_arr[0]}'" >> $main_log
echo "# -" >> $main_log
echo "mysql $MYSQL_CREDENTIAL_ARGS -N -s -A -e \"USE mysql; DELETE FROM db WHERE Db = ' ${_val_arr[0]}'\"" >> $main_log
echononl " Cleanup table mysql.db from Database '${_val_arr[0]}'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -A -e "USE mysql; DELETE FROM db WHERE Db = '${_val_arr[0]}'" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
# - Try to remove concerning User from table mysql.user
# -
# - do it only if the user meets the following facts
# - - user's name is the same as the database name
# - - user does not appear in table mysql.db
# -
_count_user_tbl_user=$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -A -e "USE mysql; SELECT count(User) from user WHERE User = '${_val_arr[0]}'" )
if [[ $? -eq 0 ]] && [[ $_count_user_tbl_user -eq 1 ]]; then
echononl " Drop User '${_val_arr[0]}' from table 'mysql.user'"
_count_user_tbl_db=$(mysql $MYSQL_CREDENTIAL_ARGS -N -s -A -e "USE mysql; SELECT count(User) from db WHERE User = '${_val_arr[0]}'")
if [[ $? -eq 0 ]] && [[ $_count_user_tbl_db -eq 0 ]]; then
echo "" >> $main_log
echo "# - Drop User '${_val_arr[0]}' from table 'mysql.user'" >> $main_log
echo "# -" >> $main_log
echo "mysql $MYSQL_CREDENTIAL_ARGS -N -s -A -e \"USE mysql; DELETE FROM user WHERE User = '${_val_arr[0]}'\"" >> $main_log
echononl " Drop User '${_val_arr[0]}' from table 'mysql.user'"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -A -e "USE mysql; DELETE FROM user WHERE User = '${_val_arr[0]}'" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
else
echo_skipped
fi # if [[ $? -eq 0 ]] && [[ $_count_user_tbl_db -eq 0 ]]
fi # if [[ $? -eq 0 ]] && [[ $_count_user_tbl_user -eq 1 ]]; then
echo "" >> $main_log
echo "# - FLUSH PRIVILEGES" >> $main_log
echo "# -" >> $main_log
echo "mysql $MYSQL_CREDENTIAL_ARGS -N -s -A -e \"FLUSH PRIVILEGES\"" >> $main_log
echononl " FLUSH PRIVILEGES"
mysql $MYSQL_CREDENTIAL_ARGS -N -s -A -e "FLUSH PRIVILEGES" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
done
fi
else
echo ""
echo -e "\033[32m------\033[m"
echo -e "*** \033[33mNo Database is marked for deletion\033[m ***"
echo -e "\033[32m------\033[m"
fi
if [[ ${#domain_marked_for_del_arr[@]} -gt 0 ]]; then
echo ""
echo -e "\033[32m------\033[m"
echo -e "Remove Domain - this may take some time"
echo -e "\033[32m------\033[m"
for _domain in "${domain_marked_for_del_arr[@]}" ; do
echo " $_domain"
done
OK=""
echo ""
echo "Type 'y' to continue 'n' or any other key to skip removal of Domains"
echo
echo -e -n "\033[1mContinue removing Domains [y/n]:\033[m "
read OK
while [[ "X${OK}X" = "XX" ]] ; do
echo ""
echo -e -n "\033[1mContinue removing Domains [y/n]:\033[m "
read OK
done
if [[ "${OK,,}" = 'yes' ]] || [[ "${OK,,}" = 'y' ]]; then
echo ""
echo "" >> $main_log
echo "" >> $main_log
echo "# ======" >> $main_log
echo "# - Remove Domains" >> $main_log
echo "# -">> $main_log
for _domain in "${domain_marked_for_del_arr[@]}" ; do
echo "# - $_domain" >> $main_log
done
echo "# ======" >> $main_log
for _domain in "${domain_marked_for_del_arr[@]}" ; do
echo "" >> $main_log
echo "# - Remove Zone '$_domain' from Nameserver $DNS_MASTER_SERVER (master)" >> $main_log
echo "# -" >> $main_log
cat <<EOF >> $main_log
ssh -q -p $DNS_SSH_PORT \\
-o BatchMode=yes \\
-o StrictHostKeyChecking=no \\
-i $DNS_SSH_KEY \\
-l $DNS_SSH_USER \\
$DNS_MASTER_SERVER "sudo $SCRIPT_remove_master_domain -d $_domain"
EOF
echononl " Remove Zone '$_domain' from '$DNS_MASTER_SERVER' (master)"
ssh -q -p $DNS_SSH_PORT \
-o BatchMode=yes \
-o StrictHostKeyChecking=no \
-i $DNS_SSH_KEY \
-l $DNS_SSH_USER \
$DNS_MASTER_SERVER "sudo $SCRIPT_remove_master_domain -d $_domain" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
if $include_slave_dns ; then
echo "" >> $main_log
echo "# - Remove Zone '$_domain' from Nameserver $DNS_SLAVE_SERVER (slave)" >> $main_log
echo "# -" >> $main_log
cat <<EOF >> $main_log
ssh -q -p $DNS_SSH_PORT \\
-o BatchMode=yes \\
-o StrictHostKeyChecking=no \\
-i $DNS_SSH_KEY \\
-l $DNS_SSH_USER \\
$DNS_SLAVE_SERVER "sudo $SCRIPT_remove_slave_domain -d $_domain"
EOF
echononl " Remove Zone '$_domain' from '$DNS_SLAVE_SERVER' (slave)"
ssh -q -p $DNS_SSH_PORT \
-o BatchMode=yes \
-o StrictHostKeyChecking=no \
-i $DNS_SSH_KEY \
-l $DNS_SSH_USER \
$DNS_SLAVE_SERVER "sudo $SCRIPT_remove_slave_domain -d $_domain" > $tmp_log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
fi # if $include_slave_dns
done # for _domain in "${domain_marked_for_del_arr[@]}"
fi
else
echo ""
echo -e "\033[32m------\033[m"
echo -e "*** \033[33mNo Domain is marked for deletion\033[m ***"
echo -e "\033[32m------\033[m"
fi
#if [[ ${#A_record_marked_for_del_arr[@]} -gt 0 ]] || [[ ${#AAAA_record_marked_for_del_arr[@]} -gt 0 ]] ; then
# echo ""
# echo -e "\033[32m------\033[m"
# echo -e "Remove Records from zone file"
# echo -e "\033[32m------\033[m"
#
# for _record in "${A_record_marked_for_del_arr[@]}" ; do
# echo " A Record for $_domain"
# done
# for _record in "${AAAA_record_marked_for_del_arr[@]}" ; do
# echo " AAAA Record for $_domain"
# done
#
# OK=""
# echo ""
# echo "Type 'y' to continue 'n' or any other key to skip removal of Domains"
# echo
# echo -e -n "\033[1mContinue removing records from zone file [y/n]:\033[m "
# read OK
# while [[ "X${OK}X" = "XX" ]] ; do
# echo ""
# echo -e -n "\033[1mContinue removing Domains [y/n]:\033[m "
# read OK
# done
#
# if [[ "${OK,,}" = 'yes' ]] || [[ "${OK,,}" = 'y' ]]; then
# echo ""
# for _record in "${A_record_marked_for_del_arr[@]}" ; do
# echo -e " Removing A Record for \033[1m$_domain\033[m"
# done
# for _record in "${AAAA_record_marked_for_del_arr[@]}" ; do
# echo -e " Removing AAAA Record for \033[1m$_domain\033[m"
# done
#
# fi
#
##else
## echo ""
## echo -e "*** \033[33mNo Domain is marked for deletion\033[m ***"
#fi
if $apache_needs_restart ; then
echo ""
echo -e "\033[32m------\033[m"
echo -e "Restart Apache Webservice"
echo -e "\033[32m------\033[m"
OK=""
echo ""
echo "Type 'y' to continue 'n' or any other key to skip removal of Domains"
echo
echo -e -n "\033[1mRestart Apache Webservice [y/n]:\033[m "
read OK
while [[ "X${OK}X" = "XX" ]] ; do
echo ""
echo -e -n "\033[1mRestart Apache Webservice [y/n]:\033[m "
read OK
done
if [[ "${OK,,}" = 'yes' ]] || [[ "${OK,,}" = 'y' ]]; then
echo "" >> $main_log
echo "# - Graceful Restart Apache Webservice" >> $main_log
echo "# -" >> $main_log
echo "apachectl graceful" >> $main_log
echo ""
echononl " Graceful restart Apache Webservice.."
apachectl graceful
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat $tmp_log_file)"
echo "Error: $(cat $tmp_log_file)" >> $main_log
fi
fi # if [[ "${OK,,}" = 'yes' ]] || [[ "${OK,,}" = 'y' ]]; then
fi # if $apache_needs_restart
echo ""
clean_up