From 80656a82695c655a01884fc175c042c71c795291 Mon Sep 17 00:00:00 2001 From: Christoph Date: Wed, 31 Jan 2024 12:39:51 +0100 Subject: [PATCH] sync_from_old_server.sh: add functionality for calling as cronjob. --- .sync_from_old_server.sh.00.swp | Bin 0 -> 16384 bytes sync_from_old_server.sh | 332 ++++++---- sync_from_old_server.sh.00 | 1099 +++++++++++++++++++++++++++++++ 3 files changed, 1321 insertions(+), 110 deletions(-) create mode 100644 .sync_from_old_server.sh.00.swp create mode 100755 sync_from_old_server.sh.00 diff --git a/.sync_from_old_server.sh.00.swp b/.sync_from_old_server.sh.00.swp new file mode 100644 index 0000000000000000000000000000000000000000..1dd630bdfb851439c708b565a020bfafbc88a7a2 GIT binary patch literal 16384 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 /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 + if $terminal ; then + echo X\\c > /tmp/shprompt$$ + if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then + echo -e -n "$*\\c" 1>&2 + else + echo -e -n "$*" 1>&2 + fi + rm /tmp/shprompt$$ fi - rm /tmp/shprompt$$ } fatal(){ + echo "" - echo -e "fatal error: $*" + if $terminal ; then + echo -e " [ \033[31m\033[1mFatal\033[m ]: $*" + echo "" + echo -e " \033[31m\033[1mScript was interupted\033[m!" + else + echo " [ Fatal ]: $*" + echo "" + echo " Script was terminated...." + fi echo "" - echo -e "\t\033[31m\033[1mInstalllation will be interrupted\033[m\033[m" - echo "" - exit 1 + clean_up 1 } error(){ echo "" - echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*" + if $terminal ; then + echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*" + else + echo "[ Error ]: $*" + fi echo "" } warn (){ echo "" - echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + if $terminal ; then + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + else + echo "[ Warning ]: $*" + fi echo "" } +warn_only_terminal () { + if $terminal ; then + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" + fi +} + info (){ - echo "" - echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*" - echo "" + if $terminal ; then + echo "" + echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*" + echo "" + fi } echo_done() { - echo -e "\033[80G[ \033[32mdone\033[m ]" + if $terminal ; then + echo -e "\033[80G[ \033[32mdone\033[m ]" + fi } echo_ok() { - echo -e "\033[80G[ \033[32mok\033[m ]" + if $terminal ; then + echo -e "\033[80G[ \033[32mok\033[m ]" + fi } echo_ok() { - echo -e "\033[80G[ \033[32mok\033[m ]" + if $terminal ; then + echo -e "\033[80G[ \033[32mok\033[m ]" + fi } echo_warning() { - echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]" + if $terminal ; then + echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]" + fi } echo_failed(){ - echo -e "\033[80G[ \033[1;31mfailed\033[m ]" + if $terminal ; then + echo -e "\033[80G[ \033[1;31mfailed\033[m ]" + fi } echo_skipped() { - echo -e "\033[80G[ \033[33m\033[1mskipped\033[m ]" + if $terminal ; then + echo -e "\033[80G[ \033[33m\033[1mskipped\033[m ]" + fi +} + +blank_line() { + if $terminal ; then + echo "" + fi } detect_mysql_version () { @@ -207,34 +272,38 @@ detect_mysql_version () { elif [[ -d "/usr/local/mysql" ]] && [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ mariadb- ]]; then MYSQL_CUR_DISTRIBUTION="MariaDB" else - error "MySQL Instalation found, but cannot determin the distribution!" + if $terminal ; then + error "MySQL Instalation found, but cannot determin the distribution!" - MYSQL_CUR_DISTRIBUTION= - echo "" - echo " Select the MySQL distribution to install." - echo "" - echo " [1] MySQL (the original community edition)" - echo " [2] Percona Server for MySQL" - echo " [3] MariaDB" - echo "" - echononl " Eingabe [1/2/3]: " + MYSQL_CUR_DISTRIBUTION= + echo "" + echo " Select the MySQL distribution to install." + echo "" + echo " [1] MySQL (the original community edition)" + echo " [2] Percona Server for MySQL" + echo " [3] MariaDB" + echo "" + echononl " Eingabe [1/2/3]: " - while [ "$MYSQL_CUR_DISTRIBUTION" != "MySQL" -a "$MYSQL_CUR_DISTRIBUTION" != "MariaDB" -a "$MYSQL_CUR_DISTRIBUTION" != "Percona" ];do - read OPTION - case $OPTION in - 1) MYSQL_CUR_DISTRIBUTION="MySQL" - ;; - 2) MYSQL_CUR_DISTRIBUTION="Percona" - ;; - 3) MYSQL_CUR_DISTRIBUTION="MariaDB" - ;; - *) echo "" - echo -e "\tFalsche Eingabe ! [ 1 = MySQL ; 2 = Percona ; 3 = MariaDB ]" - echo "" - echononl " Eingabe:" - ;; - esac - done + while [ "$MYSQL_CUR_DISTRIBUTION" != "MySQL" -a "$MYSQL_CUR_DISTRIBUTION" != "MariaDB" -a "$MYSQL_CUR_DISTRIBUTION" != "Percona" ];do + read OPTION + case $OPTION in + 1) MYSQL_CUR_DISTRIBUTION="MySQL" + ;; + 2) MYSQL_CUR_DISTRIBUTION="Percona" + ;; + 3) MYSQL_CUR_DISTRIBUTION="MariaDB" + ;; + *) echo "" + echo -e "\tFalsche Eingabe ! [ 1 = MySQL ; 2 = Percona ; 3 = MariaDB ]" + echo "" + echononl " Eingabe:" + ;; + esac + done + else + fatal "MySQL Instalation found, but cannot determin the distribution!" + fi fi MYSQL_VERSION="$(echo $_MYSQLD_VERSION | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?" | head -n 1)" @@ -255,6 +324,21 @@ service_exists() { } + +# ---------- +# - Some checks .. +# ---------- + +# - Running in a terminal? +# - +if [[ -t 1 ]] ; then + terminal=true +else + terminal=false +fi +terminal=false + + # ---------- @@ -273,7 +357,7 @@ else systemd_exists=true fi -echo "" +blank_line echononl " Test ssh connection to $old_server_ip" @@ -293,16 +377,21 @@ if [[ -f "/root/.ssh/config" ]] ; then echo_ok else echo_failed - error "$(cat $tmp_err_msg)" - echononl "continue anyway [yes/no]: " - read OK - OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" - while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do - echononl "Wrong entry! - repeat [yes/no]: " + if $terminal ; then + error "$(cat $tmp_err_msg)" + + echononl "continue anyway [yes/no]: " read OK - done - [[ $OK = "yes" ]] || fatal "Interupted by user" + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interupted by user" + else + fatal "$(cat $tmp_err_msg)" + fi fi else @@ -324,16 +413,21 @@ if [[ $? -eq 0 ]];then ssh_config_exists=true else echo_failed - error "$(cat $tmp_err_msg)" - echononl "continue anyway [yes/no]: " - read OK - OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" - while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do - echononl "Wrong entry! - repeat [yes/no]: " + if $terminal ; then + error "$(cat $tmp_err_msg)" + + echononl "continue anyway [yes/no]: " read OK - done - [[ $OK = "yes" ]] || fatal "Interupted by user" + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interupted by user" + else + fatal "$(cat $tmp_err_msg)" + fi fi @@ -357,14 +451,16 @@ fi # - Syncing PostgreSQL Databases.." # - -echo -e "\n\n \033[37m\033[1mSyncing PostgreSQL Databases..\033[m\n" +if $terminal ; then + echo -e "\n\n \033[37m\033[1mSyncing PostgreSQL Databases..\033[m\n" +fi _got_pgsql_databases=false sync_pgsql_databases="${sync_pgsql_databases##*( )}" sync_pgsql_databases="${sync_pgsql_databases%%*( )}" if [[ -z "$sync_pgsql_databases" ]]; then - warn "No PostgreSQL databases for syncing configured." + warn_only_terminal "No PostgreSQL databases for syncing configured." elif [[ "$sync_pgsql_databases" = "ALL" ]]; then echononl " Get (all) PostgreSQL databases from $old_server_ip.." _psql_databases=$(ssh $old_server_ip "cd /tmp ; sudo -u postgres psql -lt" 2> $tmp_err_msg | grep -v -e"^$" | awk '{print$1}') @@ -401,7 +497,7 @@ if $_got_pgsql_databases ; then sudo=$(ssh $old_server_ip which sudo 2> /dev/null) su=$(ssh $old_server_ip which su 2> /dev/null) - echo "" + blank_line for _db in $_pgsql_databases_remote ; do echononl " Sync Database '$_db'.." if [[ "${_db}" = "template0" ]] || [[ "${_db}" = "template1" ]] || [[ "${_db}" = "postgres" ]]; then @@ -429,10 +525,12 @@ fi # - Syncing MySQL Databases.." # - -echo -e "\n\n \033[37m\033[1mSyncing MySQL Databases..\033[m\n" +if $terminal; then + echo -e "\n\n \033[37m\033[1mSyncing MySQL Databases..\033[m\n" +fi if [[ -z "$sync_mysql_databases" ]]; then - warn "No MySQL databases for syncing configured." + warn_only_terminal "No MySQL databases for syncing configured." else detect_mysql_version @@ -542,7 +640,7 @@ if $_got_mysql_databases ; then echo_failed error "$(cat $tmp_err_msg)" fi - echo "" + blank_line # - Set Autocommit OFF @@ -624,7 +722,7 @@ if $_got_mysql_databases ; then continue fi - echo "" + blank_line if [[ -n "${_val_arr[1]}" ]] ; then echononl " Sync Database '$_src_db' --> '$_dst_db'.. " else @@ -638,7 +736,9 @@ if $_got_mysql_databases ; then echo_failed error "$(cat $tmp_err_msg)" - echo -e "\n\n \033[33mSomething went wromg! I'm trying an alternative way ..\033[m\n" + if $terminal ; then + echo -e "\n\n \033[33mSomething went wromg! I'm trying an alternative way ..\033[m\n" + fi echononl " Read remote DB '${_src_db}' into local file '/tmp/${_dst_db}-${cur_date}.sql'.." @@ -686,7 +786,7 @@ if $_got_mysql_databases ; then # - Reset (global) Autocommit value # - - echo "" + blank_line echononl " Reset (global) Autocommit value to '$CUR_AUTOCOMMIT'" ${l_mysql} $mysql_local_credential_args -N -s -e "SET GLOBAL AUTOCOMMIT='$CUR_AUTOCOMMIT'" >> $log_file 2> $tmp_err_msg if [[ $? -eq 0 ]];then @@ -698,7 +798,7 @@ if $_got_mysql_databases ; then # - Reset (global) value for 'unique_checks' # - - echo "" + blank_line echononl " Reset (global) 'unique_checks' value to '$CUR_UNIQUE_CHECKS'" ${l_mysql} $mysql_local_credential_args -N -s -e "SET GLOBAL unique_checks='$CUR_UNIQUE_CHECKS'" >> $log_file 2> $tmp_err_msg if [[ $? -eq 0 ]];then @@ -710,7 +810,7 @@ if $_got_mysql_databases ; then # - Reset (global) value for 'foreign_key_checks' # - - echo "" + blank_line echononl " Reset (global) 'foreign_key_checks' value to '$CUR_FOREIGN_KEY_CHECKS'" ${l_mysql} $mysql_local_credential_args -N -s -e "SET GLOBAL foreign_key_checks='$CUR_FOREIGN_KEY_CHECKS'" >> $log_file 2> $tmp_err_msg if [[ $? -eq 0 ]];then @@ -722,7 +822,7 @@ if $_got_mysql_databases ; then # - Reset (global) value for 'innodb_flush_log_at_trx_commit' # - - echo "" + blank_line echononl " Reset (global) 'innodb_flush_log_at_trx_commit' value to '$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT'" ${l_mysql} $mysql_local_credential_args -N -s -e "SET GLOBAL innodb_flush_log_at_trx_commit=$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT" >> $log_file 2> $tmp_err_msg if [[ $? -eq 0 ]];then @@ -735,14 +835,15 @@ if $_got_mysql_databases ; then fi - -echo -e "\n\n \033[37m\033[1mSyncing Home Directories..\033[m\n" +if $terminal ; then + echo -e "\n\n \033[37m\033[1mSyncing Home Directories..\033[m\n" +fi _got_home_dirs=false sync_home_dirs="${sync_home_dirs##*( )}" sync_home_dirs="${sync_home_dirs%%*( )}" if [[ -z "${sync_home_dirs,,}" ]] ; then - warn "No Home Directories for syncing configured" + warn_only_terminal "No Home Directories for syncing configured" else _got_home_dirs=true fi @@ -762,15 +863,16 @@ if $_got_home_dirs ; then done fi - -echo -e "\n\n \033[37m\033[1mSyncing Files..\033[m\n" +if $terminal ; then + echo -e "\n\n \033[37m\033[1mSyncing Files..\033[m\n" +fi _got_files=false sync_files="${sync_files##*( )}" sync_files="${sync_files%%*( )}" if [[ -z "${sync_files,,}" ]] ; then - warn "No Files for syncing configured" + warn_only_terminal "No Files for syncing configured" else _got_files=true fi @@ -790,14 +892,15 @@ if $_got_files ; then done fi - -echo -e "\n\n \033[37m\033[1mSyncing Web Directories..\033[m\n" +if $terminal ; then + echo -e "\n\n \033[37m\033[1mSyncing Web Directories..\033[m\n" +fi _got_web_dirs=false sync_web_dirs="${sync_web_dirs##*( )}" sync_web_dirs="${sync_web_dirs%%*( )}" if [[ -z "${sync_web_dirs,,}" ]] ; then - warn "No Web Directories for syncing configured" + warn_only_terminal "No Web Directories for syncing configured" else _got_web_dirs=true fi @@ -811,8 +914,10 @@ if $_got_web_dirs ; then _src_dir="${_val_arr[0]}" if [[ -n "${_val_arr[1]}" ]] ; then _dst_dir="${_val_arr[1]}" - echo -e " Syncinc directory \"${_src_dir}\"" - echononl " --> ${_dst_dir} .." + if $terminal ; then + echo -e " Syncinc directory \"${_src_dir}\"" + echononl " --> ${_dst_dir} .." + fi else _dst_dir="$_src_dir" echononl " Syncinc directory \"${_src_dir}\".." @@ -837,14 +942,15 @@ fi - -echo -e "\n\n \033[37m\033[1mSyncing Vhost Configurations..\033[m\n" +if $terminal ; then + echo -e "\n\n \033[37m\033[1mSyncing Vhost Configurations..\033[m\n" +fi _got_vhost_dirs=false sync_vhost_dirs="${sync_vhost_dirs##*( )}" sync_vhost_dirs="${sync_vhost_dirs%%*( )}" if [[ -z "${sync_vhost_dirs,,}" ]] ; then - warn "No Vhost Configurations for syncing configured" + warn_only_terminal "No Vhost Configurations for syncing configured" else _got_vhost_dirs=true fi @@ -871,8 +977,9 @@ if $_got_vhost_dirs ; then fi - -echo -e "\n\n \033[37m\033[1mConvert IP's in Vhost Configurations..\033[m\n" +if $terminal ; then + echo -e "\n\n \033[37m\033[1mConvert IP's in Vhost Configurations..\033[m\n" +fi if $_got_vhost_dirs ; then log_file=${log_dir}/convert_vhost_configs.log @@ -932,17 +1039,18 @@ if $_got_vhost_dirs ; then fi done else - warn "No Vhost Configurations for syncing configured, so no IP conversion done.." + warn_only_terminal "No Vhost Configurations for syncing configured, so no IP conversion done.." fi - -echo -e "\n\n \033[37m\033[1mSyncing Dehydrated Certs Dirs ..\033[m\n" +if $terminal ; then + echo -e "\n\n \033[37m\033[1mSyncing Dehydrated Certs Dirs ..\033[m\n" +fi _got_dehydrated_dirs_dirs=false sync_dehydrated_dirs="${sync_dehydrated_dirs##*( )}" sync_dehydrated_dirs="${sync_dehydrated_dirs%%*( )}" if [[ -z "${sync_dehydrated_dirs,,}" ]] ; then - warn "No other Directories for syncing configured" + warn_only_terminal "No dehydrated Directories for syncing configured" else _got_dehydrated_dirs_dirs=true fi @@ -962,14 +1070,15 @@ if $_got_dehydrated_dirs_dirs ; then done fi - -echo -e "\n\n \033[37m\033[1mSyncing other Directories..\033[m\n" +if $terminal ; then + echo -e "\n\n \033[37m\033[1mSyncing other Directories..\033[m\n" +fi _got_other_dirs=false sync_other_dirs="${sync_other_dirs##*( )}" sync_other_dirs="${sync_other_dirs%%*( )}" if [[ -z "${sync_other_dirs,,}" ]] ; then - warn "No other Directories for syncing configured" + warn_only_terminal "No other Directories for syncing configured" else _got_other_dirs=true fi @@ -983,8 +1092,10 @@ if $_got_other_dirs ; then _src_dir="${_val_arr[0]}" if [[ -n "${_val_arr[1]}" ]] ; then _dst_dir="${_val_arr[1]}" - echo -e " Syncinc directory \"${_src_dir}\"" - echononl " --> ${_dst_dir} .." + if $terminal ; then + echo -e " Syncinc directory \"${_src_dir}\"" + echononl " --> ${_dst_dir} .." + fi else _dst_dir="$_src_dir" echononl " Syncinc directory \"${_src_dir}\".." @@ -1006,7 +1117,9 @@ if $_got_other_dirs ; then done fi -echo -e "\n\n \033[37m\033[1mSome post syncing stuff..\033[m\n" +if $terminal ; then + echo -e "\n\n \033[37m\033[1mSome post syncing stuff..\033[m\n" +fi echononl " Recreate PureFTPd's password database" if [[ -f "/etc/pure-ftpd/pureftpd.passwd" ]]; then @@ -1037,8 +1150,9 @@ fi - -echo -e "\n\n \033[37m\033[1mRestart Services..\033[m\n" +if $terminal ; then + echo -e "\n\n \033[37m\033[1mRestart Services..\033[m\n" +fi echononl " (Re)start PureFTPd daemon (pure-ftpd)" if $systemd_exists ; then @@ -1094,6 +1208,4 @@ fi -rm $tmp_err_msg -echo "" -exit 0 +clean_up 0 diff --git a/sync_from_old_server.sh.00 b/sync_from_old_server.sh.00 new file mode 100755 index 0000000..fb7d8d8 --- /dev/null +++ b/sync_from_old_server.sh.00 @@ -0,0 +1,1099 @@ +#!/usr/bin/env bash + +# give old IPv4 address here - its mandatory" +# +ipv4_old="" +old_server_ip=$ipv4_old + +tmp_err_msg=$(mktemp) +log_dir=/root/sync_from_old_server_logs + +cur_date="$(date +%Y-%m-%d-%H%M)" + +# - Sync Home Directoties +# - +#sync_home_dirs=" +# /data/home/ilker +# /data/home/jan-kout +# /data/home/max +#" +sync_home_dirs="" + + +# - Sync web directories (included document root directory +# - +# - If the Path to the sncing directory differs at destination, +# - you cat use the following syntax: +# - +# - sync_other_dirs=" +# - ... +# - /: +# - ... +# - " +# - +# - +# - Note: +# - the basename of the destination directory must be the same as +# - the basename of the source directory. +# - +# - Example +# - sync_web_dirs=" +# - /var/www:/data/www +# - /home/chris +# - " +# - +sync_web_dirs="" + + +# - Sync Apache virtual host configurations +# - +# - Note - following files are excluded: +# - +# - 000-default.conf +# - 000-dehydrated.conf +# - 000-logformat.conf +# - 000-deflate.conf +# - 000-additional-ssl-settings.conf +# - +sync_vhost_dirs="" + +# - Only needed to replace ip-addresse in virtual host configuration files +# - +ipv4_new="" +ipv6_old="" +ipv6_new="" + + +# - Sync dehydrated cert directory +# - +sync_dehydrated_dirs="" + + +# - Sync other directories +# - +# - If the Path to the sncing directory differs at destination, +# - you cat use the following syntax: +# - +# - sync_other_dirs=" +# - ... +# - /: +# - ... +# - " +# - +# - +# - Note: +# - the basename of the destination directory must be the same as +# - the basename of the source directory. +# - +sync_other_dirs="" + + +# - Sync files +# - +sync_files="" + + +# - Sync Postgres databases +# - +# - Note: +# - Set to "ALL" if all databases from old server should be synced +# - Leave empty if no databases should be synced. +# - +sync_pgsql_databases="" + + +# - Sync MySQL databases +# - +# - Note: +# - Set to "ALL" if all databases from old server should be synced +# - Leave empty if no databases should be synced. +# - +# - Example: +# - To sync database 'shop19' aot source host to database 'shop19_dev' +# - at destination host: +# - sync_mysql_databases=" +# - ... +# - shop19:shop19_dev +# - ... +# - " +# - +sync_mysql_databases="" + +# - mysql_remote_credential_args +# - mysql_local_credential_args +# - +# - Example +# - mysql_credential_args="-u root -S /run/mysqld/mysqld.sock" +# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf" +# - mysql_credential_args="--login-path=local" +# - mysql_credential_args="-u -p''" +# - +mysql_remote_credential_args="" +mysql_local_credential_args="" + +# ------------- +# --- Some functions +# ------------- +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 "" + echo -e "fatal error: $*" + echo "" + echo -e "\t\033[31m\033[1mInstalllation will be interrupted\033[m\033[m" + echo "" + exit 1 +} + +error(){ + echo "" + echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*" + echo "" +} + +warn (){ + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" +} + +info (){ + echo "" + echo -e "\t[ \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_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[33m\033[1mskipped\033[m ]" +} + +detect_mysql_version () { + + _MYSQLD_VERSION="$(mysqld -V 2>/dev/null)" + + if [[ -z "$_MYSQLD_VERSION" ]]; then + fatal "No installed MySQL server or distribution found!" + elif [[ -d "/usr/local/mysql" ]] && [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ percona- ]]; then + MYSQL_CUR_DISTRIBUTION="Percona" + elif [[ "$_MYSQLD_VERSION" =~ MariaDB ]]; then + MYSQL_CUR_DISTRIBUTION="MariaDB" + elif [[ "$_MYSQLD_VERSION" =~ MySQL ]]; then + MYSQL_CUR_DISTRIBUTION="MySQL" + elif [[ -d "/usr/local/mysql" ]] && [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ mysql- ]]; then + MYSQL_CUR_DISTRIBUTION="MySQL" + elif [[ -d "/usr/local/mysql" ]] && [[ "$(basename "$(realpath "/usr/local/mysql")")" =~ mariadb- ]]; then + MYSQL_CUR_DISTRIBUTION="MariaDB" + else + error "MySQL Instalation found, but cannot determin the distribution!" + + MYSQL_CUR_DISTRIBUTION= + echo "" + echo " Select the MySQL distribution to install." + echo "" + echo " [1] MySQL (the original community edition)" + echo " [2] Percona Server for MySQL" + echo " [3] MariaDB" + echo "" + echononl " Eingabe [1/2/3]: " + + while [ "$MYSQL_CUR_DISTRIBUTION" != "MySQL" -a "$MYSQL_CUR_DISTRIBUTION" != "MariaDB" -a "$MYSQL_CUR_DISTRIBUTION" != "Percona" ];do + read OPTION + case $OPTION in + 1) MYSQL_CUR_DISTRIBUTION="MySQL" + ;; + 2) MYSQL_CUR_DISTRIBUTION="Percona" + ;; + 3) MYSQL_CUR_DISTRIBUTION="MariaDB" + ;; + *) echo "" + echo -e "\tFalsche Eingabe ! [ 1 = MySQL ; 2 = Percona ; 3 = MariaDB ]" + echo "" + echononl " Eingabe:" + ;; + esac + done + fi + + MYSQL_VERSION="$(echo $_MYSQLD_VERSION | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?" | head -n 1)" + MYSQL_MAJOR_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f1)" + MYSQL_MINOR_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f2)" + MYSQL_PATCH_LEVEL="$(echo $MYSQL_VERSION | cut -d '.' -f3)" + MYSQL_MAIN_VERSION="$(echo $MYSQL_VERSION | cut -d '.' -f1,2)" + +} + +service_exists() { + local n=$1 + if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | awk '{print$1}') == $n.service ]]; then + return 0 + else + return 1 + fi +} + + +# ---------- + + +# - At least ip address from old server must be present +# - +if [[ -z "$old_server_ip" ]]; then + fatal "No remote server ip (variable old_server_ip) is given." +fi + + +# - Is this a systemd system? +# - +if [[ "X$(which systemd)" = "X" ]]; then + systemd_exists=false +else + systemd_exists=true +fi + +echo "" + + +echononl " Test ssh connection to $old_server_ip" +ssh $old_server_ip "ls" > /dev/null 2> $tmp_err_msg +if [[ $? -eq 0 ]];then + echo_ok +else + echo_failed + fatal "$(cat $tmp_err_msg)" +fi + + +echononl " Backup file /root/bin/.ssh/config" +if [[ -f "/root/.ssh/config" ]] ; then + mv "/root/.ssh/config" "/root/.ssh/config.${cur_date}" > /dev/null 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interupted by user" + + fi +else + echo_skipped +fi + + +echononl " Create temporary file \033[1m/root/.ssh/config\033[m .." + cat < "/root/.ssh/config" +Host * + ControlMaster auto + ControlPath ~/.ssh/%r@%h:%p + ControlPersist 30 + StrictHostKeyChecking no + LogLevel FATAL +EOF +if [[ $? -eq 0 ]];then + echo_ok + ssh_config_exists=true +else + echo_failed + error "$(cat $tmp_err_msg)" + + echononl "continue anyway [yes/no]: " + read OK + OK="$(echo "$OK" | tr '[:upper:]' '[:lower:]')" + while [[ "$OK" != "yes" ]] && [[ "$OK" != "no" ]] ; do + echononl "Wrong entry! - repeat [yes/no]: " + read OK + done + [[ $OK = "yes" ]] || fatal "Interupted by user" + +fi + + + +# - Create log directory +# - +echononl " Create log directory \"${log_dir}\"" +if [[ -d "$log_dir" ]] ; then + echo_skipped +else + mkdir ${log_dir} > /dev/null 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi +fi + + +# - Syncing PostgreSQL Databases.." +# - +echo -e "\n\n \033[37m\033[1mSyncing PostgreSQL Databases..\033[m\n" + +_got_pgsql_databases=false +sync_pgsql_databases="${sync_pgsql_databases##*( )}" +sync_pgsql_databases="${sync_pgsql_databases%%*( )}" + +if [[ -z "$sync_pgsql_databases" ]]; then + warn "No PostgreSQL databases for syncing configured." +elif [[ "$sync_pgsql_databases" = "ALL" ]]; then + echononl " Get (all) PostgreSQL databases from $old_server_ip.." + _psql_databases=$(ssh $old_server_ip "cd /tmp ; sudo -u postgres psql -lt" 2> $tmp_err_msg | grep -v -e"^$" | awk '{print$1}') + if [[ $? -eq 0 ]];then + echo_ok + declare -i _index=1 + for _db in $_psql_databases ; do + if [[ "${_db}" = ":" ]] || [[ "${_db}" = "|" ]] ; then + continue + fi + if [[ $index -eq 1 ]] ; then + _pgsql_databases_remote="${_db}" + else + _pgsql_databases_remote="${_pgsql_databases_remote} ${_db}" + fi + (( _index++ )) + done + _got_pgsql_databases=true + else + echo_failed + fi +else + _pgsql_databases_remote="$sync_pgsql_databases" + _got_pgsql_databases=true +fi + + +if $_got_pgsql_databases ; then + log_file=${log_dir}/sync_pgsql.log + > $log_file + psql=$(ssh $old_server_ip which psql 2> /dev/null) + pg_dump=$(ssh $old_server_ip which pg_dump 2> /dev/null) + pg_dumpall=$(ssh $old_server_ip which pg_dumpall 2> /dev/null) + sudo=$(ssh $old_server_ip which sudo 2> /dev/null) + su=$(ssh $old_server_ip which su 2> /dev/null) + + echo "" + for _db in $_pgsql_databases_remote ; do + echononl " Sync Database '$_db'.." + if [[ "${_db}" = "template0" ]] || [[ "${_db}" = "template1" ]] || [[ "${_db}" = "postgres" ]]; then + echo_skipped + continue + fi + if [[ "${_db}" = ":" ]] || [[ "${_db}" = "|" ]] ; then + echo_skipped + continue + fi + + ssh $old_server_ip "cd /tmp ; sudo -u postgres $pg_dump -c $_db" 2> $tmp_err_msg \ + | su postgres -lc "psql $_db" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + #error "$(cat $tmp_err_msg)" + fi + + done +fi + + + +# - Syncing MySQL Databases.." +# - +echo -e "\n\n \033[37m\033[1mSyncing MySQL Databases..\033[m\n" + +if [[ -z "$sync_mysql_databases" ]]; then + warn "No MySQL databases for syncing configured." +else + detect_mysql_version + + MAJOR_VERSION="$MYSQL_MAJOR_VERSION" + MINOR_VERSION="$MYSQL_MINOR_VERSION" + PATCH_LEVEL="$MYSQL_PATCH_LEVEL" + + # - get remote mysqldump command + # - + if $(ssh $old_server_ip [[ -x "/usr/local/mysql/bin/mysqldump" ]]) ; then + r_mysqldump="/usr/local/mysql/bin/mysqldump" + else + r_mysqldump="$(ssh $old_server_ip which mysqldump)" + fi + + # - get remote mysql command + # - + if $(ssh $old_server_ip [[ -x "/usr/local/mysql/bin/mysql" ]]) ; then + r_mysql="/usr/local/mysql/bin/mysql" + else + r_mysql="$(ssh $old_server_ip which mysql)" + fi + + ssh_options="-o BatchMode=yes -o ConnectTimeout=360" + + # - get local mysql command + # - + if [[ -x "/usr/local/mysql/bin/mysql" ]] ; then + l_mysql="/usr/local/mysql/bin/mysql" + else + l_mysql="$(which mysql)" + fi + +fi + + +if [[ "$sync_mysql_databases" = "ALL" ]]; then + + _got_mysql_databases=false + sync_mysql_databases="${sync_mysql_databases##*( )}" + sync_mysql_databases="${sync_mysql_databases%%*( )}" + + echononl " Get MySQL databases from $old_server_ip.." + _mysql_databases_remote=$(ssh $old_server_ip "${r_mysql} $mysql_remote_credential_args -N -s -e \"show databases\"") + if [[ $? -eq 0 ]];then + echo_ok + _got_mysql_databases=true + else + echo_failed + fi +elif [[ -n "$sync_mysql_databases" ]]; then + _mysql_databases_remote="$sync_mysql_databases" + _got_mysql_databases=true +else + _got_mysql_databases=false +fi + +if $_got_mysql_databases ; then + log_file=${log_dir}/sync_mysql.log + > $log_file + + mysqldump_flags="--protocol=SOCKET --max_allowed_packet=512M --skip-opt --add-drop-table --add-locks --create-options --quick --compress --set-charset --disable-keys --lock-tables --routines" + + # - GET current (global) Autocommit value + # - + echononl " GET current (global) 'autocommit' value" + CUR_AUTOCOMMIT="$(${l_mysql} $mysql_local_credential_args -N -s -e "SHOW GLOBAL VARIABLES LIKE 'autocommit'" | awk '{print$2}')" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + + # - GET current (global) value for 'unique_checks' + # - + echononl " GET current (global) 'unique_checks' value" + CUR_UNIQUE_CHECKS="$(${l_mysql} $mysql_local_credential_args -N -s -e "SHOW GLOBAL VARIABLES LIKE 'unique_checks'" | awk '{print$2}')" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + + # - GET current (global) value for 'foreign_key_checks' + # - + echononl " GET current (global) 'foreign_key_checks' value" + CUR_FOREIGN_KEY_CHECKS="$(${l_mysql} $mysql_local_credential_args -N -s -e "SHOW GLOBAL VARIABLES LIKE 'foreign_key_checks'" | awk '{print$2}')" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + + # - GET current (global) value for 'innodb_flush_log_at_trx_commit' + # - + echononl " GET current (global) 'innodb_flush_log_at_trx_commit' value" + CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT="$(${l_mysql} $mysql_local_credential_args -N -s -e "SHOW GLOBAL VARIABLES LIKE 'innodb_flush_log_at_trx_commit'" | awk '{print$2}')" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + echo "" + + + # - Set Autocommit OFF + # - + echononl " Set 'autocommit' to OFF" + ${l_mysql} $mysql_local_credential_args -N -s -e "SET GLOBAL autocommit='OFF'" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + + # - Set UNIQUE_CHECKS OFF + # - + echononl " Set 'unique_checks' to OFF" + ${l_mysql} $mysql_local_credential_args -N -s -e "SET GLOBAL unique_checks='OFF'" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + + # - Set FOREIGN_KEY_CHECKS OFF + # - + echononl " Set 'foreign_key_checks' to OFF" + ${l_mysql} $mysql_local_credential_args -N -s -e "SET GLOBAL foreign_key_checks='OFF'" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + + # - Set innodb_flush_log_at_trx_commit OFF + # - + echononl " Set 'innodb_flush_log_at_trx_commit' to 2" + ${l_mysql} $mysql_local_credential_args -N -s -e "SET GLOBAL innodb_flush_log_at_trx_commit=2" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + + for _val in $_mysql_databases_remote ; do + + IFS=':' read -a _val_arr <<< "${_val}" + _src_db="${_val_arr[0]}" + if [[ -n "${_val_arr[1]}" ]] ; then + _dst_db="${_val_arr[1]}" + else + _dst_db="$_src_db" + fi + + if [[ "${_src_db}" = "information_schema" ]]; then + echo_skipped + continue + fi + if [[ "${_src_db}" = "performance_schema" ]]; then + echo_skipped + continue + fi + if [[ "${_src_db}" = "mysql" ]]; then + echo_skipped + continue + fi + if [[ "${_src_db}" = "sys" ]]; then + echo_skipped + continue + fi + if [[ "${_src_db}" = "test" ]] || [[ "${_src_db}" = "mysqltest" ]] ; then + echo_skipped + continue + fi + + echo "" + if [[ -n "${_val_arr[1]}" ]] ; then + echononl " Sync Database '$_src_db' --> '$_dst_db'.. " + else + echononl " Sync Database '$_src_db'.. " + fi + ssh $old_server_ip "${r_mysqldump} $mysql_remote_credential_args $mysqldump_flags $_src_db" \ + | ${l_mysql} $mysql_local_credential_args $_dst_db >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + + echo -e "\n\n \033[33mSomething went wromg! I'm trying an alternative way ..\033[m\n" + + + echononl " Read remote DB '${_src_db}' into local file '/tmp/${_dst_db}-${cur_date}.sql'.." + ssh $old_server_ip "${r_mysqldump} $mysql_remote_credential_args $mysqldump_flags $_src_db" \ + > "/tmp/${_dst_db}-${cur_date}.sql" 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + echononl " Get rid of DEFINER statements from Mysql dump .." + sed -i 's/DEFINER=[^*]*\*/\*/g' "/tmp/${_dst_db}-${cur_date}.sql" 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + echononl " Read in MySQL database '${_dst_db}'.." + ${l_mysql} $mysql_local_credential_args $_dst_db < "/tmp/${_dst_db}-${cur_date}.sql" 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + echononl " Remove dump file '/tmp/${_dst_db}-${cur_date}.sql'.." + rm -rf "/tmp/${_dst_db}-${cur_date}.sql" 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + fi + + + + done + + # - Reset (global) Autocommit value + # - + echo "" + echononl " Reset (global) Autocommit value to '$CUR_AUTOCOMMIT'" + ${l_mysql} $mysql_local_credential_args -N -s -e "SET GLOBAL AUTOCOMMIT='$CUR_AUTOCOMMIT'" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + # - Reset (global) value for 'unique_checks' + # - + echo "" + echononl " Reset (global) 'unique_checks' value to '$CUR_UNIQUE_CHECKS'" + ${l_mysql} $mysql_local_credential_args -N -s -e "SET GLOBAL unique_checks='$CUR_UNIQUE_CHECKS'" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + # - Reset (global) value for 'foreign_key_checks' + # - + echo "" + echononl " Reset (global) 'foreign_key_checks' value to '$CUR_FOREIGN_KEY_CHECKS'" + ${l_mysql} $mysql_local_credential_args -N -s -e "SET GLOBAL foreign_key_checks='$CUR_FOREIGN_KEY_CHECKS'" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + # - Reset (global) value for 'innodb_flush_log_at_trx_commit' + # - + echo "" + echononl " Reset (global) 'innodb_flush_log_at_trx_commit' value to '$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT'" + ${l_mysql} $mysql_local_credential_args -N -s -e "SET GLOBAL innodb_flush_log_at_trx_commit=$CUR_INNODB_FLUSH_LOG_AT_TRX_COMMIT" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + +fi + + + +echo -e "\n\n \033[37m\033[1mSyncing Home Directories..\033[m\n" + +_got_home_dirs=false +sync_home_dirs="${sync_home_dirs##*( )}" +sync_home_dirs="${sync_home_dirs%%*( )}" +if [[ -z "${sync_home_dirs,,}" ]] ; then + warn "No Home Directories for syncing configured" +else + _got_home_dirs=true +fi + +if $_got_home_dirs ; then + log_file=${log_dir}/sync_home_dirs.log + > $log_file + for sync_dir in $sync_home_dirs ; do + echononl " Syncinc directory \"${sync_dir}\".." + rsync -av -e ssh --delete $old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + done +fi + + +echo -e "\n\n \033[37m\033[1mSyncing Files..\033[m\n" + +_got_files=false +sync_files="${sync_files##*( )}" +sync_files="${sync_files%%*( )}" + +if [[ -z "${sync_files,,}" ]] ; then + warn "No Files for syncing configured" +else + _got_files=true +fi + +if $_got_files ; then + log_file=${log_dir}/sync_files.log + > $log_file + for sync_file in $sync_files ; do + echononl " Syncinc file \"${sync_file}\".." + rsync -av -e ssh --delete $old_server_ip:$sync_file $(dirname $sync_file)/ >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + done +fi + + +echo -e "\n\n \033[37m\033[1mSyncing Web Directories..\033[m\n" + +_got_web_dirs=false +sync_web_dirs="${sync_web_dirs##*( )}" +sync_web_dirs="${sync_web_dirs%%*( )}" +if [[ -z "${sync_web_dirs,,}" ]] ; then + warn "No Web Directories for syncing configured" +else + _got_web_dirs=true +fi + +if $_got_web_dirs ; then + log_file=${log_dir}/sync_web_dirs.log + > $log_file + for _val in $sync_web_dirs ; do + + IFS=':' read -a _val_arr <<< "${_val}" + _src_dir="${_val_arr[0]}" + if [[ -n "${_val_arr[1]}" ]] ; then + _dst_dir="${_val_arr[1]}" + echo -e " Syncinc directory \"${_src_dir}\"" + echononl " --> ${_dst_dir} .." + else + _dst_dir="$_src_dir" + echononl " Syncinc directory \"${_src_dir}\".." + fi + + if [[ "$(basename ${_dst_dir})" != "$(basename ${_src_dir})" ]] ; then + echo_failed + error "The basename of source directory and destination directory must not be deffer!" + continue + fi + + rsync -av -e ssh --delete "$old_server_ip:$_src_dir" "$(dirname "${_dst_dir}")" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + + done +fi + + + + +echo -e "\n\n \033[37m\033[1mSyncing Vhost Configurations..\033[m\n" + +_got_vhost_dirs=false +sync_vhost_dirs="${sync_vhost_dirs##*( )}" +sync_vhost_dirs="${sync_vhost_dirs%%*( )}" +if [[ -z "${sync_vhost_dirs,,}" ]] ; then + warn "No Vhost Configurations for syncing configured" +else + _got_vhost_dirs=true +fi + +if $_got_vhost_dirs ; then + log_file=${log_dir}/sync_vhost_configs.log + > $log_file + for sync_dir in $sync_vhost_dirs ; do + echononl " Syncinc directory \"${sync_dir}\".." + rsync -av -e ssh --delete \ + --exclude 000-default.conf \ + --exclude 000-dehydrated.conf \ + --exclude 000-logformat.conf \ + --exclude 000-deflate.conf \ + --exclude 000-additional-ssl-settings.conf \ + $old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + done +fi + + + +echo -e "\n\n \033[37m\033[1mConvert IP's in Vhost Configurations..\033[m\n" + +if $_got_vhost_dirs ; then + log_file=${log_dir}/convert_vhost_configs.log + > $log_file + for sync_dir in $sync_vhost_dirs ; do + + installation_failed=false + vhost_replaced=false + + echononl " Convert Vhost Configuration in \"$sync_dir\"" + while IFS='' read -r -d '' _file ; do + + [[ -d "$_file" ]] && continue + [[ -h "$_file" ]] && continue + [[ "$(basename $_file)" = "000-default.conf" ]] && continue + [[ "$(basename $_file)" = "000-dehydrated.conf" ]] && continue + [[ "$(basename $_file)" = "000-logformat.conf" ]] && continue + [[ "$(basename $_file)" = "000-deflate.conf" ]] && continue + [[ "$(basename $_file)" = "000-additional-ssl-settings.conf" ]] && continue + + if [[ -f "$_file" ]]; then + + # - Replace old IPv4 Address with the new one + # - + if [[ -n "$ipv6_old" ]] && [[ -n "$ipv6_new" ]] ; then + perl -i -n -p -e s"#$ipv4_old#$ipv4_new#g" \ + $_file >> $log_file 2> $tmp_err_msg + if [[ $? -ne 0 ]]; then + installation_failed=true + error "$(cat $tmp_err_msg)" + else + vhost_replaced=true + fi + fi + + # - Replace old IPv6 Address with the new one + # - + if [[ -n "$ipv6_old" ]] && [[ -n "$ipv6_new" ]] ; then + perl -i -n -p -e s"#$ipv6_old#$ipv6_new#g" \ + $_file >> $log_file 2> $tmp_err_msg + if [[ $? -ne 0 ]]; then + installation_failed=true + error "$(cat $tmp_err_msg)" + else + vhost_replaced=true + fi + fi + + fi + done < <(find $sync_dir -mindepth 1 -maxdepth 1 -type f -print0) + if $installation_failed ; then + echo_failed + elif $vhost_replaced ; then + echo_ok + else + echo_skipped + fi + done +else + warn "No Vhost Configurations for syncing configured, so no IP conversion done.." +fi + + +echo -e "\n\n \033[37m\033[1mSyncing Dehydrated Certs Dirs ..\033[m\n" + +_got_dehydrated_dirs_dirs=false +sync_dehydrated_dirs="${sync_dehydrated_dirs##*( )}" +sync_dehydrated_dirs="${sync_dehydrated_dirs%%*( )}" +if [[ -z "${sync_dehydrated_dirs,,}" ]] ; then + warn "No other Directories for syncing configured" +else + _got_dehydrated_dirs_dirs=true +fi + +if $_got_dehydrated_dirs_dirs ; then + log_file=${log_dir}/sync_dehydrated_dirs.log + > $log_file + for sync_dir in $sync_dehydrated_dirs ; do + echononl " Syncinc directory \"${sync_dir}\".." + rsync -av -e ssh $old_server_ip:$sync_dir $(dirname $sync_dir)/ >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + done +fi + + +echo -e "\n\n \033[37m\033[1mSyncing other Directories..\033[m\n" + +_got_other_dirs=false +sync_other_dirs="${sync_other_dirs##*( )}" +sync_other_dirs="${sync_other_dirs%%*( )}" +if [[ -z "${sync_other_dirs,,}" ]] ; then + warn "No other Directories for syncing configured" +else + _got_other_dirs=true +fi + +if $_got_other_dirs ; then + log_file=${log_dir}/sync_other_dirs.log + > $log_file + for _val in $sync_other_dirs ; do + + IFS=':' read -a _val_arr <<< "${_val}" + _src_dir="${_val_arr[0]}" + if [[ -n "${_val_arr[1]}" ]] ; then + _dst_dir="${_val_arr[1]}" + echo -e " Syncinc directory \"${_src_dir}\"" + echononl " --> ${_dst_dir} .." + else + _dst_dir="$_src_dir" + echononl " Syncinc directory \"${_src_dir}\".." + fi + + if [[ "$(basename ${_dst_dir})" != "$(basename ${_src_dir})" ]] ; then + echo_failed + error "The basename of source directory and destination directory must not be deffer!" + continue + fi + + rsync -av -e ssh --delete "$old_server_ip:$_src_dir" "$(dirname "${_dst_dir}")" >> $log_file 2> $tmp_err_msg + if [[ $? -eq 0 ]];then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + done +fi + +echo -e "\n\n \033[37m\033[1mSome post syncing stuff..\033[m\n" + +echononl " Recreate PureFTPd's password database" +if [[ -f "/etc/pure-ftpd/pureftpd.passwd" ]]; then + pure-pw mkdb -f "/etc/pure-ftpd/pureftpd.passwd" > /dev/null 2> $tmp_err_msg + if [[ $? -eq 0 ]] ; then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi +else + echo_skipped +fi + + +echononl " Restore file \033\033[1m/root/.ssh/config\033[m .." +if [[ -f "/root/.ssh/config.${cur_date}" ]] ; then + mv "/root/.ssh/config.${cur_date}" "/root/.ssh/config" > /dev/null 2> $tmp_err_msg + if [[ $? -eq 0 ]] ; then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi +else + echo_skipped +fi + + + + +echo -e "\n\n \033[37m\033[1mRestart Services..\033[m\n" + +echononl " (Re)start PureFTPd daemon (pure-ftpd)" +if $systemd_exists ; then + if [[ ! -f /etc/init.d/pure-ftpd ]] && [[ ! -f /etc/systemd/system/pure-ftpd.service ]] ; then + echo_skipped + else + systemctl restart pure-ftpd > /dev/null 2> $tmp_err_msg + if [[ $? -eq 0 ]] ; then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + fi +else + if [[ ! -f /etc/init.d/pure-ftpd ]]; then + echo_skipped + else + /etc/init.d/pure-ftpd restart > /dev/null 2> $tmp_err_msg + if [[ $? -eq 0 ]] ; then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + fi +fi + +echononl " (Re)start Apache webservice" +if $systemd_exists ; then + if service_exists apache2 ; then + systemctl restart apache2 > /dev/null 2> $tmp_err_msg + if [[ $? -eq 0 ]] ; then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi + else + echo_skipped + fi +elif [[ -f "/etc/init.d/apache2" ]]; then + /etc/init.d/apache2 restart > /dev/null 2> $tmp_err_msg + if [[ $? -eq 0 ]] ; then + echo_ok + else + echo_failed + error "$(cat $tmp_err_msg)" + fi +else + echo_skipped +fi + + + +rm $tmp_err_msg +echo "" +exit 0