Interactive Docker Volumes Backup & Restore Python Script
Project Overview
During my work, managing multiple Docker hosts and services, I encountered a recurring challenge: backing up and restoring Docker volumes reliably, especially in environments where data integrity is critical and volumes are mounted on NFS. Manual backups were error-prone, inconsistent, and time-consuming. To solve this, I developed a Python-based interactive backup and restore tool specifically designed to manage Docker volumes efficiently while providing operational transparency through logging and notifications.
This tool automates volume backup workflows, ensures safety with dry-run support, manages old backups, and can notify me in real time via Telegram.
The Necessity
Docker volumes are often the primary storage for databases, application data, and other persistent data inside containers. The problems I faced included:
- Data loss risk: Manual backups were skipped or mismanaged.
- Inconsistent schedules: Without automation, backups weren’t uniform across all volumes.
- Operational friction: Pausing containers during backup manually was tedious and error-prone.
- Monitoring gaps: I needed immediate feedback for successes or failures without constantly checking logs.
These challenges drove me to build a solution that was automated, reliable, and transparent, reducing human error while supporting operational best practices.
Problems Solved
My Python tool addresses these challenges head-on:
- Automates backup of Docker volumes, even when multiple containers are using them.
- Provides restore functionality to recover data from any backup point.
- Ensures that NFS-mounted backup directories are active, writable, and not stale.
- Sends real-time notifications to Telegram for backup success, failure, or any anomalies.
- Supports dry-run mode for safe testing without touching real data.
- Maintains backup history intelligently, removing older backups beyond a configurable retention count.
Features
- Interactive CLI menu for backup and restore operations, or fully automated “backup-all” mode.
- NFS-aware: Validates mounts, ensures write access, and prevents operations on stale paths.
- Container-safe operations: Automatically pauses containers using the target volume before backup, and unpauses afterward.
- Headless mode with –backup-all for cronjobs, CI pipelines, or systemd timers
- Backup cleanup: Keeps only the last N backups per volume, configurable via .env.
- Dry-run mode: Simulate backups/restores for testing or troubleshooting.
- Detailed logging: Logs every action to backup.log with timestamps, file sizes, and durations.
- Telegram integration: Sends notifications for successful or failed operations, dry-run alerts, and path validation results.
- Human-readable output: Summarizes backups and restores in tables showing volume, file, size, duration, and status.





Technical Implementation
- Built with Python 3, leveraging libraries such as subprocess, prettytable, dotenv, and requests.
- Uses Docker CLI commands to manage volumes and run backup containers in a secure, isolated manner (debian:stable-slim).
- Integrates interactive prompts with index-based selection, allowing flexible, user-friendly volume and backup selection.
- Handles filesystem type detection for NFS or local storage using df -T.
- Implements robust error handling, including exceptions for inaccessible volumes, failed container operations, and stale NFS mounts.
Outcome
After implementing this tool:
- Backups became reliable, consistent, and automated, reducing human intervention.
- Data recovery became quick and safe, even in high-volume environments.
- I gained real-time operational insight via Telegram notifications, increasing confidence during backup cycles.
- The backup workflow is now fully repeatable and auditable, supporting best practices for DevOps operations.
What I Learned
- How to integrate Docker with Python for production-ready automation tasks.
- Techniques for pausing and unpausing containers safely during backup operations.
- How to detect NFS mounts, stale mounts, and filesystem types programmatically.
- How to build robust CLI applications with interactive menus and dry-run modes.
- Managing logs and notifications for operational transparency and alerting.
Achievements
- Developed a self-sufficient backup tool that reduced the risk of data loss for critical Docker volumes.
- Learned to combine DevOps practices, Python scripting, and container management into a single cohesive project.
- Demonstrated skills in automation, system validation, and operational monitoring, which are crucial in production environments.
- Created a reusable, production-grade tool that I now leverage in multiple environments, saving significant time and effort.