From e4b18450158e0f11b75d876d4533a87dd3199f46 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Wed, 18 Dec 2019 21:56:16 +0100 Subject: [PATCH] Wrap scripts to send error mails when they fail; properly fail backup2me.sh --- backup2me.sh | 4 ++-- mail_wrap_script.sh | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100755 mail_wrap_script.sh diff --git a/backup2me.sh b/backup2me.sh index 2ef2a54..78bc691 100755 --- a/backup2me.sh +++ b/backup2me.sh @@ -67,12 +67,12 @@ echo "Done" ### Create local model of the remote's backup directory tree and send it ### echo "Directory structure..." mkdir -p "$BACKUP_CONTENTS_PATH" -eval ${RSYNC_STRUCTURE_CMD[*]} +eval ${RSYNC_STRUCTURE_CMD[*]} || exit 1 echo "Done" ### Backup home directory except some unwanted files ### echo "Backup data..." -eval ${RSYNC_DATA_CMD[*]} +eval ${RSYNC_DATA_CMD[*]} || exit 1 echo "Done" ### Remove local model of the backup directory tree ### diff --git a/mail_wrap_script.sh b/mail_wrap_script.sh new file mode 100755 index 0000000..8d8ae69 --- /dev/null +++ b/mail_wrap_script.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +SCRIPT=$1 + +shift + +OUTPUT=$($SCRIPT "$@" 2>&1) + +if [ $? -ne 0 ]; then + MAILTO=$(cat "$HOME/.admin_mail") + echo "Sending error mail" + echo -e "An error occured during the execution of $SCRIPT.\n\n$OUTPUT" | mailx -s "$(hostname) $SCRIPT failure" "$MAILTO" + echo "Sent" + exit 1 +else + echo -e "$OUTPUT" +fi