-#!/bin/sh
+#!/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
# Please read the file COPYING, README and AUTHORS for more information.
#
-[ -n "$MAILTO" ] || MAILTO="logcheck"
+if [ -z "$MAILTO" ]; then
+ if id "logcheck" >/dev/null 2>&1; then
+ MAILTO="logcheck"
+ elif fgrep "logcheck:" /etc/aliases >/dev/null 2>&1; then
+ MAILTO="logcheck"
+ else
+ MAILTO="root"
+ fi
+fi
NAME="backup-script"
-HOST=`hostname`
-TMP=`mktemp /tmp/$NAME.XXXXXXXX` || exit 11
+
+DELIMITER="- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
+HOST=`hostname -f`
+LOGFILE="/var/log/$NAME.log"
+PIDFILE="/var/run/$NAME.pid"
+SUBJECT="$NAME results"
+TMP=`mktemp /tmp/$NAME.XXXXXXXX` || exit 1
PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
export PATH
-echo "$NAME Report" >"$TMP"
-echo >>"$TMP"
-echo " - Host: `hostname -f`" >>"$TMP"
-echo " - User: `id -un`" >>"$TMP"
-echo >>"$TMP"
+# check PID file ("lock file")
+if [ -e "$PIDFILE" ]; then
+ echo "Lockfile \"$PIDFILE\" already exists."
+ echo "Is an other instance still running?"
+ echo
+ echo -n "Aborted: "; date
+ echo
+ exit 4
+fi
+
+exec >"$TMP" 2>&1
+
+echo "$NAME Report"
+echo
+echo " - Host: `hostname -f`"
+echo " - User: `id -un`"
+echo
+
+if echo "$DELIMITER" >>"$LOGFILE" 2>/dev/null; then
+ echo >>"$LOGFILE"
+ "$(dirname "$0")/backup-script" "$@" | tee -a "$LOGFILE"
+ r=${PIPESTATUS[0]}
+else
+ "$(dirname "$0")/backup-script" "$@"
+ r=$?
+ echo "(Can't write logfile: \"$LOGFILE\"!)"
+fi
-`dirname "$0"`/backup-script $* >>"$TMP" 2>&1
+case "$r" in
+ 0)
+ ;;
+ *)
+ SUBJECT="$SUBJECT - with ERRORS!"
+esac
-cat "$TMP" | mail -s "$HOST: $NAME results" "$MAILTO"
+cat "$TMP" | mail -s "$HOST: $SUBJECT" "$MAILTO"
rm -f "$TMP"
+exit $r
# -eof-