]> arthur.barton.de Git - backup-script.git/blobdiff - bin/backup-script
Fix error handling if target directory can't be created
[backup-script.git] / bin / backup-script
index c8edf82f40819e936805fb749dbf45c2dce44429..97cb941013f12c04d7340968f49eb403833035e6 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/bash
 #
 # backup-script system for cloning systems using rsync
-# Copyright (c)2008-2013 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
@@ -11,7 +11,6 @@
 #
 
 NAME=`basename $0`
-CONF_D="/etc/backup-script.d"
 PIDFILE="/var/run/$NAME.pid"
 
 DRYRUN=0
@@ -27,6 +26,7 @@ declare -i count_ok_vanished=0
 destinations=""
 
 # Default settings, can be overwritten in backup-script.conf:
+conf_d="/etc/backup-script.d"
 pre_exec=""
 post_exec=""
 default_target=""
@@ -88,25 +88,30 @@ while [ $# -gt 0 ]; do
        esac
 done
 
+trap GotSignal SIGINT
+
+echo -n "Started: "; date
+
+for conf in "/etc/backup-script.conf" "${conf_d}/backup-script.conf"; do
+       if [ -r "$conf" ]; then
+               echo "Reading configuration: \"$conf\" ..."
+               source "$conf"
+       fi
+done
+echo
+
 if [ $# -ge 1 ]; then
        for s in $@; do
-               if [ ! -r "${CONF_D}/$s" ]; then
-                       echo "$NAME: Can' read \"${CONF_D}/$s\"!"
+               if [ ! -r "${conf_d}/$s" ]; then
+                       echo "$NAME: Can' read \"${conf_d}/$s\"!"
                        exit 1
                fi
-               sys="$sys ${CONF_D}/$s"
+               sys="$sys ${conf_d}/$s"
        done
 else
-       sys=${CONF_D}/*
+       sys=${conf_d}/*
 fi
 
-trap GotSignal SIGINT
-
-echo -n "Started: "; date
-echo
-
-[ -r "${CONF_D}/backup-script.conf" ] && source "${CONF_D}/backup-script.conf"
-
 # check and create PID file
 if [ -e "$PIDFILE" ]; then
        echo "Lockfile \"$PIDFILE\" already exists."
@@ -170,7 +175,7 @@ for f in $sys; do
                || systxt="\"$fname\" [\"$system\"]"
        [ "$local" -eq 0 ] \
                && echo "Working on $systxt ..." \
-               || echo "Working on $sytxts (local system) ..."
+               || echo "Working on $systxt (local system) ..."
 
        count_all=$count_all+1
 
@@ -189,7 +194,7 @@ for f in $sys; do
                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
 
@@ -283,6 +288,7 @@ for f in $sys; do
        echo -n "Start date: "; date
        echo "$cmd"
        count_started=$count_started+1
+       ok=0
        
        if [ "$DRYRUN" -eq 0 ]; then
                rm -f "$sys_target/.stamp"
@@ -305,10 +311,43 @@ for f in $sys; do
 
                echo "System \"$system\" completed with status $ret, OK."
                [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
+               ok=1
        else
                echo "System \"$system\" completed with ERRORS, code $ret!"
        fi
 
+       # Clean up old generations
+       if [ $generations -gt 0 ]; then
+               sys_target="$target/$fname"
+               to_delete=`ls -1t "$sys_target" 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"
+                               if [ ! -e "$dir/.stamp" ]; then
+                                       echo "Not deleting \"$dir\", not a backup directory!?"
+                                       continue
+                               fi
+                               last=`stat "$dir/.stamp" 2>/dev/null | grep "^Modify: " \
+                                | cut -d':' -f2- | cut -d. -f1`
+                               echo "Removing backup from" $last "..."
+                               if [ "$DRYRUN" -eq 0 ]; then
+                                       btrfs subvolume delete \
+                                        "$dir" >/dev/null 2>&1
+                                       [ $? -eq 0 ] || \
+                                        echo "Failed to delete \"$dir\"!"
+                               fi
+                       done
+                       echo -n "Clean up finished: "; date
+               elif [ -n "$to_delete" ]; then
+                       echo "There have been errors, not cleaning up old generations!"
+               else
+                       echo "Nothing to clean up."
+               fi
+       fi
+
        destinations="$destinations $target"
        echo
 done