Encrypting Docker volume backups before they leave the server
A tarball of a Docker volume uploaded to object storage is readable by anyone who gets the bucket credentials. Encrypting on the server before upload means a leaked bucket exposes nothing useful.
restic does most of the work
restic encrypts, deduplicates and uploads in one step, and supports S3-compatible storage:
export RESTIC_REPOSITORY=s3:https://s3.example.com/backups/app
export RESTIC_PASSWORD_FILE=/root/.restic-pass
export AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=...
restic init
Back up a volume
Mount the volume read-only into a restic container, so the backup cannot modify data:
docker run --rm -v app_data:/data:ro \
-e RESTIC_REPOSITORY -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY \
-v /root/.restic-pass:/pass:ro -e RESTIC_PASSWORD_FILE=/pass \
restic/restic backup /data --tag app_data
For databases, back up a dump file rather than the live data directory.
Retention
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
Deduplication keeps this cheap: unchanged files are stored once.
The key is the backup
Without the repository password, the backups are useless, including to you. Store it in a password manager that is not hosted on the same server, and give a second person access. Losing the server and the key together is the worst case.
Check integrity
restic check verifies the repository. Once a month, run restic restore latest --target /tmp/restore-test and look at the files.
Separate credentials
The bucket key on the server should not be able to delete old snapshots if your provider supports that restriction. Run forget --prune from a different machine with a stronger key.
A broader guide to back up Docker volumes is on the Peon blog. I work on Peon, an open-source self-hosted deployment platform.