From bfb9e34b673d7ecde0d6543550560f6fda9bdd48 Mon Sep 17 00:00:00 2001 From: mrclschstr Date: Wed, 24 Jul 2019 10:25:48 +0200 Subject: [PATCH 1/2] Implemented a simple mail notification after backups using mailx. This can be used to send mail via an external SMTP server. Syntax uniformity is questionable. --- Dockerfile | 3 ++- README.md | 3 ++- backup.sh | 12 +++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 200334b..936551f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,7 @@ FROM alpine:3.10.1 COPY --from=build /etc/ssl/certs /etc/ssl/certs COPY --from=build /bin/restic /bin/restic -RUN apk add --update --no-cache fuse openssh-client +RUN apk add --update --no-cache fuse openssh-client heirloom-mailx RUN \ mkdir -p /mnt/restic /var/spool/cron/crontabs /var/log; \ @@ -25,6 +25,7 @@ ENV \ BACKUP_CRON="0 */6 * * *" \ RESTIC_FORGET_ARGS="" \ RESTIC_JOB_ARGS="" + MAILX_ARGS="" # /data is the dir where you have to put the data to be backed up VOLUME /data diff --git a/README.md b/README.md index cf0723d..a0ec047 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ docker logs ``` Shows `/var/log/cron.log` -Additionally you can see the the full log, including restic output, of the last execution in `/var/log/backup-last.log`. When the backup fails the log is copied to `/var/log/restic-error-last.log`. +Additionally you can see the the full log, including restic output, of the last execution in `/var/log/backup-last.log`. When the backup fails the log is copied to `/var/log/restic-error-last.log`. If configured, you can find the full output of the mail notification in `/var/log/mail-last.log`. # Customize the Container @@ -77,6 +77,7 @@ The container is setup by setting [environment variables](https://docs.docker.co * `RESTIC_JOB_ARGS` - Optional. Allows to specify extra arguments to the back up job such as limiting bandwith with `--limit-upload` or excluding file masks with `--exclude`. * `AWS_ACCESS_KEY_ID` - Optional. When using restic with AWS S3 storage. * `AWS_SECRET_ACCESS_KEY` - Optional. When using restic with AWS S3 storage. +* `MAILX_ARGS` - Optional. If specified, the content of `/var/log/backup-last.log` is sent via mail after each backup using an *external SMTP*. To have maximum flexibility, you have to specify the mail/smtp parameters by 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 backup 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'"`. ## Volumes diff --git a/backup.sh b/backup.sh index 6a19b36..ef254a0 100755 --- a/backup.sh +++ b/backup.sh @@ -1,6 +1,7 @@ #!/bin/sh lastLogfile="/var/log/backup-last.log" +lastMailLogfile="/var/log/mail-last.log" copyErrorLog() { cp ${lastLogfile} /var/log/backup-error-last.log @@ -11,7 +12,7 @@ logLast() { } start=`date +%s` -rm -f ${lastLogfile} +rm -f ${lastLogfile} ${lastMailLogfile} echo "Starting Backup at $(date +"%Y-%m-%d %H:%M:%S")" echo "Starting Backup at $(date)" >> ${lastLogfile} logLast "BACKUP_CRON: ${BACKUP_CRON}" @@ -50,3 +51,12 @@ fi end=`date +%s` echo "Finished Backup at $(date +"%Y-%m-%d %H:%M:%S") after $((end-start)) seconds" + +if [ -n "${MAILX_ARGS}" ]; then + sh -c "mailx -v -S sendwait ${MAILX_ARGS} < ${lastLogfile} > ${lastMailLogfile} 2>&1" + if [ $? == 0 ]; then + echo "Mail notification successfully sent." + else + echo "Sending mail notification FAILED. Check ${lastMailLogfile} for further information." + fi +fi From 4a1a5519235e91b856f94f4d89c9b8da71ea94fe Mon Sep 17 00:00:00 2001 From: mrclschstr Date: Thu, 25 Jul 2019 09:59:58 +0200 Subject: [PATCH 2/2] Fixed missing backslash by separating ENV lines again --- Dockerfile | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 936551f..8d75eb2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,15 +17,14 @@ RUN \ mkdir -p /mnt/restic /var/spool/cron/crontabs /var/log; \ touch /var/log/cron.log; -ENV \ - RESTIC_REPOSITORY=/mnt/restic \ - RESTIC_PASSWORD="" \ - RESTIC_TAG="" \ - NFS_TARGET="" \ - BACKUP_CRON="0 */6 * * *" \ - RESTIC_FORGET_ARGS="" \ - RESTIC_JOB_ARGS="" - MAILX_ARGS="" +ENV RESTIC_REPOSITORY=/mnt/restic +ENV RESTIC_PASSWORD="" +ENV RESTIC_TAG="" +ENV NFS_TARGET="" +ENV BACKUP_CRON="0 */6 * * *" +ENV RESTIC_FORGET_ARGS="" +ENV RESTIC_JOB_ARGS="" +ENV MAILX_ARGS="" # /data is the dir where you have to put the data to be backed up VOLUME /data