Skip to content

Commit 7669389

Browse files
committed
Refactored error send script to support more cases
"resticprofile-send-error" is included in posix packages
1 parent b7fab74 commit 7669389

File tree

3 files changed

+118
-70
lines changed

3 files changed

+118
-70
lines changed

contrib/systemd/README.md renamed to contrib/notification-scripts/README.md

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,30 @@
1-
# Send an email on error (systemd schedule)
1+
# Email with failure details - "resticprofile-send-error.sh"
22

33
In `profiles.yaml` you set:
44

55
```yaml
66
default:
77
...
88
run-after-fail:
9-
- 'resticprofile-send-error.sh [email protected]'
9+
- 'resticprofile-send-error.sh -s [email protected]'
1010
```
1111
12-
With `/usr/local/bin/resticprofile-send-error.sh` being:
12+
Usage:
1313
14-
```sh
15-
#!/usr/bin/env bash
16-
[[ -z "${PROFILE_NAME}" ]] || sendmail -t <<ERRMAIL
17-
To: $1
18-
From: "resticprofile $(hostname -f)" <$USER@$(hostname -f)>
19-
Subject: restic failed: ${PROFILE_COMMAND} "${PROFILE_NAME}"
20-
Content-Transfer-Encoding: 8bit
21-
Content-Type: text/plain; charset=UTF-8
22-
23-
${ERROR}
24-
25-
----
26-
COMMANDLINE:
27-
28-
${ERROR_COMMANDLINE}
29-
30-
----
31-
STDERR:
32-
33-
${ERROR_STDERR}
34-
35-
----
36-
DETAILS:
37-
38-
$(systemctl status --full "resticprofile-${PROFILE_COMMAND}@profile-${PROFILE_NAME}")
39-
40-
----
41-
CONFIG:
42-
43-
$(resticprofile --name "${PROFILE_NAME}" show)
44-
45-
ERRMAIL
46-
exit 0
14+
```
15+
resticprofile-send-error.sh [options] user1@domain user2@domain ...
16+
Options:
17+
-s Only send mail when operating on schedule (RESTICPROFILE_ON_SCHEDULE=1)
18+
-c command Set the profile command (instead of PROFILE_COMMAND)
19+
-n name Set the profile name (instead of PROFILE_NAME)
20+
-p Print mail to stdout instead of sending it
21+
-f Send mail even when no profile name is specified
4722
```
4823

4924
## Quick installation
5025

5126
```sh
52-
curl -ssL https://github.com/creativeprojects/resticprofile/raw/master/contrib/systemd/resticprofile-send-error.sh \
27+
curl -ssL https://github.com/creativeprojects/resticprofile/raw/master/contrib/notification-scripts/resticprofile-send-error.sh \
5328
> /usr/local/bin/resticprofile-send-error.sh \
5429
&& chmod +x /usr/local/bin/resticprofile-send-error.sh
5530
```
@@ -64,7 +39,7 @@ In this example, the failure is caused by a custom pre-script complaining about
6439
Date: Fri, 23 Apr 2021 23:25:03 +0200
6540
6641
From: "resticprofile hyper1.domain.tl" <[email protected]>
67-
Subject: restic failed: backup "vms"
42+
Subject: restic failed: "backup" in "vms"
6843
6944
run-before backup on profile 'vms': exit status 1
7045
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Error notification sendmail script
4+
#
5+
help() {
6+
cat - <<HELP
7+
Usage $1 [options] user1@domain user2@domain ...
8+
Options:
9+
-s Only send mail when operating on schedule (RESTICPROFILE_ON_SCHEDULE=1)
10+
-c command Set the profile command (instead of PROFILE_COMMAND)
11+
-n name Set the profile name (instead of PROFILE_NAME)
12+
-p Print mail to stdout instead of sending it
13+
-f Send mail even when no profile name is specified
14+
HELP
15+
}
16+
17+
# Parse CLI args
18+
FORCE_SENDING=0
19+
SEND_COMMAND=""
20+
while getopts 'c:fhn:ps' flag ; do
21+
case "${flag}" in
22+
c) PROFILE_COMMAND="${OPTARG}" ;;
23+
f) FORCE_SENDING=1 ;;
24+
n) PROFILE_NAME="${OPTARG}" ;;
25+
p) SEND_COMMAND="cat -" ;;
26+
s) (( ${RESTICPROFILE_ON_SCHEDULE:-0} > 0 )) || exit 0 ;;
27+
*) help "$0" ; exit 0 ;;
28+
esac
29+
done
30+
shift $((OPTIND-1))
31+
32+
# Parameters
33+
MAIL_TO=""
34+
MAIL_FROM="\"resticprofile $(hostname -f)\" <$USER@$(hostname -f)>"
35+
MAIL_SUBJECT="restic failed: \"${PROFILE_COMMAND}\" in \"${PROFILE_NAME}\""
36+
37+
SEND_COMMAND="${SEND_COMMAND:-sendmail -t}"
38+
39+
DETAILS_COMMAND_RESULT=""
40+
DETAILS_COMMAND=""
41+
42+
# Get command to capture output from scheduler ( if in use )
43+
if [[ -d /etc/systemd/ ]] \
44+
&& (( ${RESTICPROFILE_ON_SCHEDULE:-0} > 0 )) \
45+
&& resticprofile --name "${PROFILE_NAME}" show | grep -v -q -E "scheduler:\s*cron" ; then
46+
DETAILS_COMMAND="systemctl status --full \"resticprofile-${PROFILE_COMMAND:-*}@profile-${PROFILE_NAME:-*}\""
47+
fi
48+
49+
# Load parameter overrides
50+
RC_FILE="/etc/resticprofile/$(basename "$0").rc}"
51+
[[ -f "${RC_FILE}" ]] && source "${RC_FILE}"
52+
53+
main() {
54+
if [[ -n "${PROFILE_NAME}" || "${FORCE_SENDING}" == "1" ]] ; then
55+
if [[ -n "${DETAILS_COMMAND}" ]] ; then
56+
DETAILS_COMMAND_RESULT="$(${DETAILS_COMMAND})"
57+
fi
58+
59+
for email in "$@" "${MAIL_TO}" ; do
60+
if [[ "${email}" =~ ^[a-zA-Z0-9_.%+-]+@[a-zA-Z0-9_]+[a-zA-Z0-9_.-]+$ ]] ; then
61+
send_mail "${email}" || echo "Failed sending to \"${email}\""
62+
elif [[ -n "${email}" ]] ; then
63+
echo "Skipping notification for invalid address \"${email}\""
64+
fi
65+
done
66+
fi
67+
return 0
68+
}
69+
70+
send_mail() {
71+
${SEND_COMMAND} <<ERRMAIL
72+
To: $1
73+
From: $MAIL_FROM
74+
Subject: $MAIL_SUBJECT
75+
Content-Transfer-Encoding: 8bit
76+
Content-Type: text/plain; charset=UTF-8
77+
78+
${ERROR:-No error information available}
79+
80+
----
81+
COMMANDLINE:
82+
83+
${ERROR_COMMANDLINE:-N/A}
84+
85+
----
86+
STDERR:
87+
88+
${ERROR_STDERR:-N/A}
89+
90+
----
91+
DETAILS:
92+
93+
${DETAILS_COMMAND_RESULT:-N/A}
94+
95+
----
96+
CONFIG:
97+
98+
$(resticprofile --name "${PROFILE_NAME}" show)
99+
100+
ERRMAIL
101+
}
102+
103+
# Invoke main and exit without error to ensure other error handlers run
104+
main "$@"
105+
exit 0

contrib/systemd/resticprofile-send-error.sh

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)