| config_example | ||
| docker_backup.sh | ||
| LICENSE | ||
| README.md | ||
Docker Volume Backup Script
Overview
This script automates the process of backing up Docker volumes (named volumes, bind mounts, and anonymous volumes) for containers managed by a docker-compose.yml file. It supports error handling, dependency checks, and notification via ntfy for failures or significant events.
Features
- Supports All Volume Types:
- Named Volumes
- Bind Mounts
- Anonymous Volumes
- Error Handling:
- Sends notifications on errors via
ntfy.
- Sends notifications on errors via
- Dependency Check:
- Ensures required tools (
docker,jq,tar,curl) are installed.
- Ensures required tools (
- Automatic Backup Directory Creation:
- Stores backups in timestamped directories for organization.
- Container-Level Processing:
- Automatically identifies and processes volumes attached to containers.
Requirements
- Dependencies:
dockerjqtarcurl
- ntfy Configuration:
- Replace
NTFY_TOPICandNTFY_SERVERin the script with your ntfy topic and server.
- Replace
Installation
- Save the script to a file, e.g.,
backup_docker_volumes.sh. - Make it executable:
chmod +x backup_docker_volumes.sh - Install dependencies if not already installed:
sudo apt-get install docker jq tar curl
Usage
Run the script with the path to a docker-compose.yml file:
./backup_docker_volumes.sh /path/to/docker-compose.yml
Example Output
-
Backup Directory: Backups are stored in a directory named
backup_<DATE>_<TIME>, created in the current working directory. -
Notifications:
- Successful completions send a "Backup Complete" notification.
- Errors send "Backup Error" notifications.
Script Details
Functions
-
Dependency Check
check_command: Verifies if a command is available on the system.check_dependency: Checks all dependencies before execution.
-
Notification System
send_notification: Sends notifications via ntfy for errors or events.
-
Backup Operations
backup_named_volume: Backs up Docker named volumes.backup_bind_mount: Backs up bind-mounted directories.backup_anonymous_volume: Backs up anonymous volumes.
-
Container Volume Processing
backup_volumes_for_service: Processes all volumes for a specific container.backup_docker_compose_volumes: Processes all containers defined in thedocker-compose.yml.
-
Utility
create_backup_dir: Creates a timestamped backup directory.
Notifications
ntfy Integration
This script uses ntfy to send notifications. Update the following variables in the script to configure ntfy:
NTFY_TOPIC: The topic for the ntfy server (e.g., "backup-alerts").NTFY_SERVER: The ntfy server URL (default:https://ntfy.sh).
Example Notifications
- Backup Success:
- Title:
Backup Complete - Message:
Backup successfully completed for all services in <docker-compose.yml>.
- Title:
- Backup Error:
- Title:
Backup Error - Message: Details of the failed backup.
- Title:
Known Limitations
- Docker Volumes Only:
- The script does not handle other container data (e.g., logs or ephemeral filesystems).
- ntfy Dependency:
- Requires an active ntfy server and internet access for notifications.
- jq Parsing:
- Assumes consistent Docker JSON structure; unexpected changes might cause parsing issues.
Troubleshooting
Common Errors
-
Missing Dependencies:
- Error:
"Error: <dependency> is not installed." - Solution: Install the missing dependency using your package manager.
- Error:
-
ntfy Notifications Fail:
- Error:
"Failed to send notification via ntfy." - Solution: Verify
curlinstallation and ntfy server configuration.
- Error:
-
Permission Issues:
- Error:
"Permission denied" - Solution: Ensure the script has executable permissions and proper access to Docker volumes.
- Error:
License
This project is licensed under the AGPL-3.0 License. See the LICENSE file for details.