]> arthur.barton.de Git - backup-script.git/commitdiff
Implement "{default_}job_pre_exec" and "{default_}job_post_exec"
authorAlexander Barton <alex@barton.de>
Thu, 26 Feb 2015 13:50:06 +0000 (14:50 +0100)
committerAlexander Barton <alex@barton.de>
Thu, 26 Feb 2015 13:50:06 +0000 (14:50 +0100)
When the pre-exec command returns non 0, the backup job will be
skipped and listed as failed (which results in the "there have been
errors" summy); if it returns 99, the backup job is skipped but NOT
accounted as failed.

The exit code of the post-exec command is ignored.

bin/backup-script

index 97cb941013f12c04d7340968f49eb403833035e6..5c4811e10214a4834cb68c238985648d988ff4a7 100755 (executable)
@@ -38,6 +38,8 @@ default_compress=1
 default_ping=1
 default_local=0
 default_generations=0
+default_job_pre_exec=""
+default_job_post_exec=""
 
 Usage() {
        echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
@@ -72,6 +74,27 @@ GotSignal() {
        exit 9
 }
 
+ExecJob() {
+       what="$1"
+       cmd="$2"
+
+       echo "Running job ${what}-exec command ..."
+       [ "$local" -eq 0 ] \
+               && cmd="$ssh_cmd ${user}@${system} $cmd"
+       echo -n "Start date (${what}-exec): "; date
+       echo "$cmd"
+       if [ "$DRYRUN" -eq 0 ]; then
+               $SHELL -c "$cmd"; ret=$?
+       else
+               echo " *** Trial run, not executing ${what}-exec command!"
+               ret=0
+       fi
+       [ $ret -eq 0 ] \
+               && echo "The ${what}-exec command completed with status 0, OK." \
+               || echo "The ${what}-exec command completed with ERRORS, code $ret!"
+       return $ret
+}
+
 while [ $# -gt 0 ]; do
        case "$1" in
          "-n"|"--dry-run")
@@ -163,6 +186,8 @@ for f in $sys; do
        ping="$default_ping"
        local="$default_local"
        generations="$default_generations"
+       job_pre_exec="$default_job_pre_exec"
+       job_post_exec="$default_job_post_exec"
 
        # Read in system configuration file
        source "$f"
@@ -269,6 +294,17 @@ for f in $sys; do
        ssh_cmd="ssh"
        [ -n "$ssh_args_add" ] && ssh_cmd="$ssh_cmd $ssh_args_add"
 
+       # execute job "pre-exec" command, if any
+       if [ -n "$job_pre_exec" ]; then
+               ExecJob pre "$job_pre_exec" ; ret=$?
+               if [ $ret -ne 0 ]; then
+                       [ $ret -ne 99 ] && count_started=$count_started+1
+                       echo "Pre-exec command failed, \"$system\" skipped!"
+                       echo; continue
+               fi
+       fi
+
+       # prepare (remote) command ...
        cmd="rsync --archive"
        [ "$compress" -ne 0 ] && cmd="$cmd --compress"
        cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
@@ -316,6 +352,11 @@ for f in $sys; do
                echo "System \"$system\" completed with ERRORS, code $ret!"
        fi
 
+       # execute job "post-exec" command, if any
+       if [ -n "$job_post_exec" ]; then
+               ExecJob post "$job_post_exec"
+       fi
+
        # Clean up old generations
        if [ $generations -gt 0 ]; then
                sys_target="$target/$fname"