This script automates the process of backing up Docker volumes.
Find a file
2024-11-16 14:12:20 +01:00
config_example init 2024-11-16 14:12:20 +01:00
docker_backup.sh init 2024-11-16 14:12:20 +01:00
LICENSE init 2024-11-16 14:12:20 +01:00
README.md init 2024-11-16 14:12:20 +01:00

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.
  • Dependency Check:
    • Ensures required tools (docker, jq, tar, curl) are installed.
  • Automatic Backup Directory Creation:
    • Stores backups in timestamped directories for organization.
  • Container-Level Processing:
    • Automatically identifies and processes volumes attached to containers.

Requirements

  • Dependencies:
    • docker
    • jq
    • tar
    • curl
  • ntfy Configuration:
    • Replace NTFY_TOPIC and NTFY_SERVER in the script with your ntfy topic and server.

Installation

  1. Save the script to a file, e.g., backup_docker_volumes.sh.
  2. Make it executable:
    chmod +x backup_docker_volumes.sh
    
  3. 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

  1. Dependency Check

    • check_command: Verifies if a command is available on the system.
    • check_dependency: Checks all dependencies before execution.
  2. Notification System

    • send_notification: Sends notifications via ntfy for errors or events.
  3. 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.
  4. Container Volume Processing

    • backup_volumes_for_service: Processes all volumes for a specific container.
    • backup_docker_compose_volumes: Processes all containers defined in the docker-compose.yml.
  5. 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>.
  • Backup Error:
    • Title: Backup Error
    • Message: Details of the failed backup.

Known Limitations

  1. Docker Volumes Only:
    • The script does not handle other container data (e.g., logs or ephemeral filesystems).
  2. ntfy Dependency:
    • Requires an active ntfy server and internet access for notifications.
  3. jq Parsing:
    • Assumes consistent Docker JSON structure; unexpected changes might cause parsing issues.

Troubleshooting

Common Errors

  1. Missing Dependencies:

    • Error: "Error: <dependency> is not installed."
    • Solution: Install the missing dependency using your package manager.
  2. ntfy Notifications Fail:

    • Error: "Failed to send notification via ntfy."
    • Solution: Verify curl installation and ntfy server configuration.
  3. Permission Issues:

    • Error: "Permission denied"
    • Solution: Ensure the script has executable permissions and proper access to Docker volumes.

License

This project is licensed under the AGPL-3.0 License. See the LICENSE file for details.