Skip to content
This repository has been archived by the owner on Jul 18, 2021. It is now read-only.

Commit

Permalink
Implemented mail notification via mailx after each backup
Browse files Browse the repository at this point in the history
  • Loading branch information
mrclschstr committed Jul 20, 2019
1 parent 071abc1 commit 3068433
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM alpine:3.10

# Prepare alpine image
RUN echo https://nl.alpinelinux.org/alpine/v3.10/community >> /etc/apk/repositories \
&& apk add --update --no-cache ca-certificates fuse openssh-client nfs-utils \
&& apk add --update --no-cache ca-certificates fuse openssh-client nfs-utils heirloom-mailx \
&& mkdir -p /mnt/restic /var/spool/cron/crontabs /var/log \
&& touch /var/log/cron.log

Expand All @@ -22,6 +22,7 @@ ENV RESTIC_FORGET_ARGS=""
ENV RESTIC_JOB_ARGS=""
ENV AWS_ACCESS_KEY_ID=""
ENV AWS_SECRET_ACCESS_KEY=""
ENV MAILX_ARGS=""

# /data is the dir where you have to put the data to be backed up
VOLUME /data
Expand Down
39 changes: 24 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ At the moment (July 2019) the container [lobaro/restic-backup-docker](https://gi

To use this container just use the following docker command on your shell:

```
```console
docker pull mrclschstr/restic-backup-docker
```

To enter your container execute:

```
```console
docker exec -ti <your-container-name> /bin/sh
```

Expand All @@ -48,17 +48,18 @@ The container is setup by setting [environment variables](https://docs.docker.co

## Environment variables

| Docker Environment Variable | Mandatory | Default | Description |
|------------------------------|------------|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `RESTIC_REPOSITORY` | Yes | `/mnt/restic` | The location of the restic repository. For S3: `s3:https://s3.amazonaws.com/BUCKET_NAME`. |
| `RESTIC_PASSWORD` | Yes | *empty* | The password for the restic repository. Will also be used for restic init during first start when the repository is not initialized. |
| `RESTIC_TAG` | No | *empty* | To tag the images created by the container. |
| `NFS_TARGET` | No | *empty* | If set the given NFS is mounted, i.e. `mount -o nolock -v ${NFS_TARGET} /mnt/restic`. `RESTIC_REPOSITORY` must remain it's default value! |
| `BACKUP_CRON` | No | `0 */6 * * *` | A cron expression to run the backup. Note: cron daemon uses UTC time zone. |
| `RESTIC_FORGET_ARGS` | No | *empty* | Only if specified `restic forget` is run with the given arguments after each backup. Example value: `-e "RESTIC_FORGET_ARGS=--prune --keep-last 10 --keep-hourly 24 --keep-daily 7 --keep-weekly 52 --keep-monthly 120 --keep-yearly 100"`. |
| `RESTIC_JOB_ARGS` | No | *empty* | 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` | No | *empty* | When using restic with AWS S3 storage. |
| `AWS_SECRET_ACCESS_KEY` | No | *empty* | When using restic with AWS S3 storage. |
| Docker Environment Variable | Mandatory | Default | Description |
|------------------------------|------------|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `RESTIC_REPOSITORY` | Yes | `/mnt/restic` | The location of the restic repository. For S3: `s3:https://s3.amazonaws.com/BUCKET_NAME`. |
| `RESTIC_PASSWORD` | Yes | *empty* | The password for the restic repository. Will also be used for restic init during first start when the repository is not initialized. |
| `RESTIC_TAG` | No | *empty* | To tag the images created by the container. |
| `NFS_TARGET` | No | *empty* | If set the given NFS is mounted, i.e. `mount -o nolock -v ${NFS_TARGET} /mnt/restic`, `RESTIC_REPOSITORY` must remain it's default value! |
| `BACKUP_CRON` | No | `0 */6 * * *` | A cron expression to run the backup. **Note:** cron daemon uses UTC time zone. |
| `RESTIC_FORGET_ARGS` | No | *empty* | Only if specified `restic forget` is run with the given arguments after each backup. Example value: `-e "RESTIC_FORGET_ARGS=--prune --keep-last 10 --keep-hourly 24 --keep-daily 7 --keep-weekly 52 --keep-monthly 120 --keep-yearly 100"`. |
| `RESTIC_JOB_ARGS` | No | *empty* | 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` | No | *empty* | When using restic with AWS S3 storage. |
| `AWS_SECRET_ACCESS_KEY` | No | *empty* | When using restic with AWS S3 storage. |
| `MAILX_ARGS` | No | *empty* | If specified, the content of `/var/log/backup-last.log` is sent via mail after each backup. 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

Expand All @@ -70,13 +71,22 @@ Since restic saves the hostname with each snapshot and the hostname of a docker

Either by setting the [environment variable](https://docs.docker.com/engine/reference/run/#env-environment-variables) `HOSTNAME` or with `--hostname` in the [network settings](https://docs.docker.com/engine/reference/run/#network-settings)

## Cron time and timezone

The cron daemon uses UTC time zone by default. You can map the files `/etc/localtime` and `/etc/timezone` read-only to the container to match the time and timezone of your host.

```console
-v /etc/localtime:/etc/localtime:ro \
-v /etc/timezone:/etc/timezone:ro
```

## Backup to SFTP

Since restic needs a **password less login** to the SFTP server make sure you can do `sftp user@host` from inside the container. If you can do so from your host system, the easiest way is to just mount your `.ssh` folder conaining the authorized cert into the container by specifying `-v ~/.ssh:/root/.ssh` as argument for `docker run`.

Now you can simply specify the restic repository to be an [SFTP repository](https://restic.readthedocs.io/en/stable/Manual/#create-an-sftp-repository).

```
```console
-e "RESTIC_REPOSITORY=sftp:user@host:/tmp/backup"
```

Expand All @@ -85,4 +95,3 @@ Now you can simply specify the restic repository to be an [SFTP repository](http
- Use tags for official releases and not just the master branch
- Provide simple docker run examples in README
- Include cronjob for regular restic repository checks (`restic check`)
- Implement mail notifications for certain events (successfull/failed backups, inconsistent repository, ...) &#8594; see [#3](https://github.com/mrclschstr/restic-backup-docker/issues/3)
14 changes: 8 additions & 6 deletions backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ fi
end=`date +%s`
outputAndLog "Finished at $(date +"%Y-%m-%d %H:%M:%S") after $((end-start)) seconds"

# if [ -n ${MAIL_RECEIPIENT} ] && [ grep -sq "/etc/ssmtp/ssmtp.conf" /proc/mounts ]; then
# # INFO https://wiki.archlinux.org/index.php/SSMTP
# # INFO https://decatec.de/home-server/linux-einfach-e-mails-senden-mit-ssmtp/
# # INFO https://gist.github.com/titpetric/114eb27f6e453e3e8849d65ca1a3d360
# mailx -s "Subject" -r "From" ${MAIL_RECEIPIENT} < ${lastLogfile}
# fi
if [ -n ${MAILX_ARGS} ]; then
mailx ${MAILX_ARGS} < ${lastLogfile} > /dev/null 2>&1
if [ $? -eq 0 ]; then
outputAndLog "Mail notification successfully sent."
else
outputAndLog "Sending mail notification FAILED. Please check your SMTP configuration!"
fi
fi

0 comments on commit 3068433

Please sign in to comment.