How to Automatically Backup Docker Volumes with a Python Script and Cronjob on Linux
Table of Content
Docker volumes are essential for persisting data across container restarts. But what happens if you lose that data? Regular backups are crucial, and while Docker doesn’t offer built-in volume backup functionality, you can easily automate it with a simple Python script.
In this tutorial, I’ll show you how to create a Python script to automatically back up your Docker volumes. You’ll learn how to install the required dependencies, create the script, and run it to ensure your data is always safe.
Let’s get started!
Step 1: Install Dependencies
To interact with Docker using Python, we’ll use the docker library (also known as docker-py
). You can install it using pip.
Run the following command in your terminal:
pip install docker
Make sure you have Docker installed and running on your system. You’ll also need Python 3 installed.
Step 2: Create the Backup Directory
We need a directory to store our backups. Let’s create a directory called docker_volume_backups
.
mkdir -p ./docker_volume_backups
This is where the script will save the backup files.
Step 3: Write the Python Script
Now, let’s write the script that will back up all your Docker volumes.
Create a new file called docker_volume_backup.py
and add the following code:
import docker
import os
import tarfile
import datetime
# Directory to save backups
BACKUP_DIR = "./docker_volume_backups"
# Create backup directory if it doesn't exist
os.makedirs(BACKUP_DIR, exist_ok=True)
# Initialize Docker client
client = docker.from_env()
def backup_volume(volume_name):
"""Backup a single Docker volume."""
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
backup_filename = f"{volume_name}_{timestamp}.tar.gz"
backup_path = os.path.join(BACKUP_DIR, backup_filename)
# Create a temporary container to access the volume
container = client.containers.run(
"alpine",
command="sleep 5",
volumes={volume_name: {"bind": "/backup_volume", "mode": "ro"}},
detach=True
)
try:
# Copy the volume's content to a tar file
tar_stream, _ = container.get_archive("/backup_volume")
# Save the tar stream to a file
with open(backup_path, "wb") as f:
for chunk in tar_stream:
f.write(chunk)
print(f"Backup completed for volume '{volume_name}' -> {backup_path}")
except Exception as e:
print(f"Error backing up volume '{volume_name}': {e}")
finally:
# Remove the temporary container
container.remove(force=True)
def backup_all_volumes():
"""Backup all Docker volumes."""
volumes = client.volumes.list()
if not volumes:
print("No Docker volumes found.")
return
for volume in volumes:
backup_volume(volume.name)
if __name__ == "__main__":
print("Starting Docker volume backup...")
backup_all_volumes()
print("Backup process completed.")
Step 4: How the Script Works
Here’s a quick breakdown of what the script does:
- Sets the Backup Directory: All backups are saved in
./docker_volume_backups
. You can modify this to any path you prefer. - Initializes the Docker Client: Uses the
docker
library to interact with Docker. - Creates a Temporary Container: The script creates a temporary Alpine Linux container, mounts each volume as read-only, and copies its contents.
- Saves the Backup: Each volume is backed up as a timestamped
.tar.gz
file. - Cleans Up: The temporary container is removed after the backup is completed.
Step 5: Run the Script
Make the script executable (optional):
chmod +x docker_volume_backup.py
Run the script with:
python docker_volume_backup.py
You should see output similar to this:
Starting Docker volume backup...
Backup completed for volume 'my_docker_volume' -> ./docker_volume_backups/my_docker_volume_20240427_123456.tar.gz
Backup process completed.
Step 6: Automate the Backup (Optional)
To automate backups, you can set up a cron job (Linux) or a Task Scheduler task (Windows).
Example Cron Job
Open your crontab:
crontab -e
Add the following line to run the script daily at 2 AM:
0 2 * * * /usr/bin/python3 /path/to/docker_volume_backup.py
Example Task Scheduler Command (Windows)
In Windows Task Scheduler, create a new task with the following command:
python C:\path\to\docker_volume_backup.py
Set the schedule to your desired frequency.
Final Thoughts
Backing up Docker volumes doesn’t have to be a manual chore. With this simple Python script, you can automate the process and ensure your data is always protected. Whether you’re running critical services or just want peace of mind, regular backups are a must.
Try it out and let us know how it works for you!
Happy Docker-ing! 🛠️⚡️