Receive notifications
Send email notifications on failed backup runs
To send out email notifications on failed backup runs, provide SMTP credentials, a sender and a recipient:
services:
backup:
image: offen/docker-volume-backup:v2
environment:
# ... other configuration values go here
NOTIFICATION_URLS=smtp://me:secret@smtp.example.com:587/?fromAddress=no-reply@example.com&toAddresses=you@example.com
Notification backends other than email are also supported. Refer to the documentation of shoutrrr to find out about options and configuration.
If you also want notifications on successful executions, set NOTIFICATION_LEVEL to info.
Monitor backup runs with a heartbeat monitoring service
Failure notifications cannot tell you about backups that never ran, for example because the container is stopped or fails before notifications are set up. Heartbeat (dead man’s switch) monitoring services cover this case: they expect a ping on every scheduled run and alert you when it is late or reports a failure.
NOTIFICATION_URLS sends success and failure notifications to the same URLs, so use it for the failure ping only, and send the start and success pings from custom commands labeled on the docker-volume-backup container itself:
services:
backup:
image: offen/docker-volume-backup:v2
environment:
# ... other configuration values go here
NOTIFICATION_URLS: generic+https://monitor.example.com/ping/your-check-uuid/fail
labels:
- docker-volume-backup.archive-pre=/bin/sh -c 'wget -q -T 10 -O /dev/null https://monitor.example.com/ping/your-check-uuid/start || true'
- docker-volume-backup.prune-post=/bin/sh -c 'wget -q -T 10 -O /dev/null https://monitor.example.com/ping/your-check-uuid || true'
volumes:
- data:/backup/my-app-backup:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
The prune step only runs after the backup has been copied to all storages, so the success ping is not sent when an earlier step fails. The failure notification posts the error and the log output of the run to the /fail URL. || true makes sure an unreachable monitoring service does not fail the backup itself. If you use EXEC_LABEL, add the matching docker-volume-backup.exec-label label to the backup service as well.
This setup was tested with Watchgoose monitoring platform.
Customize notifications
The title and body of the notifications can be tailored to your needs using Go templates. Template sources must be mounted inside the container in /etc/dockervolumebackup/notifications.d/: any file inside this directory will be parsed.
services:
backup:
image: offen/docker-volume-backup:v2
volumes:
- ./customized.template:/etc/dockervolumebackup/notifications.d/01.template
The files have to define nested templates in order to override the original values. An example:
{{ define "title_success" -}}
✅ Successfully ran backup {{ .Config.BackupStopDuringBackupLabel }}
{{- end }}
{{ define "body_success" -}}
▶️ Start time: {{ .Stats.StartTime | formatTime }}
⏹️ End time: {{ .Stats.EndTime | formatTime }}
⌛ Took time: {{ .Stats.TookTime }}
🛑 Stopped containers: {{ .Stats.Containers.Stopped }}/{{ .Stats.Containers.All }} ({{ .Stats.Containers.StopErrors }} errors)
⚖️ Backup size: {{ .Stats.BackupFile.Size | formatBytesBin }} / {{ .Stats.BackupFile.Size | formatBytesDec }}
🗑️ Pruned backups: {{ .Stats.Storages.Local.Pruned }}/{{ .Stats.Storages.Local.Total }} ({{ .Stats.Storages.Local.PruneErrors }} errors)
{{- end }}
Template names that can be overridden are:
title_success(the title used for a successful execution)body_success(the body used for a successful execution)title_failure(the title used for a failed execution)body_failure(the body used for a failed execution)
Notification templates reference
Configuration, data about the backup run and helper functions will be passed to these templates, this page documents them fully.
Data
Here is a list of all data passed to the template:
Config: this object holds the configuration that has been passed to the script. The field names are the name of the recognized environment variables converted in PascalCase. (e.g.BACKUP_STOP_DURING_BACKUP_LABELbecomesBackupStopDuringBackupLabel)Error: the error that made the backup fail. Only available in thetitle_failureandbody_failuretemplatesStats: objects that holds stats regarding script execution. In case of an unsuccessful run, some information may not be available.StartTime: time when the script started executionEndTime: time when the backup has completed successfully (after pruning)TookTime: amount of time it took for the backup to run. (equal toEndTime - StartTime)LockedTime: amount of time it took for the backup to acquire the exclusive lockLogOutput: full log of the applicationContainers: object containing stats about the docker containersAll: total number of containersToStop: number of containers matched by the stop ruleStopped: number of containers successfully stoppedStopErrors: number of containers that were unable to be stopped (equal toToStop - Stopped)
Services: object containing stats about the docker services (only populated when Docker is running in Swarm mode)All: total number of servicesToScaleDown: number of containers matched by the scale down ruleScaledDown: number of containers successfully scaled downScaleDownErrors: number of containers that were unable to be stopped (equal toToScaleDown - ScaledDown)
BackupFile: object containing information about the backup fileName: name of the backup file (e.g.backup-2022-02-11T01-00-00.tar.gz)FullPath: full path of the backup file (e.g./archive/backup-2022-02-11T01-00-00.tar.gz)Size: size in bytes of the backup file
Storages: object that holds stats about each storageLocal,S3,WebDAV,Azure,DropboxorSSH:Total: total number of backup filesPruned: number of backup files that were deleted due to pruning rulePruneErrors: number of backup files that were unable to be pruned
Functions
Some formatting and helper functions are also available:
formatTime: formats a time object using RFC3339 format (e.g.2022-02-11T01:00:00Z)formatBytesBin: formats an amount of bytes using powers of 1024 (e.g.7055258bytes will be6.7 MiB)formatBytesDec: formats an amount of bytes using powers of 1000 (e.g.7055258bytes will be7.1 MB)env: returns the value of the environment variable of the given key if settoJson: converting object to JSONtoPrettyJson: converting object to pretty JSON
Special characters in notification URLs
The value given to NOTIFICATION_URLS is a comma separated list of URLs. If such a URL contains special characters (e.g. commas) these need to be URL encoded. To obtain an encoded version of your URL, you can use the CLI tool provided by shoutrrr (which is the library used for sending notifications):
docker run --rm -ti ghcr.io/nicholas-fedor/shoutrrr generate [service]
where service is any of the supported services, e.g. for SMTP:
docker run --rm -ti ghcr.io/nicholas-fedor/shoutrrr generate smtp