7.2 KiB
ZTNet Docker Backup & Restore Guide
This guide explains how to use the backup script for your ZTNet Docker Compose setup.
Overview
The backup script creates compressed archives of your ZTNet installation, including:
- PostgreSQL database volume
- ZeroTier configuration and data
- Docker Compose configuration file
- Backup metadata and system information
Prerequisites
- Docker and Docker Compose installed
- Bash shell
- Sufficient disk space for backups
- Running ZTNet Docker Compose setup
Installation
-
Download the
backup.shscript to your ZTNet directory (wheredocker-compose.ymlis located) -
Make the script executable:
chmod +x backup.sh
Creating a Backup
Manual Backup
Run the backup script manually:
./backup.sh
The script will:
- Stop all Docker containers
- Create compressed backups of volumes
- Save the docker-compose.yml file
- Generate backup metadata
- Create a single tar.gz archive with timestamp
- Clean up old backups (keeps last 5)
- Restart all containers
Backup Output
Backups are stored in the ./backup directory with timestamps:
./backup/
├── ztnet_backup_20241129_143022.tar.gz
├── ztnet_backup_20241129_120000.tar.gz
└── ztnet_backup_20241128_090000.tar.gz
Each backup file contains:
postgres-data.tar.gz- PostgreSQL databasezerotier.tar.gz- ZeroTier configurationdocker-compose.yml- Docker Compose configurationbackup_info.txt- Backup metadata
Automated Backups with Cron
To schedule automatic backups, add a cron job:
# Edit crontab
crontab -e
# Add one of these lines:
# Daily backup at 2 AM
0 2 * * * cd /path/to/ztnet && ./backup.sh >> /var/log/ztnet-backup.log 2>&1
# Weekly backup on Sunday at 3 AM
0 3 * * 0 cd /path/to/ztnet && ./backup.sh >> /var/log/ztnet-backup.log 2>&1
# Every 6 hours
0 */6 * * * cd /path/to/ztnet && ./backup.sh >> /var/log/ztnet-backup.log 2>&1
Important: Replace /path/to/ztnet with the actual path to your ZTNet directory.
Restoring from Backup
Step 1: Stop Running Containers
docker compose down
Step 2: Extract Backup Archive
# Create temporary directory
mkdir -p restore_temp
# Extract the backup (replace TIMESTAMP with your backup date)
tar -xzf backup/ztnet_backup_TIMESTAMP.tar.gz -C restore_temp
Example:
tar -xzf backup/ztnet_backup_20241129_143022.tar.gz -C restore_temp
Step 3: Restore PostgreSQL Volume
docker run --rm \
-v ztnet_postgres-data:/data \
-v $(pwd)/restore_temp:/backup \
alpine tar xzf /backup/postgres-data.tar.gz -C /data
Step 4: Restore ZeroTier Volume
docker run --rm \
-v ztnet_zerotier:/data \
-v $(pwd)/restore_temp:/backup \
alpine tar xzf /backup/zerotier.tar.gz -C /data
Step 5: Restore Docker Compose Configuration (Optional)
# Backup current configuration first
cp docker-compose.yml docker-compose.yml.backup
# Restore from backup
cp restore_temp/docker-compose.yml .
Step 6: Clean Up Temporary Files
rm -rf restore_temp
Step 7: Start Containers
docker compose up -d
Step 8: Verify Restoration
# Check container status
docker compose ps
# Check logs
docker compose logs -f
Complete Restore Script
For convenience, here's a complete restore script:
#!/bin/bash
# Check if backup file is provided
if [ -z "$1" ]; then
echo "Usage: ./restore.sh backup/ztnet_backup_TIMESTAMP.tar.gz"
exit 1
fi
BACKUP_FILE="$1"
if [ ! -f "$BACKUP_FILE" ]; then
echo "Error: Backup file not found: $BACKUP_FILE"
exit 1
fi
echo "Stopping containers..."
docker compose down
echo "Extracting backup..."
mkdir -p restore_temp
tar -xzf "$BACKUP_FILE" -C restore_temp
echo "Restoring PostgreSQL volume..."
docker run --rm \
-v ztnet_postgres-data:/data \
-v $(pwd)/restore_temp:/backup \
alpine tar xzf /backup/postgres-data.tar.gz -C /data
echo "Restoring ZeroTier volume..."
docker run --rm \
-v ztnet_zerotier:/data \
-v $(pwd)/restore_temp:/backup \
alpine tar xzf /backup/zerotier.tar.gz -C /data
echo "Cleaning up..."
rm -rf restore_temp
echo "Starting containers..."
docker compose up -d
echo "Waiting for services..."
sleep 5
echo "Checking status..."
docker compose ps
echo "Restore completed!"
Save as restore.sh, make executable with chmod +x restore.sh, and use:
./restore.sh backup/ztnet_backup_20241129_143022.tar.gz
Backup Management
View Backup Information
Extract and view the backup info without restoring:
tar -xzf backup/ztnet_backup_TIMESTAMP.tar.gz backup_info.txt -O
List All Backups
ls -lh backup/
Check Backup Size
du -sh backup/*
Manual Cleanup
The script automatically keeps the last 5 backups. To manually delete old backups:
# Delete specific backup
rm backup/ztnet_backup_20241128_090000.tar.gz
# Delete all backups older than 30 days
find backup/ -name "ztnet_backup_*.tar.gz" -mtime +30 -delete
Transferring Backups
Copy to Remote Server
# Using SCP
scp backup/ztnet_backup_TIMESTAMP.tar.gz user@remote:/path/to/backups/
# Using rsync
rsync -avz backup/ user@remote:/path/to/backups/
Upload to Cloud Storage
# AWS S3
aws s3 cp backup/ztnet_backup_TIMESTAMP.tar.gz s3://your-bucket/ztnet-backups/
# Google Cloud Storage
gsutil cp backup/ztnet_backup_TIMESTAMP.tar.gz gs://your-bucket/ztnet-backups/
Troubleshooting
Backup Script Fails
-
Check Docker is running:
docker ps -
Check disk space:
df -h -
Check script permissions:
ls -l backup.sh
Restore Fails
-
Ensure containers are stopped:
docker compose down docker compose ps -
Check volume names match:
docker volume ls | grep ztnet -
Verify backup file integrity:
tar -tzf backup/ztnet_backup_TIMESTAMP.tar.gz
Services Don't Start After Restore
-
Check logs:
docker compose logs -
Check volume permissions:
docker volume inspect ztnet_postgres-data docker volume inspect ztnet_zerotier -
Try removing and recreating volumes:
docker compose down -v # Then restore again
Best Practices
- Test Restores Regularly - Verify backups work before you need them
- Store Backups Off-Site - Keep copies on different servers or cloud storage
- Monitor Backup Size - Ensure sufficient disk space
- Document Restore Process - Keep this README accessible
- Automate Backups - Use cron for regular scheduled backups
- Keep Multiple Versions - The script keeps 5 backups by default
- Verify After Restore - Always check services are working properly
Security Notes
- Backup files contain sensitive data (database, configurations)
- Store backups securely with appropriate permissions
- Consider encrypting backups if storing remotely
- Restrict access to backup directory
Support
For issues or questions:
- Check Docker logs:
docker compose logs - Review backup_info.txt in backup archives
- Verify Docker and Docker Compose versions
- Check ZTNet documentation
License
This backup script is provided as-is for use with ZTNet installations.