]> arthur.barton.de Git - backup-script.git/blobdiff - bin/backup-script
Add /run to hardcoded exclude list
[backup-script.git] / bin / backup-script
index ee9646823bef4fcc973b49ffc1736eea7601e6c8..ad5e628220c6f2c96f5a15babcdcb4a44f4dd203 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/bash
 #
 # backup-script system for cloning systems using rsync
-# Copyright (c)2008-2011 Alexander Barton, alex@barton.de
+# Copyright (c)2008-2013 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
 
 NAME=`basename $0`
 CONF_D="/etc/backup-script.d"
-VERBOSE=0
 PIDFILE="/var/run/$NAME.pid"
 
+DRYRUN=0
+VERBOSE=0
+
 export LC_ALL=C
 
 declare -i count_all=0
@@ -23,22 +25,29 @@ declare -i count_ok=0
 declare -i count_ok_vanished=0
 
 destinations=""
+
+# Default settings, can be overwritten in backup-script.conf:
 pre_exec=""
 post_exec=""
 default_target=""
 default_user="root"
-
-if [ "$1" == "-p" ]; then
-       VERBOSE=1
-       shift
-fi
-
-case "$1" in
-    "-"*)
-       echo "Usage: $NAME [-p] [<system> [<system> [...]]]"
+default_ssh_args_add=""
+default_rsync_args_add=""
+default_exclude_args_add=""
+default_compress=1
+default_ping=1
+default_local=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
+       echo "When no <system> is given, all defined systems are used."
+       echo
        exit 1
-       ;;
-esac
+}
 
 Log() {
        logger -t "$NAME" "$*"
@@ -75,6 +84,22 @@ GotSignal() {
        exit 9
 }
 
+while [ $# -gt 0 ]; do
+       case "$1" in
+         "-n"|"--dry-run")
+               DRYRUN=1; shift
+               ;;
+         "-p"|"--progress")
+               VERBOSE=1; shift
+               ;;
+         "-"*)
+               Usage
+               ;;
+         *)
+               break
+       esac
+done
+
 if [ $# -ge 1 ]; then
        for s in $@; do
                if [ ! -r "${CONF_D}/$s" ]; then
@@ -131,21 +156,23 @@ for f in $sys; do
        [ -r "$f" -a -f "$f" ] || continue
 
        system=`basename $f`
-       user="$default_user"
-       target="$default_target"
-       ssh_args_add=""
-       rsync_args_add=""
-       compress=1
-       ping=1
-       local=0
-
        case "$system" in
                "backup-script.conf"|*.sh)
                        continue
                        ;;
        esac
 
-       # Read in configuration file
+       # Set global defaults
+       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"
+
+       # Read in system configuration file
        source "$f"
 
        [ "$local" -eq 0 ] \
@@ -156,17 +183,17 @@ for f in $sys; do
 
        # Check target directory
        if [ -z "$target" ]; then
-               MessageLog "No target directory specified for \"$system\"!? Skipped."
+               MessageLog "No target directory specified for \"$system\"!? Skipped!"
                echo; continue
        fi
        if [ ! -d "$target" ]; then
-               MessageLog "Target \"$target\" is not a directory!? \"$system\" skipped."
+               MessageLog "Target \"$target\" is not a directory!? \"$system\" skipped!"
                echo; continue
        fi
 
        destdir="$target"
        target="$target/$system"
-       mkdir -p "$target"
+       [ "$DRYRUN" -gt 1 ] || mkdir -p "$target"
 
        if [ "$local" -eq 0 -a "$ping" -ne 0 ]; then
                # Check if system is alive
@@ -185,11 +212,11 @@ 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 ] \
@@ -201,8 +228,13 @@ for f in $sys; do
        count_started=$count_started+1
        rm -f "$target/.stamp"
        
-       $SHELL -c "$cmd"; ret=$?
-       echo "code=$ret" >"$target/.stamp"
+       if [ "$DRYRUN" -lt 1 ]; then
+               $SHELL -c "$cmd"; ret=$?
+               echo "code=$ret" >"$target/.stamp"
+       else
+               MessageLog " *** Trial run, not executing synchronization command!"
+               ret=0
+       fi
 
        if [ $ret -eq 20 ]; then
                MessageLog "Backup of \"$system\" interrupted. Aborting ..."
@@ -214,7 +246,7 @@ for f in $sys; do
                [ $ret -eq 24 ] && count_ok_vanished=$count_ok_vanished+1
 
                MessageLog "System \"$system\" completed with status $ret, OK."
-               count_ok=$count_ok+1
+               [ "$DRYRUN" -gt 0 ] || count_ok=$count_ok+1
        else
                MessageLog "System \"$system\" completed with ERRORS, code $ret!"
        fi