From c84c4b1e21a84967eaead6bd5d30be7f2d6b4d69 Mon Sep 17 00:00:00 2001 From: gschafra Date: Fri, 17 Nov 2023 06:41:48 +0000 Subject: [PATCH 1/2] fix: Send mail notifications via msmtp - since Alpine's mail no longer supports `-S` - fixes #82 --- CHANGELOG.md | 4 ++++ README.md | 5 ++++- backup.sh | 4 ++-- check.sh | 4 ++-- entry.sh | 10 ++++++++++ msmtprc.example | 18 ++++++++++++++++++ 6 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 msmtprc.example diff --git a/CHANGELOG.md b/CHANGELOG.md index c0d6c82..13d821f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index a75fcd6..61e7177 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -211,6 +212,7 @@ 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 @@ -218,6 +220,7 @@ services: - 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 diff --git a/backup.sh b/backup.sh index 6391ebc..b243586 100755 --- a/backup.sh +++ b/backup.sh @@ -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 diff --git a/check.sh b/check.sh index 56d18d7..2f3fe45 100755 --- a/check.sh +++ b/check.sh @@ -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 diff --git a/entry.sh b/entry.sh index de51760..a184495 100755 --- a/entry.sh +++ b/entry.sh @@ -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 diff --git a/msmtprc.example b/msmtprc.example new file mode 100644 index 0000000..34aa532 --- /dev/null +++ b/msmtprc.example @@ -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 \ No newline at end of file From e5cd563fceeb5a402c147c05f435d1dc1d0ce4f3 Mon Sep 17 00:00:00 2001 From: gschafra Date: Sat, 20 Jan 2024 21:27:32 +0100 Subject: [PATCH 2/2] fix: Missing msmtp apk and msmtp -> sendmail link --- Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 9cf220b..24569d4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,10 +6,12 @@ RUN unzip rclone-current-linux-amd64.zip && mv rclone-*-linux-amd64/rclone /bin/ FROM restic/restic:0.16.0 -RUN apk add --update --no-cache curl mailx +RUN apk add --update --no-cache curl mailx msmtp COPY --from=rclone /bin/rclone /bin/rclone +RUN ["ln", "-sf", "/usr/bin/msmtp", "/usr/sbin/sendmail"] + RUN \ mkdir -p /mnt/restic /var/spool/cron/crontabs /var/log; \ touch /var/log/cron.log;