2025-05-04 13:18:07 +02:00
# docker-volume-s3-backup
## Introduction
2025-05-04 13:38:00 +02:00
This project provides a Docker image that periodically backs up a Docker volume to AWS S3 as a gzipped tape archive, and can restore a backup as needed.
2025-05-04 13:18:07 +02:00
## Usage
### Backup
Example `docker-compose.yml` :
2019-11-27 20:57:21 -07:00
```yaml
2022-07-03 13:47:02 -04:00
services:
2025-05-04 13:18:07 +02:00
my_service:
image: ...
volumes:
data:/app/some-data-dir
2019-11-27 20:57:21 -07:00
2025-05-04 13:18:07 +02:00
volume_backup:
2025-05-04 14:00:29 +02:00
image: garraflavatra/docker-volume-s3-backup
2025-05-04 13:54:42 +02:00
volumes:
data:/data/folder1
2022-07-03 13:47:02 -04:00
environment:
2025-05-04 13:54:42 +02:00
BACKUP_NAME: myvolume # required
SCHEDULE: '@weekly ' # optional
BACKUP_KEEP_DAYS: 7 # optional
PASSPHRASE: passphrase # optional
S3_REGION: region # or set S3_ENDPOINT if you do not use AWS
S3_ACCESS_KEY_ID: key # alias S3_ACCESS_KEY
S3_SECRET_ACCESS_KEY: secret # alias S3_SECRET_KEY
S3_BUCKET: my-bucket # required
S3_PREFIX: backup # required
2019-11-27 20:57:21 -07:00
```
2022-04-12 21:39:51 +03:00
2025-05-04 13:54:42 +02:00
- You can mount as many folders into `/data` as you like. Everything in `/data` is copied to your S3 storage in a single `.tar.gz` .
2021-12-07 22:15:47 -05:00
- The `SCHEDULE` variable determines backup frequency. See go-cron schedules documentation [here ](http://godoc.org/github.com/robfig/cron#hdr-Predefined_schedules ). Omit to run the backup immediately and then exit.
2019-12-01 18:47:18 -07:00
- If `PASSPHRASE` is provided, the backup will be encrypted using GPG.
2025-05-04 13:18:07 +02:00
- Run `docker exec <container_name> sh backup.sh` to trigger a backup ad-hoc.
2023-01-29 12:23:42 -05:00
- If `BACKUP_KEEP_DAYS` is set, backups older than this many days will be deleted from S3.
- Set `S3_ENDPOINT` if you're using a non-AWS S3-compatible storage provider.
2019-11-27 20:57:21 -07:00
2025-05-04 13:18:07 +02:00
### Restore
2024-01-27 11:33:49 -05:00
> [!CAUTION]
2025-05-04 18:03:24 +02:00
> All existing files will be replaced by the backup content!
2024-01-27 11:33:49 -05:00
2025-05-04 18:03:24 +02:00
#### From latest backup
2025-05-04 13:18:07 +02:00
2019-11-27 20:57:21 -07:00
```sh
2025-05-04 13:18:07 +02:00
docker exec < container_name > sh restore.sh
2019-11-27 20:57:21 -07:00
```
2024-01-27 11:33:49 -05:00
> [!NOTE]
2025-05-04 18:03:24 +02:00
> If your bucket has over 1000 files, the latest may not be restored – the S3 `ls` command is issued only once.
2024-01-27 11:33:49 -05:00
2025-05-04 18:03:24 +02:00
#### From specific backup
2019-12-01 21:49:48 -07:00
2022-07-03 12:23:22 -04:00
```sh
2025-05-04 13:18:07 +02:00
docker exec < container_name > sh restore.sh < timestamp >
2022-07-03 12:23:22 -04:00
```
2025-05-04 13:18:07 +02:00
## Development
2025-05-04 18:03:24 +02:00
You can use the Docker Compose file in the root with your own S3 bucket for development and testing.
2022-07-03 13:43:54 -04:00
```sh
2025-05-04 14:00:29 +02:00
cp .env.example .env
2022-07-03 13:43:54 -04:00
# fill out your secrets/params in .env
docker compose up -d
```
2022-07-03 12:23:22 -04:00
2025-05-04 13:18:07 +02:00
## Acknowledgements
This project is a modification of the excellent [`eeshugerman/postgres-backup-s3` ](https://github.com/eeshugerman/postgres-backup-s3 ), which in turn is a fork and re-structuring of [`schickling/postgres-backup-s3` ](https://github.com/schickling/dockerfiles/tree/master/postgres-backup-s3 ) and [`schickling/postgres-restore-s3` ](https://github.com/schickling/dockerfiles/tree/master/postgres-restore-s3 ).
## Copyright
Copyright (c) 2017 Johannes Schickling
Copyright (c) 2022 Elliott Shugerman
Copyright (c) 2025 [Romein van Buren ](mailto:romein@smartyellow.nl )
Licensed under the MIT license.