This script automates backups of Docker stacks by identifying containers labeled with SDVB=..., stopping them, archiving their named volumes, and restarting them. Itβs designed to work across systems like TrueNAS SCALE and Debian, backing up only Docker-managed volumes (not bind mounts or host paths).
- Discovers all running containers with a
SDVBlabel. - Groups containers by unique
SDVB=value (e.g.,SDVB=Netbox,SDVB=Gitea). - Stops containers in each stack to ensure volume consistency.
- Identifies named Docker volumes (ignores bind mounts and host paths).
- Creates a
.tararchive of each stackβs volumes in a specified backup directory. - Restarts containers after backup is complete.
Each backup file is named:
<StackName>Backup<YYYY-MM-DD>.tar
Example:
Netbox_Backup_2025-10-05.tar
Backups are stored in:
/mnt/Backups
You can change this path by editing the BACKUP_DIRECTORY variable in the script, oterwise you need to ensure /mnt/Backups exists.
π How to Use
-
Label your containers with SDVB=StackName in your Docker Compose or run commands. Example:
labels: - SDVB=Netbox -
Place the script on your host system (e.g., /usr/local/bin/docker_stack_backup.sh).
-
Make it executable:
chmod +x docker_stack_backup.sh -
Run the script manually:
./docker_stack_backup.sh
4.b (Optional) Automate with cron:
crontab -e
0 2 * * * /usr/local/bin/docker_stack_backup.sh >> /var/log/docker_backup.log 2>&1
5. Bonus: Use Rclone on Script Completion to copy the contents to a backup storage of your choosing