A script for backing-up persistent Docker container data.
This script is written for use with Docker on Ubuntu Server hosts. Containers should store permanent data on disk via system bind mounts. The script:
- Stops all active containers.
- Creates a backup of the data directories.
- Restarts all previously stopped containers.
- Calls to a machine-specific rclone config to create an offsite backup. I recommend an S3 provider wrapped in the crypt utility.
To protect data integrity all containers are stopped and then restarted. This results in a short outage period and so it is best to schedule this script to run during a quieter time period.
Note
When working with databases, it is generally more reliable to use a purpose-built backup tool (e.g.pgdumpfor PostgreSQL) to perform data backups.
This script expects the following file structure on disk. The /var/ctrs* directories should belong to the Docker service user (which is usually root).
The backup.sh script and a sample backup.env file are provided in this repository.
/var/ctrs/
backup.sh
backup.env
/mounts/
container_name/
bind_mount_a/
bind_mount_b/
stacks/
container_name/
.env
compose.yaml
dockge/ (only if using)
/var/ctrs-backup/
ctrs-YYMMDD-hhmm.tar.gz (illustrative)