#!/usr/bin/env bash ipv4_old="" old_server_ip=$ipv4_old tmp_err_msg=$(mktemp) log_dir=/root/sync_from_old_server_logs # - Sync Home Directoties # - #sync_home_dirs=" # /data/home/ilker # /data/home/jan-kout # /data/home/max #" sync_home_dirs=" /home/confluence " # - 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: # - 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=" /var/lib/dehydrated/certs " # - 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=" /root/bin/mysql/databases " # - Sync files # - sync_files=" /var/lib/dehydrated/domains.txt /etc/pure-ftpd/pureftpd.passwd " # - 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="ALL" # - 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="-u root -S /run/mysqld/mysqld.sock" # ------------- # --- 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 ]" } service_exists() { local n=$1 if [[ $(systemctl list-units --all -t service --full --no-legend "$n.service" | cut -f1 -d' ') == $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 # - 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" _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 $mysql_remote_credential_args -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" # - GET current (global) Autocommit value # - echononl " GET current (global) Autocommit value" CUR_AUTOCOMMIT="$(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 # - Set Autocommit OFF # - echononl " Set Autocommit to OFF" 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 echo "" 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]}" echononl " Sync Database '$_src_db' --> '$_dst_db'.. " else echononl " Sync Database '$_src_db'.. " _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 ssh $old_server_ip "/usr/local/mysql/bin/mysqldump $mysql_remote_credential_args $_src_db" | 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)" fi done # - Reset (global) Autocommit value # - echo "" echononl " Reset (global) Autocommit value to '$CUR_AUTOCOMMIT'" 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 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 \ $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 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 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