Backing Up Docker Volumes and Containers is a common requirement for VPS administrators. This guide provides practical instructions that you can follow on Ubuntu 22.04/24.04 or Debian 12, though most steps apply to other distributions as well.
Prerequisites
- A remote storage destination (S3, B2, or another server)
- A VPS running Ubuntu 22.04 or later (2GB+ RAM recommended)
- Sufficient storage for backups (2-3x data size)
Backup Configuration
If you encounter issues during setup, check the system logs first. Most problems can be diagnosed by examining the output of journalctl or the application-specific log files in /var/log/.
# Install and initialize backup tool
sudo apt install -y docker
docker init --repo /backup/repo
# Create first backup
docker backup --repo /backup/repo /etc /home /var/www
# List backups
docker snapshots --repo /backup/repo
The output should show the service running without errors. If you see any warning messages, address them before proceeding to the next step.
Important Notes
The docker configuration requires careful attention to resource limits and security settings. On a VPS with limited resources, it's important to tune these parameters according to your available RAM and CPU cores.
Scheduling Automated Backups
Regular maintenance is essential for keeping your docker installation running smoothly. Schedule periodic reviews of log files, disk usage, and security updates to prevent issues before they occur.
# Automated backup script: /usr/local/bin/backup.sh
#!/bin/bash
set -euo pipefail
BACKUP_REPO="/backup/repo"
LOG_FILE="/var/log/backup.log"
DATE=$(date +%Y-%m-%d_%H-%M)
echo "[$DATE] Starting backup..." >> $LOG_FILE
# Database dump
mysqldump --all-databases | gzip > /tmp/db-$DATE.sql.gz
# Run backup
docker backup --repo $BACKUP_REPO /etc /home /var/www /tmp/db-$DATE.sql.gz
# Cleanup old backups (keep 30 days)
docker forget --repo $BACKUP_REPO --keep-daily 7 --keep-weekly 4 --keep-monthly 3 --prune
rm /tmp/db-$DATE.sql.gz
echo "[$DATE] Backup completed successfully" >> $LOG_FILE
These commands should be run as root or with sudo privileges. If you're using a non-root user, prefix each command with sudo.
Encryption and Security
After applying these changes, monitor the server's resource usage for at least 24 hours to ensure stability. Tools like htop, iostat, and vmstat can provide real-time insights into system performance.
# Install and initialize backup tool
sudo apt install -y docker
docker init --repo /backup/repo
# Create first backup
docker backup --repo /backup/repo /etc /home /var/www
# List backups
docker snapshots --repo /backup/repo
Make sure to restart the service after applying these changes. Some settings require a full restart rather than a reload to take effect.
Testing Restore Procedures
It's recommended to test this configuration in a staging environment before deploying to production. This helps identify potential compatibility issues and allows you to benchmark performance differences.
# Automated backup script: /usr/local/bin/backup.sh
#!/bin/bash
set -euo pipefail
BACKUP_REPO="/backup/repo"
LOG_FILE="/var/log/backup.log"
DATE=$(date +%Y-%m-%d_%H-%M)
echo "[$DATE] Starting backup..." >> $LOG_FILE
# Database dump
mysqldump --all-databases | gzip > /tmp/db-$DATE.sql.gz
# Run backup
docker backup --repo $BACKUP_REPO /etc /home /var/www /tmp/db-$DATE.sql.gz
# Cleanup old backups (keep 30 days)
docker forget --repo $BACKUP_REPO --keep-daily 7 --keep-weekly 4 --keep-monthly 3 --prune
rm /tmp/db-$DATE.sql.gz
echo "[$DATE] Backup completed successfully" >> $LOG_FILE
Make sure to restart the service after applying these changes. Some settings require a full restart rather than a reload to take effect.
Common Issues and Solutions
- High memory usage: Review the configuration for memory-related settings. Reduce worker counts or buffer sizes if running on a low-RAM VPS.
- Service won't start: Check the logs with
journalctl -xe -u docker. Common causes include port conflicts, missing configuration files, or insufficient permissions.
Next Steps
With docker now set up and running, consider implementing monitoring to track performance metrics over time. Regularly review your configuration as your workload changes and scale resources accordingly.