Restic: send an email on backup failure #202

Closed
reynir wants to merge 0 commits from restic-notifications into main
Owner

The restic docker image has a mechanism for specifying scripts to execute on success and failure. https://github.com/djmaze/resticker?tab=readme-ov-file#execute-commands-after-backup

However, a challenge is that the image comes with very few utilities. You are expected to use curl to notify a web service that then can send an email for you. Thankfully, curl can do everything including sending mail.

There is some tomfoolery in order to setup noreply.data.coop as a general purpose domain for sending notification emails. We can't use data.coop (I think) because mailu manages that.

I have manually tested the curl invocation in a random container that had curl installed and was on the postfix network. First attempt I sent it from noreply@services.data.coop, but that is not in our allowed sender domains. Then I (ab)used noreply@member.data.coop with success. The remaining changes are untested.

I also set restic to exclude backup of its own files just because they are all generated by ansible.

The restic docker image has a mechanism for specifying scripts to execute on success and failure. https://github.com/djmaze/resticker?tab=readme-ov-file#execute-commands-after-backup However, a challenge is that the image comes with very few utilities. You are expected to use curl to notify a web service that then can send an email for you. Thankfully, curl **can do everything** including sending mail. There is some tomfoolery in order to setup noreply.data.coop as a general purpose domain for sending notification emails. We can't use data.coop (I think) because mailu manages that. I have manually tested the curl invocation in a random container that had curl installed **and** was on the postfix network. First attempt I sent it from noreply@services.data.coop, but that is not in our allowed sender domains. Then I (ab)used noreply@member.data.coop with success. The remaining changes are untested. I also set restic to exclude backup of its own files just because they are all generated by ansible.
reynir added 1 commit 2024-03-03 20:24:21 +00:00
samsapti requested changes 2024-03-03 20:43:52 +00:00
Dismissed
samsapti left a comment
Owner

Some suggestions and minor things that need to be fixed, otherwise great work!

Some suggestions and minor things that need to be fixed, otherwise great work!
@ -53,0 +53,4 @@
# mail dance
domain: "noreply.{{ base_domain }}"
allowed_sender_domain: true
mail-from: "noreply@noreply.{{ base_domain }}"
Owner

I suggest restic@noreply.{{ base_domain }} or backup@noreply.{{ base_domain }} instead. Also, let's keep the syntax style with underscores, so mail_from instead of mail-from.

I suggest `restic@noreply.{{ base_domain }}` or `backup@noreply.{{ base_domain }}` instead. Also, let's keep the syntax style with underscores, so `mail_from` instead of `mail-from`.
reynir marked this conversation as resolved
@ -15,6 +15,7 @@ services:
--tag datacoop-volumes
--exclude '*.tmp'
--exclude '/mnt/volumes/mastodon/mastodon_data/cache/'
--exclude '/mnt/volumes/restic/'
Owner

Please change this line to use spaces instead of tabs, and have it on the same indentation level as the above lines.

Please change this line to use spaces instead of tabs, and have it on the same indentation level as the above lines.
reynir marked this conversation as resolved
@ -22,9 +23,13 @@ services:
--keep-weekly 5
--keep-monthly 12
TZ: Europe/Copenhagen
POST_COMMANDS_FAILURE=/run/libexec/failure.sh
Owner

This needs to be with a : instead of = like the rest of the variables.

This needs to be with a `: ` instead of `=` like the rest of the variables.
reynir marked this conversation as resolved
@ -0,0 +1,14 @@
#!/bin/sh
curl smtp://postfix --mail-from {{ services.restic.mail-from }} --mail-rcpt admin-hold@data.coop --upload-file . << END_OF_MAIL
Owner

Our email is admin@data.coop 🙂
Also, mail-from -> mail_from like above.

Our email is `admin@data.coop` 🙂 Also, `mail-from` -> `mail_from` like above.
Author
Owner

Actually we have both for some reason

Actually we have both for some reason
reynir marked this conversation as resolved
reynir added 1 commit 2024-03-04 08:22:07 +00:00
9fb16d3a69 Address comments by @samsapti
We need to use ':' instead of '=' in yaml for environment variable
bindings.
Spurious tab where it should be all spaces
Rename variable mail-from to mail_from to align with existing code style
Nit: change email addresses
samsapti requested changes 2024-03-04 11:22:17 +00:00
Dismissed
samsapti left a comment
Owner

One last thing, then it's approved 💯

One last thing, then it's approved 💯
@ -0,0 +1,14 @@
#!/bin/sh
curl smtp://postfix --mail-from {{ services.restic.mail_from }} --mail-rcpt admin@data.coop --upload-file . << END_OF_MAIL
Owner

Let's use {{ smtp_host }} here 😄

Let's use `{{ smtp_host }}` here 😄
Owner

You mean like smtp://{{ smtp_host }} ?

You mean like `smtp://{{ smtp_host }}` ?
Author
Owner

FINE!

FINE!
samsapti marked this conversation as resolved
reynir added 1 commit 2024-03-04 11:48:59 +00:00
samsapti requested changes 2024-03-04 12:05:57 +00:00
Dismissed
samsapti left a comment
Owner

One last thing (v2.0) 😃

One last thing (v2.0) 😃
@ -0,0 +1,14 @@
#!/bin/sh
curl smtp://{{ smtp_host }} --mail-from {{ services.restic.mail_from }} --mail-rcpt admin@data.coop --upload-file . << END_OF_MAIL
From: Restic backup <{{ services.restic.mail-from }}>
To: admin-hold@data.coop
Owner

Wrong email (perhaps a services.restic.mail_to is appropriate?) 😉

Wrong email (perhaps a `services.restic.mail_to` is appropriate?) 😉
Owner

@reynir To: admin-hold@data.coop

@reynir `To: admin-hold@data.coop`
samsapti marked this conversation as resolved
reynir added 1 commit 2024-03-04 12:31:10 +00:00
reynir added 1 commit 2024-03-04 13:15:58 +00:00
samsapti approved these changes 2024-03-04 14:03:23 +00:00
samsapti left a comment
Owner

LGTM 💯

LGTM 💯
Author
Owner

Manually merged.

Manually merged.
reynir closed this pull request 2024-03-05 08:23:20 +00:00
reynir deleted branch restic-notifications 2024-03-05 08:27:05 +00:00
Author
Owner

I deployed restic again, but this did not redeploy postfix even if its config changed.

I deployed restic again, but this did not redeploy postfix even if its config changed.
Author
Owner

(I have since then redeployed postfix)

(I have since then redeployed postfix)

Pull request closed

Sign in to join this conversation.
No description provided.