From 2e3a89908af773070006ef7051c3e51fa68793ef Mon Sep 17 00:00:00 2001 From: Christoph Date: Sun, 13 Aug 2017 18:37:34 +0200 Subject: [PATCH] handle_domain_on_webserver.sh: first version ready for usage. --- .gitignore | 2 + conf/handle_domain_on_webserver.conf.sample | 119 ++ delete_domain_related_configurations.sh | 1 + handle_domain_on_webserver.sh | 1231 ++++++++++++++++++- 4 files changed, 1299 insertions(+), 54 deletions(-) create mode 100644 conf/handle_domain_on_webserver.conf.sample create mode 120000 delete_domain_related_configurations.sh diff --git a/.gitignore b/.gitignore index 3fdddbe..3b4fd2f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /BAK/* +*.log /conf/*.conf +*.swp diff --git a/conf/handle_domain_on_webserver.conf.sample b/conf/handle_domain_on_webserver.conf.sample new file mode 100644 index 0000000..1786daf --- /dev/null +++ b/conf/handle_domain_on_webserver.conf.sample @@ -0,0 +1,119 @@ +## =================================================================== +## - Configuration File for "handle_domain_on_webserver.sh" Script +## =================================================================== + +# - Primary Nameserver which supports NS Records for websites on this server +# - +DNS_SERVER="" + +# - Secondary Nameserver which supports NS Records for websites on this server +# - +# - Leave empty if slave nameserver should be omitted. +# - +#DNS_SLAVE_SERVER="" + + + +# ----- +# - How to access the namesever via ssh? +# - +# - Note: +# - Take care to provide an entry 'DNS_SSH_USER' for nameservers in his +# - authorized_keys file. +# - +# - The ssh-key must not have a password! Otherwise this script will +# - not work! +# ----- + +# - DNS_SSH_USER +# - +# - Defaults to 'manage-bind' +# - +#DNS_SSH_USER="manage-bind" + +# - DNS_SSH_PORT +# - +# - Defaults to '22' +# - +#DNS_SSH_PORT=22 + +# - DNS_SSH_KEY +# - +# - Defaults to '/root/.ssh/id_rsa-dns1' +# - +#DNS_SSH_KEY=/root/.ssh/id_rsa-dns1 + + +# ----- +# - Scripts used on nameserver. +# - +# - Note: +# - The scripts must be accessable by dane_ssh_user. This +# - ca be realised by adding a concerning entry into sudo file +# - +# - for example: +# - manage-bind ALL=(root)NOPASSWD:/root/bin/bind/bind_* +# ------ + +# - SCRIPT_get_domain_by_hostname +# - +# - The full path to the script on the remote host. +# - +# - Defaults to '/root/bin/bind/bind_get_domain_by_hostname.sh' +# - +#SCRIPT_get_domain_by_hostname="/root/bin/bind/bind_get_domain_by_hostname.sh" + +# - SCRIPT_remove_master_domain +# - +# - The full path to the script on the remote host. +# - +# - defaults to '/root/bin/bind/bind_remove_domain_on_master.sh' +# - +#SCRIPT_remove_master_domain="/root/bin/bind/bind_remove_domain_on_master.sh" + +# - SCRIPT_remove_slave_domain +# - +# - The full path to the script on the remote host. +# - +# - defaults to '/root/bin/bind/bind_remove_domain_on_slave.sh' +# - +#SCRIPT_remove_slave_domain="/root/bin/bind/bind_remove_domain_on_slave.sh" + + +# ----- +# - How to access the database service? +# ----- + +# - MYSQL_CREDENTIAL_ARGS +# - +# - Giving password on command line is insecure an sind mysql 5.5 +# - you will get a warning doing so. +# - +# - Reading username/password fro file ist also possible, using MySQL/MariaDB +# - commandline parameter '--defaults-file'. +# - +# - Since Mysql Version 5.6, you can read username/password from +# - encrypted file. +# - +# - Create (encrypted) option file: +# - $ mysql_config_editor set --login-path=local --socket=/tmp/mysql.sock --user=root --password +# - $ Password: +# - +# - Create (encrypted) option file: +# - $ mysql_config_editor set --login-path=remote --host= --user= --password +# - $ Password: +# - +# - To see what mysql_config_editor wrote to the .mylogin.cnf file, use the print command: +# - mysql_config_editor print --all +# - +# - Use of option file: +# - $ mysql --login-path=local ... +# - +# - Example +# - mysql_credential_args="--login-path=local" +# - mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf" (Debian default) +# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf" +# - +# - Defaults to '--login-path=local' +# - +#MYSQL_CREDENTIAL_ARGS="--login-path=local" diff --git a/delete_domain_related_configurations.sh b/delete_domain_related_configurations.sh new file mode 120000 index 0000000..ffd30eb --- /dev/null +++ b/delete_domain_related_configurations.sh @@ -0,0 +1 @@ +handle_domain_on_webserver.sh \ No newline at end of file diff --git a/handle_domain_on_webserver.sh b/handle_domain_on_webserver.sh index 9960df1..ee7e02f 100755 --- a/handle_domain_on_webserver.sh +++ b/handle_domain_on_webserver.sh @@ -1,16 +1,23 @@ #!/usr/bin/env bash -log_file="$(mktemp)" +# ------------- +# - TODO: +# - - Support postgres databases +# - - Deleting maildomain on mailserver +# ------------- -only_check_domains=false -print_warnings=true +working_dir="$(dirname $(realpath $0))" +conf_file="${working_dir}/conf/handle_domain_on_webserver.conf" +log_dir="${working_dir}/log" -if [[ "$(basename $0)" = "check_domain_on_webserver.sh" ]] ; then - only_check_domains=true -elif [[ "$(basename $0)" = "show_domain_on_webserver.sh" ]] ; then - only_check_domains=true - print_warnings=false -fi +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 @@ -25,7 +32,7 @@ fi clean_up() { # Perform program exit housekeeping - rm -f $log_file + rm -f $tmp_log_file exit $1 } @@ -111,6 +118,19 @@ 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 @@ -133,14 +153,128 @@ if [[ -d "/usr/local/apache2/conf/vhosts" ]]; then else _apache_vhost_dir="/usr/local/apache2/conf/vhosts" fi -elif [[ -d "/etc/apache2/sites-available" ]]; then - _apache_vhost_dir="/etc/apache2/sites-available" +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 "" @@ -196,34 +330,156 @@ else 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 -n " Domain(s)............................: " +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 "$_domain" + echo -e "\033[1m$_domain\033[m" else - echo " $_domain" + echo -e " \033[1m$_domain\033[m" fi counter=$((counter+1)) done unset counter echo "" -echo " Apache Vhosts Directory..............: $apache_vhost_dir" -echo "" -echo "" -warn "To continue with te above Setting type uppercase 'YES'." -echo -n "Continue: " -read OK -echo "" -if [[ "$OK" != "YES" ]] ; then - fatal "Abort by user request - Answer as not 'YES'" +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 # ----- @@ -275,11 +531,16 @@ 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 @@ -296,6 +557,7 @@ for domain in "${domain_req_for_del_arr[@]}" ; do documentroot_arr=() database_arr=() + site_cms_arr=() msg_missing_database_arr=() msg_hostname_in_use_arr=() @@ -457,10 +719,12 @@ for domain in "${domain_req_for_del_arr[@]}" ; do # --- - # - Get databases + # - 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" @@ -492,6 +756,8 @@ for domain in "${domain_req_for_del_arr[@]}" ; do _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 @@ -549,11 +815,21 @@ for domain in "${domain_req_for_del_arr[@]}" ; do if $_found ; then database_arr+=("${database}:${_doc_root}") - database_req_for_del_arr+=("${database}") + #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 @@ -668,6 +944,13 @@ for domain in "${domain_req_for_del_arr[@]}" ; do 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 @@ -693,15 +976,6 @@ for domain in "${domain_req_for_del_arr[@]}" ; do fi IFS=$CUR_IFS - # - !! must be placed outside domain-loop - # - - #if [[ ${#documentroot_in_use_arr[@]} -gt 0 ]] ; then - # for _val in ${documentroot_in_use_arr[@]} ; do - # IFS=':' read -a _val_arr <<< "${_val}" - # echo "${_val_arr[0]} is also in use at VHost configuration file ${_val_arr[1]}!" - # done - #fi - # ----- # - Warnings current database(s) @@ -729,9 +1003,9 @@ for domain in "${domain_req_for_del_arr[@]}" ; do done -if $only_check_domains ; then +if $only_check || $only_show ; then - rm -f $log_file + rm -f $tmp_log_file echo "" clean_up fi @@ -818,7 +1092,7 @@ IFS=$CUR_IFS echo "" echo -e "\033[32m------\033[m" -echo -e "Summary DocumentRoot directories" +echo -e "Summary DocumentRoot directories - This may take some time" echo -e "\033[32m------\033[m" # ========== @@ -830,30 +1104,73 @@ echo -e "\033[32m------\033[m" # - 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 - _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 + _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[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="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 - 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 + + 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 # - @@ -905,9 +1222,95 @@ IFS=$CUR_IFS echo "" echo -e "\033[32m\033[1m------\033[m" -echo -e "Summary Domains" +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 @@ -937,7 +1340,55 @@ for domain in "${domain_req_for_del_arr[@]}" ; do itself will not marked for deletion." msg_exclude_domain_from_del_arr[${#msg_exclude_domain_from_del_arr[@]}]="$__msg" else - domain_marked_for_del_arr+=("$domain") + 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 @@ -989,6 +1440,678 @@ if [[ ${#msg_exclude_domain_from_del_arr[@]} -gt 0 ]] ; then fi IFS=$CUR_IFS -rm -f $log_file +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 <> $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 <> $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