#!/usr/bin/env bash ipv4_old="83.223.85.101" ipv4_new="83.223.86.116" ipv6_old="2a01:30:1fff:3::101" ipv6_new="2a01:30:0:13:2c5:48ff:feee:f21c" old_server_ip=$ipv4_old sync_home_dirs="" tmp_err_msg=$(mktemp) log_dir=/root/sync_from_old_server_logs sync_web_dirs=" /var/www/lists.aktionsbuendnis-brandenburg.de /var/www/lists.initiativenserver.de " sync_web_dirs="" sync_vhost_dirs=" /usr/local/apache2/conf/vhosts " sync_vhost_dirs="" sync_dehydrated_dirs=" /var/lib/dehydrated/certs " sync_other_dirs=" /etc/opendkim /usr/local/mailman/lists /usr/local/mailman/data /usr/local/mailman/archives " sync_files="" # - Set to "ALL" if all databases from old server should be synced # - Leave empty if no satabases should be synced # - sync_pgsql_databases="" # - Set to "ALL" if all databases from old server should be synced # - Leave empty if no satabases should be synced # - sync_mysql_databases=" postfix roundcubemail " sync_mysql_databases="" # ------------- # --- 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_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 ]" } # ---------- # - 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 # - 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" | grep -v -e"^$" | awk '{print$1}') if [[ $? -eq 0 ]];then echo_ok _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) pg_dump=$(ssh $old_server_ip which pg_dump) pg_dumpall=$(ssh $old_server_ip which pg_dumpall) sudo=$(ssh $old_server_ip which sudo) su=$(ssh $old_server_ip which su) for _db in $_pgsql_databases_remote ; do echononl " Sync Database $_db" if [[ "${_db}" = "template0" ]] || [[ "${_db}" = "template1" ]] ; 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" \ | 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" _got_mysql_databases=false sync_mysql_databases="${sync_mysql_databases##*( )}" sync_mysql_databases="${sync_mysql_databases%%*( )}" if [[ -z "$sync_mysql_databases" ]]; then warn "No MySQL databases for syncing configured." elif [[ "$sync_mysql_databases" = "ALL" ]]; then echononl " Get MySQL databases from $old_server_ip.." _mysql_databases_remote=$(ssh $old_server_ip "/usr/local/mysql/bin/mysql --login-path=local -N -s -e \"show databases\"") if [[ $? -eq 0 ]];then echo_ok _got_mysql_databases=true else echo_failed fi else _mysql_databases_remote="$sync_mysql_databases" _got_mysql_databases=true fi if $_got_mysql_databases ; then log_file=${log_dir}/sync_mysql.log > $log_file mysqldump_flags="--protocol=SOCKET --max_allowed_packet=128M --skip-opt --add-drop-table --add-locks --create-options --quick --set-charset --disable-keys --lock-tables --routines" for _db in $_mysql_databases_remote ; do echononl " Sync Database $_db" if [[ "${_db}" = "information_schema" ]]; then echo_skipped continue fi if [[ "${_db}" = "performance_schema" ]]; then echo_skipped continue fi if [[ "${_db}" = "mysql" ]]; then echo_skipped continue fi if [[ "${_db}" = "test" ]] || [[ "${_db}" = "mysqltest" ]] ; then echo_skipped continue fi ssh $old_server_ip "/usr/local/mysql/bin/mysqldump --login-path=local $_db" | mysql --login-path=local $_db >> $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 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 sync_dir in $sync_web_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 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 \ $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 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 if [[ -f "$_file" ]]; 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)" fi 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)" fi fi done < <(find $sync_dir -mindepth 1 -maxdepth 1 -type f -print0) if ! $installation_failed ; then echo_ok fi done else echo_skipped 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 sync_dir in $sync_other_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[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 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 /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 fi rm $tmp_err_msg echo "" exit 0 if [[ "$?" -ne 0 ]] ; then installation_failed=true error "$(cat $tmp_err_msg)" fi if ! $installation_failed ; then echo_ok fi