#!/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=" # - ... # - /: # - ... # - " # - # - Examples: # - # - sync_other_dirs=" # - /etc/cron.d:/data/old-server/etc/cron.d # - /opt:/data/old-server/opt # - /root/crontab # - /usr/local/bin # - " # - # - 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="--login-path=local" mysql_local_credential_args="-S /run/mysqld/mysqld.sock" # ------------- # --- Some functions # ------------- clean_up() { # Perform program exit housekeeping #rm -rf "$LOCK_DIR" rm $tmp_err_msg blank_line exit $1 } echononl(){ if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n "$*\\c" 1>&2 else echo -e -n "$*" 1>&2 fi rm /tmp/shprompt$$ fi } fatal(){ echo "" 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 "" 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 "" } warn_only_terminal () { if $terminal ; then echo "" echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" echo "" fi } info (){ if $terminal ; then echo "" echo -e "\t[ \033[32m\033[1mInfo\033[m ]: $*" echo "" fi } echo_done() { if $terminal ; then echo -e "\033[80G[ \033[32mdone\033[m ]" fi } echo_ok() { if $terminal ; then echo -e "\033[80G[ \033[32mok\033[m ]" fi } echo_ok() { if $terminal ; then echo -e "\033[80G[ \033[32mok\033[m ]" fi } echo_warning() { if $terminal ; then echo -e "\033[80G[ \033[33m\033[1mwarn\033[m ]" fi } echo_failed(){ if $terminal ; then echo -e "\033[80G[ \033[1;31mfailed\033[m ]" fi } echo_skipped() { if $terminal ; then echo -e "\033[80G[ \033[33m\033[1mskipped\033[m ]" fi } blank_line() { if $terminal ; then echo "" fi } 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 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]: " 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)" 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 } # ---------- # - Some checks .. # ---------- # - Running in a terminal? # - if [[ -t 1 ]] ; then terminal=true else terminal=false 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 blank_line 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 if $terminal ; then 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" else fatal "$(cat $tmp_err_msg)" fi 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 if $terminal ; then 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" else fatal "$(cat $tmp_err_msg)" fi 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.." # - 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_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}') 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) blank_line 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.." # - if $terminal; then echo -e "\n\n \033[37m\033[1mSyncing MySQL Databases..\033[m\n" fi if [[ -z "$sync_mysql_databases" ]]; then warn_only_terminal "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 blank_line # - 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 blank_line 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)" 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'.." 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 # - 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 echo_ok else echo_failed error "$(cat $tmp_err_msg)" fi # - Reset (global) value for 'unique_checks' # - 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 echo_ok else echo_failed error "$(cat $tmp_err_msg)" fi # - Reset (global) value for 'foreign_key_checks' # - 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 echo_ok else echo_failed error "$(cat $tmp_err_msg)" fi # - Reset (global) value for 'innodb_flush_log_at_trx_commit' # - 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 echo_ok else echo_failed error "$(cat $tmp_err_msg)" fi fi 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_only_terminal "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 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_only_terminal "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 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_only_terminal "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]}" if $terminal ; then echo -e " Syncinc directory \"${_src_dir}\"" echononl " --> ${_dst_dir} .." fi 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 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_only_terminal "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 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 > $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_only_terminal "No Vhost Configurations for syncing configured, so no IP conversion done.." fi 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_only_terminal "No dehydrated 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 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_only_terminal "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]}" if $terminal ; then echo -e " Syncinc directory \"${_src_dir}\"" echononl " --> ${_dst_dir} .." fi 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 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 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 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 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 clean_up 0