Skip to content

Commit

Permalink
fix: Send mail notifications via msmtp
Browse files Browse the repository at this point in the history
- since Alpine's mail no longer supports `-S`
- fixes lobaro#82
  • Loading branch information
gschafra committed Nov 17, 2023
1 parent c2ae895 commit c84c4b1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Changed
* BREAKING: Added `msmtp` package to be used to send mail via SMTP, since Alpine's `mail` no longer supports
providing external server configurations via `-S` parameters.

## v1.3.2 (restic 0.16.0)

### Changed
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ The container is set up by setting [environment variables](https://docs.docker.c
* `AWS_ACCESS_KEY_ID` - Optional. When using restic with AWS S3 storage.
* `AWS_SECRET_ACCESS_KEY` - Optional. When using restic with AWS S3 storage.
* `TEAMS_WEBHOOK_URL` - Optional. If specified, the content of `/var/log/backup-last.log` and `/var/log/check-last.log` is sent to your Microsoft Teams channel after each backup and data integrity check.
* `MAILX_ARGS` - Optional. If specified, the content of `/var/log/backup-last.log` and `/var/log/check-last.log` is sent via mail after each backup and data integrity check using an *external SMTP*. To have maximum flexibility, you have to specify the mail/smtp parameters on your own. Have a look at the [mailx manpage](https://linux.die.net/man/1/mailx) for further information. Example value: `-e "MAILX_ARGS=-r 'from@example.de' -s 'Result of the last restic run' -S smtp='smtp.example.com:587' -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user='username' -S smtp-auth-password='password' 'to@example.com'"`.
* `MAILX_ARGS` - Optional. If specified, the content of `/var/log/backup-last.log` and `/var/log/check-last.log` is sent via mail after each backup and data integrity check using an *external SMTP*. To have maximum flexibility, you have to specify the mail/smtp parameters on your own. Have a look at the [mailx manpage](https://linux.die.net/man/1/mailx) for further information. Example value: `-e "MAILX_ARGS=-r 'from@example.de' -s 'Result of the last restic run' 'to@example.com'"`. ***ATTENTION: A [msmtp](https://wiki.alpinelinux.org/wiki/Relay_email_to_gmail_(msmtp,_mailx,_sendmail)) config file must be provided (by mounting `/config/msmtprc`) for the mail sending to work using an external SMTP server/relay***
* `OS_AUTH_URL` - Optional. When using restic with OpenStack Swift container.
* `OS_PROJECT_ID` - Optional. When using restic with OpenStack Swift container.
* `OS_PROJECT_NAME` - Optional. When using restic with OpenStack Swift container.
Expand All @@ -146,6 +146,7 @@ The container is set up by setting [environment variables](https://docs.docker.c
## Volumes

* `/data` - This is the data that gets backed up. Just [mount](https://docs.docker.com/engine/reference/run/#volume-shared-filesystems) it to wherever you want.
* `/config/msmtprc` - Optional. If specified, the file is copied to `/etc/msmtprc` and used for sending mails via an external SMTP server/relay. ***ATTENTION: The file must be provided for the mail sending to work using an external SMTP server/relay***

## Set the hostname

Expand Down Expand Up @@ -211,13 +212,15 @@ services:
- ./post-backup.sh:/hooks/post-backup.sh:ro # Run script post-backup.sh after every backup
- ./post-check.sh:/hooks/post-check.sh:ro # Run script post-check.sh after every check
- ./ssh:/root/.ssh # SSH keys and config so we can login to "storageserver" without password
- ./msmtprc:/config/msmtprc:ro # Mail config for sending mails via an external SMTP server/relay
environment:
- RESTIC_REPOSITORY=sftp:storageserver:/storage/nas # Backup to server "storageserver"
- RESTIC_PASSWORD=passwordForRestic # Password restic uses for encryption
- BACKUP_CRON=0 22 * * 0 # Start backup every Sunday 22:00 UTC
- CHECK_CRON=0 22 * * 3 # Start check every Wednesday 22:00 UTC
- RESTIC_DATA_SUBSET=50G # Download 50G of data from "storageserver" every Wednesday 22:00 UTC and check the data integrity
- RESTIC_FORGET_ARGS=--prune --keep-last 12 # Only keep the last 12 snapshots
- MAILX_ARGS=-r 'from@example.de' -s 'Result of the last restic run' 'to@example.com'
```

# Versioning
Expand Down
4 changes: 2 additions & 2 deletions backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ if [ -n "${TEAMS_WEBHOOK_URL}" ]; then
fi
fi

if [ -n "${MAILX_ARGS}" ]; then
sh -c "mail -v -S sendwait ${MAILX_ARGS} < ${lastLogfile} > ${lastMailLogfile} 2>&1"
if [ -n "${MAILX_ARGS}" ] && [ -f /config/msmtprc ]; then
sh -c "mail -v ${MAILX_ARGS} < ${lastLogfile} > ${lastMailLogfile} 2>&1"
if [ $? == 0 ]; then
echo "Mail notification successfully sent."
else
Expand Down
4 changes: 2 additions & 2 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ if [ -n "${TEAMS_WEBHOOK_URL}" ]; then
fi
fi

if [ -n "${MAILX_ARGS}" ]; then
sh -c "mail -v -S sendwait ${MAILX_ARGS} < ${lastLogfile} > ${lastMailLogfile} 2>&1"
if [ -n "${MAILX_ARGS}" ] && [ -f /config/msmtprc ]; then
sh -c "mail -v ${MAILX_ARGS} < ${lastLogfile} > ${lastMailLogfile} 2>&1"
if [ $? == 0 ]; then
echo "Mail notification successfully sent."
else
Expand Down
10 changes: 10 additions & 0 deletions entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ if [ -n "${CHECK_CRON}" ]; then
echo "${CHECK_CRON} /usr/bin/flock -n /var/run/backup.lock /bin/check >> /var/log/cron.log 2>&1" >> /var/spool/cron/crontabs/root
fi

# Copy msmtp config file from volume to /etc/msmtprc if exists and chown
# ATTENTION: E-mail notification will only work if this files exists
if [ -f /config/msmtprc ]; then
echo "Found msmtp config file in /config/msmtprc"
cp /config/msmtprc /etc/msmtprc
chmod 600 /etc/msmtprc
else
echo "No msmtp config file found in /config/msmtprc"
fi

# Make sure the file exists before we start tail
touch /var/log/cron.log

Expand Down
18 changes: 18 additions & 0 deletions msmtprc.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Set default values for all following accounts.
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
syslog on

# Gmail
account gmail
host smtp.gmail.com
port 587
from from@gmail.com
user user@gmail.com
password XXXXXXXXXXXXXXXXXXXXX

# Set a default account
account default : gmail
aliases /etc/aliases

0 comments on commit c84c4b1

Please sign in to comment.