#!/usr/bin/env bash CUR_IFS="$IFS" script_name="$(basename $(realpath $0))" script_dir="$(dirname $(realpath $0))" conf_dir="${script_dir}/conf" declare -a unsorted_website_arr declare -a website_arr log_file="$(mktemp)" backup_date=$(date +%Y-%m-%d-%H%M) # ============= # --- Some functions # ============= clean_up() { if [[ -f "$_backup_crontab_file" ]]; then blank_line echononl "(Re)Install previously saved crontab from '$_backup_crontab_file'.." crontab $_backup_crontab_file >> $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed error "$(cat $log_file)" fi fi # Perform program exit housekeeping rm -f $log_file blank_line exit $1 } is_number() { return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1); # - also possible # - #[[ ! -z "${1##*[!0-9]*}" ]] && return 0 || return 1 #return $([[ ! -z "${1##*[!0-9]*}" ]]) } 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$$ } echo_done() { if $terminal ; then echo -e "\033[75G[ \033[32mdone\033[m ]" else echo " [ done ]" fi } echo_ok() { if $terminal ; then echo -e "\033[75G[ \033[32mok\033[m ]" else echo " [ ok ]" fi } echo_warning() { if $terminal ; then echo -e "\033[75G[ \033[33m\033[1mwarn\033[m ]" else echo " [ warning ]" fi } echo_failed(){ if $terminal ; then echo -e "\033[75G[ \033[1;31mfailed\033[m ]" else echo ' [ failed! ]' fi } echo_skipped() { if $terminal ; then echo -e "\033[75G[ \033[37mskipped\033[m ]" else echo " [ skipped ]" fi } fatal (){ echo "" echo "" if $terminal ; then echo -e "\t[ \033[31m\033[1mFatal\033[m ]: \033[37m\033[1m$*\033[m" echo "" echo -e "\t\033[31m\033[1m Script will be interrupted..\033[m\033[m" else echo "fatal: $*" echo "Script will be interrupted.." fi clean_up 1 } error(){ echo "" if $terminal ; then echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*" else echo "Error: $*" fi echo "" } warn (){ echo "" if $terminal ; then echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" else echo "Warning: $*" fi echo "" } info (){ echo "" if $terminal ; then echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*" else echo "Info: $*" fi echo "" } detect_os_1 () { if $(which lsb_release > /dev/null 2>&1) ; then os_dist="$(lsb_release -i | awk '{print tolower($3)}')" os_version="$(lsb_release -r | awk '{print tolower($2)}')" os_codename="$(lsb_release -c | awk '{print tolower($2)}')" if [[ "$os_dist" = "debian" ]]; then if $(echo "$os_version" | grep -q '\.') ; then os_version=$(echo "$os_version" | cut --delimiter='.' -f1) fi fi elif [[ -e "/etc/os-release" ]]; then . /etc/os-release os_dist=$ID os_version=${VERSION_ID} fi # remove whitespace from os_dist and os_version os_dist="${os_dist// /}" os_version="${os_version// /}" } ## - 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 } blank_line() { if $terminal ; then echo "" fi } # ---------- # - Jobhandling # ---------- # - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM # - trap clean_up SIGHUP SIGINT SIGTERM # ---------- # - Some checks .. # ---------- # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true else terminal=false fi # ---------- # Read Configurations from $conf_file # ---------- # ========== # - Begin Main Script # ========== # ---------- # - Headline # ---------- clear if $terminal ; then echo "" echo -e "\033[1m----------\033[m" echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m" echo -e "\033[1m----------\033[m" fi # - Determin httpd binary # - _httpd_binary="`which httpd`" if [ -z "$_httpd_binary" ]; then _httpd_binary="$(ps -axu | grep httpd | grep -e "^root" | grep -v grep | awk '{print$11}')" if [ -z "$_httpd_binary" ]; then if [ -x "/usr/local/apache2/bin/httpd" ]; then _httpd_binary="/usr/local/apache2/bin/httpd" fi fi fi # - Determin ServerRoot Directory # - apache_base_dir=`$_httpd_binary -t -D DUMP_RUN_CFG | grep ServerRoot | awk '{print$2}' | tr -d '"'` if [ "`realpath /usr/local/apache2`" = "$apache_base_dir" ]; then apache_base_dir="/usr/local/apache2" _apache_base_dir_realpath="`realpath $apache_base_dir`" elif [ -z "$apache_base_dir" ]; then if [ -d "`realpath /usr/local/apache2`" ];then apache_base_dir="/usr/local/apache2" _apache_base_dir_realpath="`realpath $apache_base_dir`" fi else _apache_base_dir_realpath=$apache_base_dir fi if [ -z "$apache_base_dir" ];then _base_webserver_info_needed=true else _pass_apache_base_dir=true fi # - Determine the installed different PHP major versions # - __major_php_verisons="" _php_installation_dirs=`find /usr/local -mindepth 1 -maxdepth 1 -type l -name "*php-*" -print | sort` for dir in $_php_installation_dirs ; do _major_version="${dir##*-}" __major_php_verisons="$__major_php_verisons $_major_version" done _major_php_verisons=`echo "$__major_php_verisons" | sed 's/^ *//'` __vhost_base_dir=${apache_base_dir}/conf/vhosts echo "" echo "" echo -e "\033[32m--\033[m" echo "" echo "Insert Directory where the vhost configuration file should stay.." echo "" echo "" _vhost_base_dir= while [ "X$_vhost_base_dir" = "X" ] ; do echononl "VHost Base Directory [$__vhost_base_dir]: " read _vhost_base_dir if [ "X$_vhost_base_dir" = "X" ]; then _vhost_base_dir=$__vhost_base_dir fi if [ ! -d "$_vhost_base_dir" ];then echo -e "\n\t\033[1;33mDirectory \"${_vhost_base_dir}\" not found! Try again..\033[m\n" _vhost_base_dir="" fi done echo "" echo -e " \033[32m\033[1mParameter Summary:\033[m" echo "" echo " PHP installed versions.............: $_major_php_verisons" echo "" echo " Apache VHost directory.............: $_vhost_base_dir" echo "" echononl " \033[33mContinue with this parameters? [\033[1myes/no\033[m]: " read OK while [[ "${OK,,}" != "yes" ]] && [[ "${OK,,}" != "no" ]] ; do echononl " \033[33mWrong entry!\033[m [\033[1myes/no\033[m]: " read OK done [[ "${OK,,}" = "yes" ]] || fatal "Canceled by user input." # ---------- # - Some pre-script tasks .. # ---------- if $terminal ; then echo "" echo "" echo -e " \033[32m-----\033[m" echo -e " \033[1mDoing some pre-script tasks ..\033[m" echo -e " \033[32m-----\033[m" echo "" fi echononl " Backup vhost directory \033[1m${_vhost_base_dir}\033[m" cp -a "${_vhost_base_dir}" "${_vhost_base_dir}.${backup_date}" > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed error "$(cat $log_file)" fi # ---------- # - Main part of script # ---------- if $terminal ; then echo "" echo "" echo -e " \033[32m-----\033[m" echo -e " \033[1mMain part of script ..\033[m" echo -e " \033[32m-----\033[m" fi _changed=false for __version in ${_major_php_verisons} ; do if $terminal ; then echo "" echo -e " Check socket path for PHP version $__version" echo "" fi _changed_version=false if [[ -S "/tmp/php-${__version}-fpm.www.sock" ]]; then _socket_path="/tmp" elif [[ -S "/run/php/php-${__version}-fpm.www.sock" ]]; then _socket_path="/run/php" else warn "No PHP socket file found for version $__version .." continue fi for _file in $(ls -d ${_vhost_base_dir}/*) ; do if [[ -d "$_file" ]] ; then continue fi if [[ -h "$_file" ]]; then continue fi if grep -q -e "${__version}-fpm.www.sock" "$_file" > /dev/null 2>&1 ; then if ! grep -q -e "${_socket_path}/php-${__version}-fpm.www.sock" "$_file" > /dev/null 2>&1 ; then echononl " Change vhost configuration \033[1m$(basename "$_file")\033[m" perl -i -n -p -e "s#(.+proxy:unix:).+${__version}-fpm.www.sock(\|.*)#\1${_socket_path}/php-${__version}-fpm.www.sock\2#" $_file > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed error "$(cat $log_file)" fi _changed=true _changed_version=true fi fi done if ! $_changed_version ; then echo -e " -- \033[33mNothing to do\033[m --" fi done # ---------- # - Some post-script tasks .. # ---------- if $terminal ; then echo "" echo "" echo -e " \033[32m-----\033[m" echo -e " \033[1mDoing some post-script tasks ..\033[m" echo -e " \033[32m-----\033[m" echo "" fi echononl " Restart apache webservice.." if $_changed ; then systemctl restart apache2 > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok _changed=true else echo_failed error "$(cat $log_file)" fi else echo_skipped echo "" echo " Nothing has changed" echononl " Remove previously created backup of '${_vhost_base_dir}'." rm -rf "${_vhost_base_dir}.${backup_date}" > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok _changed=true else echo_failed error "$(cat $log_file)" fi fi clean_up 0