Fuh Hoe

Using post-server to check backup-status and send notification mails

We make backups for several Servers via dirvish and want short summaries to be sent via mail for every Backup. If an error or warning occured during rsync or in one of the scripts an extra mail should be sent to a second Mailaddress. We accomnplish that by an post-server script that interprets the logfile, extracts the returnstatus of the rsync and the scripts and sends the emails.

Files:

/etc/dirvish/master.conf:

...
post-server: /etc/dirvish/dirvish_mail
...
# if there is a command, execute it and redirect stderr to 'log', otherwise do an echo for dirvish_mail
pre-client:  [ -x /usr/local/sbin/dirvish_pre-client ]  && /usr/local/sbin/dirvish_pre-client  2>&1 || echo  "PRE_CLIENT_RET_OK - No pre-client installed"
post-client: [ -x /usr/local/sbin/dirvish_post-client ] && /usr/local/sbin/dirvish_post-client 2>&1 || echo "POST_CLIENT_RET_OK - No post-client installed"
...
Sample pre-client:
#!/bin/bash
# dump database
exportall_dbs
ret=$? 
# echo status for dirvish_mail
if [ $ret -ne 0 ]; then
  echo "PRE_CLIENT_RET_FAILED"
else
  echo "PRE_CLIENT_RET_OK"
fi
exit $ret

/etc/dirvish/dirvish_mail:

#
# Send Summary-Mail to $MAILTO, if something bad happens send a second mail to $MAILERR
# dirvish_mail [--erroronly]
#
# EDIT HERE
# Mail-Adress for info and errormails
MAILTO=info@domain.XXX
# errormails only
MAILERR=infoerr@domain.XXX

# Returncode
ERR=0

# DIRVISH-Vars
DV_DIR=$DIRVISH_DEST/..
DV_SUMMARY=$DV_DIR/summary
DV_LOG=$DV_DIR/log
DV_RSYNDERROR=$DV_DIR/rsync_error
DV_CLIENT=$DIRVISH_CLIENT
DV_SRC="$DIRVISH_SRC"
DV_STATUS=$DIRVISH_STATUS
PRE_STATUS=
POST_STATUS=

# Mail-Stuff
MAILTO="${MAILTO:-root}"
MAILERR="${MAILERR:-root}"
SUBJECT="Backup-$DV_CLIENT:$DIRVISH_SRC (backupserver)"
LC_CTYPE=de_DE@euro ; # let mail use 8bit charset
MAILFILE=$DV_DIR/mailfile
MAIL=mail
ERRONLY=false
if [ "$1" == '--erroronly' ]; then
       ERRONLY=:
fi

# Check Rsync-Status
if [ "$DV_STATUS" == success ]; then
       DV_STATUS_OK=:
else
       DV_STATUS_OK=false
fi

# check pre-client Status

if grep -q "^PRE_CLIENT_RET_FAILED" $DV_LOG 2>/dev/null; then
       DV_STATUS_OK=false
       PRE_STATUS=FAILED
elif grep -q "^PRE_CLIENT_RET_OK" $DV_LOG 2>/dev/null; then
       PRE_STATUS=OK
else
       DV_STATUS_OK=false
       PRE_STATUS=UNKNOWN
fi

# check post-client Status

if grep -q "^POST_CLIENT_RET_FAILED" $DV_LOG 2>/dev/null; then
       DV_STATUS_OK=false
       POST_STATUS=FAILED
elif grep -q "^POST_CLIENT_RET_OK" $DV_LOG 2>/dev/null; then
       POST_STATUS=OK
else
       DV_STATUS_OK=false
       POST_STATUS=UNKNOWN
fi

STATUS_TXT="Rsync: $DV_STATUS Pre: $PRE_STATUS, Post: $POST_STATUS"
if $DV_STATUS_OK; then
       if $ERRONLY; then
               # No ERR, no mail wanted -> exit
               exit 0
       fi
       SUBJECT="$SUBJECT: OK"
else
       # CC an root
       MAIL="$MAIL -c $MAILERR"
       SUBJECT="### ERR ### - $SUBJECT: $STATUS_TXT"
       (
       echo "========================================================================="
       # ...
               echo "ERROR:   $DIRVISH_SERVER:$DV_DIR"
       echo "========================================================================="
       echo ) >>$MAILFILE
fi

# Short summary
(
echo "========================================================================="
echo "Status : $STATUS_TXT"
echo "========================================================================="
echo "directory: $DV_DIR"
echo "Summary ($DV_SUMMARY):"
echo "========================================================================="
sed 's/^/SUM:  /' < $DV_SUMMARY        ) >>$MAILFILE

if [ -s $DV_RSYNDERROR ]; then
       (
       echo
       echo "========================================================================="
       echo "Rsync-Errors ($DV_RSYNDERROR):"
       echo "========================================================================="
       ) >>$MAILFILE
       cat $DV_RSYNDERROR | sed 's/^/RSYNC_ERR:        /'      >> $MAILFILE
fi

(
echo
echo "========================================================================="
echo "Logfile ($DV_LOG) (head and tail):"
echo "========================================================================="
) >>$MAILFILE
if $DV_STATUS_OK; then
       if [ -f $DV_LOG ]; then
               (
               echo "LOG:   ..."
               tail -30 $DV_LOG
               echo
               ) | sed 's/^/LOG:   /' >>$MAILFILE
       fi
else
       if [ -f $DV_LOG ]; then
               LINES=`wc -l $DV_LOG | awk '{ print $1 }'`
               if [ $LINES -gt 600 ]; then
                       head -200 $DV_LOG | sed 's/^/LOG:   /'  >>$MAILFILE
                       echo "LOG:   ..."                       >>$MAILFILE
                       tail -200 $DV_LOG | sed 's/^/LOG:   /'  >>$MAILFILE
               else
                       # small enough
                       sed 's/^/LOG:   /' < $DV_LOG    >>$MAILFILE
               fi
       fi
fi

# send mail and exit
$MAIL -s "$SUBJECT" "$MAILTO" <$MAILFILE
$DV_STATUS_OK  || ERR=1
exit $ERR

FuhHoe (last edited 2011-01-24 05:11:12 by KeithLofstrom)