sync_from_old_server.sh: add functionality for calling as cronjob.

This commit is contained in:
Christoph 2024-01-31 12:39:51 +01:00
parent d7a10405ad
commit 80656a8269
3 changed files with 1321 additions and 110 deletions

Binary file not shown.

View File

@ -2,7 +2,7 @@
# give old IPv4 address here - its mandatory"
#
ipv4_old=""
ipv4_old="<SRC-IP_ADDRESS>"
old_server_ip=$ipv4_old
tmp_err_msg=$(mktemp)
@ -80,6 +80,14 @@ sync_dehydrated_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
@ -128,13 +136,26 @@ sync_mysql_databases=""
# - mysql_credential_args="--login-path=local"
# - mysql_credential_args="-u <db_name> -p'<db_passwd>'"
# -
mysql_remote_credential_args=""
mysql_local_credential_args=""
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
@ -142,52 +163,96 @@ echononl(){
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
fi
}
fatal(){
echo ""
echo -e "fatal error: $*"
if $terminal ; then
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
echo ""
echo -e "\t\033[31m\033[1mInstalllation will be interrupted\033[m\033[m"
echo -e " \033[31m\033[1mScript was interupted\033[m!"
else
echo " [ Fatal ]: $*"
echo ""
exit 1
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 () {
@ -207,6 +272,7 @@ detect_mysql_version () {
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=
@ -235,6 +301,9 @@ detect_mysql_version () {
;;
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,6 +377,8 @@ if [[ -f "/root/.ssh/config" ]] ; then
echo_ok
else
echo_failed
if $terminal ; then
error "$(cat $tmp_err_msg)"
echononl "continue anyway [yes/no]: "
@ -303,6 +389,9 @@ if [[ -f "/root/.ssh/config" ]] ; then
read OK
done
[[ $OK = "yes" ]] || fatal "Interupted by user"
else
fatal "$(cat $tmp_err_msg)"
fi
fi
else
@ -324,6 +413,8 @@ if [[ $? -eq 0 ]];then
ssh_config_exists=true
else
echo_failed
if $terminal ; then
error "$(cat $tmp_err_msg)"
echononl "continue anyway [yes/no]: "
@ -334,6 +425,9 @@ else
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.."
# -
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.."
# -
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)"
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
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
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
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]}"
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
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
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
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
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]}"
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
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
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

1099
sync_from_old_server.sh.00 Executable file

File diff suppressed because it is too large Load Diff