]> arthur.barton.de Git - backup-script.git/blobdiff - bin/backup-script
Catch SIGTERM as well as SIGINT
[backup-script.git] / bin / backup-script
index 04572682d78832f7acde423f8b8013f41e7deedd..a235190c3038a67f6941965e1099ee98443cfc24 100755 (executable)
@@ -61,6 +61,7 @@ Usage() {
        echo
        echo "When no <system> is given, all defined systems are used."
        echo
+       # shellcheck disable=SC2086
        echo -e $config_info
        echo
        exit 2
@@ -80,7 +81,7 @@ CleanUp() {
 
 GotSignal() {
        echo
-       echo "--> Got break signal, cleaning up & aborting ..."
+       echo "--> Got signal, cleaning up & aborting ..."
        echo
        CleanUp
        echo -n "Aborted: "; date
@@ -234,6 +235,7 @@ Initialize_Last_SysTarget_Snapshot() {
        case "$fs" in
          "btrfs")
                # Search directory of last generation, if any
+               # shellcheck disable=SC2012
                last=$(ls -1d "$sys_target"/[0-9]* 2>/dev/null | sort -r | head -n1)
                if [ -n "$last" ]; then
                        if [ ! -d "$last" ]; then
@@ -330,8 +332,6 @@ echo "Rsync command is $rsync, protocol version $rsync_proto."
 [[ -n "$TAG" ]] && echo "Running jobs tagged with \"$TAG\"."
 echo
 
-trap GotSignal SIGINT
-
 if [ $# -ge 1 ]; then
        for s in "$@"; do
                if [ ! -r "${conf_d}/$s" ]; then
@@ -349,7 +349,6 @@ if [ -n "$setup_exec" ]; then
        sh -c $setup_exec
        if [ $? -ne 0 ]; then
                echo "Error: setup command failed!"; echo
-               CleanUp
                echo "Aborting backup."; echo
                exit 5
        fi
@@ -357,6 +356,8 @@ if [ -n "$setup_exec" ]; then
        echo
 fi
 
+trap GotSignal SIGINT SIGTERM
+
 # check and create PID file
 if [ -e "$PIDFILE" ]; then
        echo "Lockfile \"$PIDFILE\" already exists."
@@ -659,12 +660,14 @@ for f in "${sys[@]}"; do
                $SHELL -c "$cmd"; ret=$?
                end_t=$(date "+%s")
 
-               echo "code=$ret" >"$stamp_file"
-               echo "start_t=$start_t" >>"$stamp_file"
-               echo "end_t=$end_t" >>"$stamp_file"
-               echo "cmd='$cmd'" >>"$stamp_file"
-               echo "backup_host='`hostname -f`'" >>"$stamp_file"
-               echo "backup_user='`id -un`'" >>"$stamp_file"
+               {
+                       echo "code=$ret"
+                       echo "start_t=$start_t"
+                       echo "end_t=$end_t"
+                       echo "cmd='$cmd'"
+                       echo "backup_host='$(hostname -f)'"
+                       echo "backup_user='$(id -un)'"
+               } >"$stamp_file"
        else
                echo " *** Trial run, not executing save command!"
                ret=0
@@ -699,6 +702,7 @@ for f in "${sys[@]}"; do
                fi
                # Clean up old generations
                declare -i gen_count=$generations+2
+               # shellcheck disable=SC2012
                to_delete=$(ls -1t "$sys_root" 2>/dev/null | tail -n+$gen_count | sort)
                if [[ -n "$to_delete" && $ok -eq 1 ]]; then
                        [ "$DRYRUN" -eq 0 ] \
@@ -712,6 +716,7 @@ for f in "${sys[@]}"; do
                                fi
                                last=$(stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
                                 | cut -d':' -f2- | cut -d. -f1)
+                               # shellcheck disable=SC2086
                                echo "Removing backup from" $last "..."
                                if [ "$DRYRUN" -eq 0 ]; then
                                        DeleteSubvolume "$dir"
@@ -736,6 +741,7 @@ sync
 if [ "$DRYRUN" -eq 0 ]; then
        paths=""
        paths_zfs=""
+       # shellcheck disable=SC2086
        for dest in $(echo $destinations | sed -e 's/ /\n/g' | sort | uniq); do
                fs=$(GetFS "$dest")
                case $fs in
@@ -747,10 +753,12 @@ if [ "$DRYRUN" -eq 0 ]; then
                esac
        done
        if [ -n "$paths" ]; then
+               # shellcheck disable=SC2086
                df -h $paths
                echo
        fi
        if [ -n "$paths_zfs" ]; then
+               # shellcheck disable=SC2086
                zfs list $paths_zfs
                echo
        fi