From 76a7275d1bd37576c33645d4681f607591c93048 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Fri, 28 Oct 2022 16:53:09 +0200 Subject: [PATCH] extract function to build rsync command --- client/zzz_evobackup | 123 +++++++++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 52 deletions(-) diff --git a/client/zzz_evobackup b/client/zzz_evobackup index 6cca866..fe49ca0 100755 --- a/client/zzz_evobackup +++ b/client/zzz_evobackup @@ -371,6 +371,58 @@ local_tasks() { log "STOP LOCAL_TASKS" } +build_rsync_cmd() { + ################################################################### + # /!\ WARNING /!\ WARNING /!\ WARNING /!\ WARNING /!\ WARNING /!\ # + ################################################################### + # DO NOT USE COMMENTS in rsync lines # + # DO NOT ADD WHITESPACES AFTER \ in rsync lines # + # It breaks the command and destroys data # + # You should not modify this, unless you are really REALLY sure # + ################################################################### + + # Rsync command + cmd="$(command -v rsync)" + + # Rsync main options + cmd="${cmd} --archive" + cmd="${cmd} --itemize-changes" + cmd="${cmd} --quiet" + cmd="${cmd} --stats" + cmd="${cmd} --human-readable" + cmd="${cmd} --relative" + cmd="${cmd} --partial" + cmd="${cmd} --delete" + cmd="${cmd} --delete-excluded" + cmd="${cmd} --force" + cmd="${cmd} --ignore-errors" + cmd="${cmd} --log-file=${RSYNC_LOGFILE}" + cmd="${cmd} --rsh='ssh -p ${SSH_PORT} -o \"ConnectTimeout ${SSH_CONNECT_TIMEOUT}\"'" + + # Rsync excludes + while read line ; do + # Ignore lines containing # or ; (anywhere) + exclude=$(echo "${line}" | grep --invert-match --extended-regexp "[;#]") + if [ -n "${exclude}" ]; then + cmd="${cmd} --exclude ${exclude}" + fi + done < "${excludes_file}" + + # Rsync local sources + cmd="${cmd} ${default_includes}" # Default includes are platform specific + while read line ; do + # Ignore blank lines, and lines beginning with # or ; + include=$(echo "${line}" | grep --extended-regexp "^[^;#]+") + if [ -n "${include}" ]; then + cmd="${cmd} ${include}" + fi + done < "${includes_file}" + + # Rsync remote destination + cmd="${cmd} root@${SSH_SERVER}:/var/backup/" + + echo "${cmd}" +} sync_tasks() { n=0 server="" @@ -404,10 +456,11 @@ sync_tasks() { default_includes="/bsd /bin /sbin /usr" fi - # Create a temp file for excludes + # Create a temp file for excludes and includes excludes_file="$(mktemp "${PROGNAME}.excludes.XXXXXX")" - # … and add it to the list of files to delete at exit - temp_files="${temp_files} ${excludes_file}" + includes_file="$(mktemp "${PROGNAME}.includes.XXXXXX")" + # … and add them to the list of files to delete at exit + temp_files="${temp_files} ${excludes_file} ${includes_file}" # Excluded paths can be customized cat >> "${excludes_file}" <> "${includes_file}" < "${RSYNC_LOGFILE}" fi - # execute Rsync command + # Build the final Rsync command + rsync_cmd=$(build_rsync_cmd) + + # … log it + log "SYNC_TASKS - Rsync command : ${rsync_cmd}" + + # … execute it eval "${rsync_cmd}" rsync_rc=$?