]> arthur.barton.de Git - backup-script.git/blobdiff - bin/backup-script
Fix usage: --dry-run equals -n (not -d)
[backup-script.git] / bin / backup-script
index ba26e7baad655c0e258f52e468048e4c041c9490..c8edf82f40819e936805fb749dbf45c2dce44429 100755 (executable)
@@ -33,34 +33,23 @@ default_target=""
 default_user="root"
 default_ssh_args_add=""
 default_rsync_args_add=""
+default_exclude_args_add=""
 default_compress=1
 default_ping=1
 default_local=0
+default_generations=0
 
 Usage() {
        echo "Usage: $NAME [<options>] [<system> [<system> [...]]]"
        echo
        echo "  -p, --progress    Show progress, see rsync(1)."
-       echo "  -d, --dry-run     Test run only, don't copy any data."
+       echo "  -n, --dry-run     Test run only, don't copy any data."
        echo
        echo "When no <system> is given, all defined systems are used."
        echo
        exit 1
 }
 
-Log() {
-       logger -t "$NAME" "$*"
-}
-
-Message() {
-       echo "$*"
-}
-
-MessageLog() {
-       Log "$*"
-       Message "$*"
-}
-
 CleanUp() {
        if [ -n "$post_exec" ]; then
                echo "Executing \"$post_exec\" ..."
@@ -113,8 +102,6 @@ fi
 
 trap GotSignal SIGINT
 
-Log "Started ..."
-
 echo -n "Started: "; date
 echo
 
@@ -122,7 +109,6 @@ echo
 
 # check and create PID file
 if [ -e "$PIDFILE" ]; then
-       Log "Lockfile \"$PIDFILE\" already exists. Aborting!"
        echo "Lockfile \"$PIDFILE\" already exists."
        echo "Is an other instance still running?"
        echo
@@ -154,53 +140,125 @@ fi
 for f in $sys; do
        [ -r "$f" -a -f "$f" ] || continue
 
-       system=`basename $f`
-       case "$system" in
+       fname=`basename $f`
+       case "$fname" in
                "backup-script.conf"|*.sh)
                        continue
                        ;;
        esac
 
        # Set global defaults
+       system="$fname"
        user="$default_user"
        target="$default_target"
        ssh_args_add="$default_ssh_args_add"
        rsync_args_add="$default_rsync_args_add"
+       exclude_args_add="$default_exclude_args_add"
        compress="$default_compress"
        ping="$default_ping"
        local="$default_local"
+       generations="$default_generations"
 
        # Read in system configuration file
        source "$f"
 
+       # Validate configuration
+       [ "$system" = "localhost" -o "$system" = "127.0.0.1" ] && local=1
+
+       [ "$system" = "$fname" ] \
+               && systxt="\"$system\"" \
+               || systxt="\"$fname\" [\"$system\"]"
        [ "$local" -eq 0 ] \
-               && MessageLog "Working on \"$system\" ..." \
-               || MessageLog "Working on \"$system\" (local system) ..."
+               && echo "Working on $systxt ..." \
+               || echo "Working on $sytxts (local system) ..."
 
        count_all=$count_all+1
 
        # Check target directory
        if [ -z "$target" ]; then
-               MessageLog "No target directory specified for \"$system\"!? Skipped!"
+               echo "No target directory specified for \"$system\"!? Skipped!"
                echo; continue
        fi
        if [ ! -d "$target" ]; then
-               MessageLog "Target \"$target\" is not a directory!? \"$system\" skipped!"
+               echo "Target \"$target\" is not a directory!? \"$system\" skipped!"
                echo; continue
        fi
 
-       destdir="$target"
-       target="$target/$system"
-       [ "$DRYRUN" -gt 1 ] || mkdir -p "$target"
+       sys_target="$target/$fname"
+       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
+               fi
+       fi
 
        if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
                # Check if system is alive
                ping -c 1 "$system" >/dev/null 2>&1
                if [ $? -ne 0 ]; then
-                       MessageLog "Host \"$system\" seems not to be alive!? Skipped."
+                       echo "Host \"$system\" seems not to be alive!? Skipped."
                        echo; continue
                fi
-               Message "OK, host \"$system\" seems to be alive."
+               echo "OK, host \"$system\" seems to be alive."
+       fi
+
+       if [ $generations -gt 0 ]; then
+               # Make sure no old backup is stored in system directory
+               if [ -e "$sys_target/.stamp" ]; then
+                       # There seems to be a genearation-less backup in the
+                       # target directory!
+                       echo "Target directory \"$sys_target\" seems to be unclean!? \"$system\" skipped!"
+                       echo; continue
+               fi
+
+               # Search directory of last generation, if any
+               last="`ls -1 "$sys_target" 2>/dev/null | sort -r | head -n1`"
+               if [ -n "$last" ]; then
+                       last="$sys_target/$last"
+                       if [ ! -d "$last" ]; then
+                               echo "Last snapshot \"$last\" seems not to be a directory!? \"$system\" skipped!"
+                               echo; continue
+                       fi
+               fi
+               sys_target="$sys_target/`date +%Y%m%d-%H%M%S`"
+
+               if [ -n "$last" -a ! -e "$last/.stamp" ]; then
+                       # Old backup directory without "stamp file", continue
+                       echo "Found incomplete snapshot in \"$last\", reusing and renaming it ..."
+                       mv "$last" "$sys_target" >/dev/null 2>&1
+                       if [ $? -ne 0 ]; then
+                               echo "Failed to rename last snapshot \"$last\" to \"$sys_target\"!? \"$system\" skipped!"
+                               echo; continue
+                       fi
+               elif [ -n "$last" ]; then
+                       # Old backup directory found, create new snapshot
+                       echo "Found last snapshot in \"$last\"."
+                       if [ "$DRYRUN" -eq 0 ]; then
+                               btrfs subvolume snapshot \
+                                 "$last" "$sys_target" >/dev/null 2>&1; r=$?
+                               if [ $r -ne 0 ]; then
+                                       echo "Can't create btrfs snapshot \"$sys_target\" of \"$last\", code $r!? \"$system\" skipped!"
+                                       echo; continue
+                               fi
+                               echo "Created new snapshot in \"$sys_target\"."
+                       else
+                               echo " *** Trial run, not creating new snapshot in \"$sys_target\"!"
+                       fi
+               else
+                       # No old backup found, create new subvolume
+                       if [ "$DRYRUN" -eq 0 ]; then
+                               btrfs subvolume create \
+                                 "$sys_target" >/dev/null 2>&1; r=$?
+                               if [ $r -ne 0 ]; then
+                                       echo "Can't create btrfs subvolume \"$sys_target\", code $r!? \"$system\" skipped!"
+                                       echo; continue
+                               fi
+                               echo "Created new subvolume in \"$sys_target\"."
+                       else
+                               echo " *** Trial run, not creating new subvolume \"$sys_target\"!"
+                       fi
+               fi
        fi
 
        ssh_cmd="ssh"
@@ -210,56 +268,55 @@ for f in $sys; do
        [ "$compress" -ne 0 ] && cmd="$cmd --compress"
        cmd="$cmd --rsh=\"$ssh_cmd\" --delete --delete-excluded --sparse"
        [ "$VERBOSE" -gt 0 ] && cmd="$cmd --progress"
-       cmd="$cmd --exclude=/BACKUP --exclude=/backup --exclude=/mnt"
        cmd="$cmd --exclude=/dev --exclude=/proc --exclude=/sys"
-       cmd="$cmd --exclude=/usr/src --exclude=/usr/local/src"
-       cmd="$cmd --exclude=/var/cache/apt --exclude=/var/amavis/blocked"
-       cmd="$cmd --exclude=/var/log --exclude=/tmp --exclude=/var/tmp"
+       cmd="$cmd --exclude=/run --exclude=/tmp --exclude=/var/tmp"
+       cmd="$cmd --exclude=/media --exclude=/mnt --exclude=/net"
+       cmd="$cmd --exclude=/var/cache/apt --exclude=/var/log"
+       [ -n "$exclude_args_add" ] && cmd="$cmd $exclude_args_add"
        [ -n "$rsync_args_add" ] && cmd="$cmd $rsync_args_add"
 
        [ "$local" -eq 0 ] \
-               && cmd="$cmd ${user}@${system}:/ $target" \
-               || cmd="$cmd / $target"
+               && cmd="$cmd ${user}@${system}:/ $sys_target/" \
+               || cmd="$cmd / $sys_target/"
 
-       Message "Calling: $cmd"
+       echo "Backing up to \"$sys_target\" ..."
        echo -n "Start date: "; date
+       echo "$cmd"
        count_started=$count_started+1
-       rm -f "$target/.stamp"
        
-       if [ "$DRYRUN" -lt 1 ]; then
+       if [ "$DRYRUN" -eq 0 ]; then
+               rm -f "$sys_target/.stamp"
                $SHELL -c "$cmd"; ret=$?
-               echo "code=$ret" >"$target/.stamp"
+               echo "code=$ret" >"$sys_target/.stamp"
        else
-               MessageLog " *** Trial run, not executing synchronization command!"
+               echo " *** Trial run, not executing synchronization command!"
                ret=0
        fi
 
        if [ $ret -eq 20 ]; then
-               MessageLog "Backup of \"$system\" interrupted. Aborting ..."
+               echo "Backup of \"$system\" interrupted. Aborting ..."
                CleanUp
                exit 1
        fi
 
+       echo -n "End date: "; date
        if [ $ret -eq 0 -o $ret -eq 24 ]; then
                [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
 
-               MessageLog "System \"$system\" completed with status $ret, OK."
+               echo "System \"$system\" completed with status $ret, OK."
                [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
        else
-               MessageLog "System \"$system\" completed with ERRORS, code $ret!"
+               echo "System \"$system\" completed with ERRORS, code $ret!"
        fi
-       echo -n "End date: "; date
 
-       destinations="$destinations $destdir"
+       destinations="$destinations $target"
        echo
 done
 
 sync
 
-Log "Done: $count_all jobs, $count_started started, $count_ok completed without errors."
-
 paths=$( echo $destinations | sed -e 's/ /\n/g' | sort | uniq )
-if [ -n "$paths" ]; then
+if [ "$DRYRUN" -eq 0 -a -n "$paths" ]; then
        df -h $paths
        echo
 fi
@@ -268,8 +325,10 @@ CleanUp
 
 echo -n "Done: "; date
 echo
-echo " - $count_all jobs defined,"
-echo " - $count_started jobs started,"
+[ $count_all -eq 1 ] && s="" || s="s"
+echo " - $count_all job$s defined,"
+[ $count_started -eq 1 ] && s="" || s="s"
+echo " - $count_started job$s started,"
 echo " - $count_ok done without errors."
 echo