]> arthur.barton.de Git - backup-script.git/blobdiff - bin/backup-script
Introduce internal "sys_root" variable
[backup-script.git] / bin / backup-script
index 445b9d19e9871cf189bb6ecdbdd9cc90f76aa7a4..df719d3fe314731408efdc142c10967d4a0069d5 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/bash
 #
 # backup-script system for cloning systems using rsync
-# Copyright (c)2008-2014 Alexander Barton, alex@barton.de
+# Copyright (c)2008-2015 Alexander Barton <alex@barton.de>
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -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"
@@ -190,11 +215,12 @@ for f in $sys; do
        fi
 
        sys_target="$target/$fname"
+       sys_root="$sys_target"
        if [ "$DRYRUN" -eq 0 ]; then
                mkdir -p "$sys_target" >/dev/null 2>&1
                if [ $? -ne 0 ]; then
                        echo "Can't create \"$sys_target\"!? \"$system\" skipped!"
-                       echo continue
+                       echo; continue
                fi
        fi
 
@@ -269,6 +295,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,16 +353,20 @@ for f in $sys; do
                echo "System \"$system\" completed with ERRORS, code $ret!"
        fi
 
-       # Clean up old generations
+       # execute job "post-exec" command, if any
+       if [ -n "$job_post_exec" ]; then
+               ExecJob post "$job_post_exec"
+       fi
+
        if [ $generations -gt 0 ]; then
-               sys_target="$target/$fname"
-               to_delete=`ls -1t "$sys_target" 2>/dev/null | tail -n+$generations | sort`
+               # Clean up old generations
+               to_delete=`ls -1t "$sys_root" 2>/dev/null | tail -n+$generations | sort`
                if [ -n "$to_delete" -a $ok -eq 1 ]; then
                        [ "$DRYRUN" -eq 0 ] \
                                && echo "Deleting old backup generations:" \
                                || echo " *** Trial run, not deleting old generations:"
                        for delete in $to_delete; do
-                               dir="$sys_target/$delete"
+                               dir="$sys_root/$delete"
                                if [ ! -e "$dir/.stamp" ]; then
                                        echo "Not deleting \"$dir\", not a backup directory!?"
                                        continue