Compare commits

...

2 commits

Author SHA1 Message Date
Dejan R. 7ae24985eb added readme.md file 2025-11-29 18:40:55 +00:00
Dejan R. d71935def7 added script 2025-11-29 18:37:43 +00:00
2 changed files with 476 additions and 0 deletions

117
backup.sh Normal file → Executable file
View file

@ -0,0 +1,117 @@
#!/bin/bash
# ZTNet Docker Compose Backup Script
# This script stops containers, backs up volumes and configs, then restarts
set -e # Exit on error
# Configuration
BACKUP_DIR="./backup"
COMPOSE_FILE="docker-compose.yml"
MAX_BACKUPS=5
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_NAME="ztnet_backup_${TIMESTAMP}"
BACKUP_PATH="${BACKUP_DIR}/${BACKUP_NAME}"
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Function to print colored messages
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if docker-compose file exists
if [ ! -f "$COMPOSE_FILE" ]; then
log_error "docker-compose.yml not found in current directory!"
exit 1
fi
# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
log_info "Starting backup process..."
log_info "Backup will be saved to: ${BACKUP_PATH}"
# Step 1: Stop containers
log_info "Stopping Docker containers..."
docker compose down
# Step 2: Create backup directory for this backup
mkdir -p "$BACKUP_PATH"
# Step 3: Backup PostgreSQL data volume
log_info "Backing up PostgreSQL data..."
docker run --rm \
-v ztnet_postgres-data:/data \
-v "$(pwd)/${BACKUP_PATH}:/backup" \
alpine tar czf /backup/postgres-data.tar.gz -C /data .
# Step 4: Backup ZeroTier volume
log_info "Backing up ZeroTier data..."
docker run --rm \
-v ztnet_zerotier:/data \
-v "$(pwd)/${BACKUP_PATH}:/backup" \
alpine tar czf /backup/zerotier.tar.gz -C /data .
# Step 5: Backup docker-compose.yml
log_info "Backing up docker-compose.yml..."
cp "$COMPOSE_FILE" "${BACKUP_PATH}/docker-compose.yml"
# Step 6: Create backup metadata
cat > "${BACKUP_PATH}/backup_info.txt" << EOF
Backup Information
==================
Date: $(date)
Timestamp: ${TIMESTAMP}
Hostname: $(hostname)
Docker Version: $(docker --version)
Docker Compose Version: $(docker compose version)
Backed up volumes:
- postgres-data
- zerotier
Backed up files:
- docker-compose.yml
EOF
log_info "Backup completed successfully!"
# Step 7: Cleanup old backups (keep only last 5)
log_info "Cleaning up old backups (keeping last ${MAX_BACKUPS})..."
cd "$BACKUP_DIR"
ls -t | grep "ztnet_backup_" | tail -n +$((MAX_BACKUPS + 1)) | xargs -r rm -rf
cd - > /dev/null
REMAINING_BACKUPS=$(ls -1 "$BACKUP_DIR" | grep "ztnet_backup_" | wc -l)
log_info "Current number of backups: ${REMAINING_BACKUPS}"
# Step 8: Restart containers
log_info "Starting Docker containers..."
docker compose up -d
# Step 9: Wait for services to be ready
log_info "Waiting for services to start..."
sleep 5
# Check if containers are running
log_info "Checking container status..."
docker compose ps
log_info "=========================================="
log_info "Backup process completed successfully!"
log_info "Backup location: ${BACKUP_PATH}"
log_info "Backup size: $(du -sh "${BACKUP_PATH}" | cut -f1)"
log_info "=========================================="

359
readme.md Normal file
View file

@ -0,0 +1,359 @@
# 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
1. Download the `backup.sh` script to your ZTNet directory (where `docker-compose.yml` is located)
2. Make the script executable:
```bash
chmod +x backup.sh
```
## Creating a Backup
### Manual Backup
Run the backup script manually:
```bash
./backup.sh
```
The script will:
1. Stop all Docker containers
2. Create compressed backups of volumes
3. Save the docker-compose.yml file
4. Generate backup metadata
5. Create a single tar.gz archive with timestamp
6. Clean up old backups (keeps last 5)
7. 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 database
- `zerotier.tar.gz` - ZeroTier configuration
- `docker-compose.yml` - Docker Compose configuration
- `backup_info.txt` - Backup metadata
### Automated Backups with Cron
To schedule automatic backups, add a cron job:
```bash
# 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
```bash
docker compose down
```
### Step 2: Extract Backup Archive
```bash
# 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:
```bash
tar -xzf backup/ztnet_backup_20241129_143022.tar.gz -C restore_temp
```
### Step 3: Restore PostgreSQL Volume
```bash
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
```bash
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)
```bash
# 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
```bash
rm -rf restore_temp
```
### Step 7: Start Containers
```bash
docker compose up -d
```
### Step 8: Verify Restoration
```bash
# Check container status
docker compose ps
# Check logs
docker compose logs -f
```
## Complete Restore Script
For convenience, here's a complete restore script:
```bash
#!/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:
```bash
./restore.sh backup/ztnet_backup_20241129_143022.tar.gz
```
## Backup Management
### View Backup Information
Extract and view the backup info without restoring:
```bash
tar -xzf backup/ztnet_backup_TIMESTAMP.tar.gz backup_info.txt -O
```
### List All Backups
```bash
ls -lh backup/
```
### Check Backup Size
```bash
du -sh backup/*
```
### Manual Cleanup
The script automatically keeps the last 5 backups. To manually delete old backups:
```bash
# 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
```bash
# 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
```bash
# 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
1. Check Docker is running:
```bash
docker ps
```
2. Check disk space:
```bash
df -h
```
3. Check script permissions:
```bash
ls -l backup.sh
```
### Restore Fails
1. Ensure containers are stopped:
```bash
docker compose down
docker compose ps
```
2. Check volume names match:
```bash
docker volume ls | grep ztnet
```
3. Verify backup file integrity:
```bash
tar -tzf backup/ztnet_backup_TIMESTAMP.tar.gz
```
### Services Don't Start After Restore
1. Check logs:
```bash
docker compose logs
```
2. Check volume permissions:
```bash
docker volume inspect ztnet_postgres-data
docker volume inspect ztnet_zerotier
```
3. Try removing and recreating volumes:
```bash
docker compose down -v
# Then restore again
```
## Best Practices
1. **Test Restores Regularly** - Verify backups work before you need them
2. **Store Backups Off-Site** - Keep copies on different servers or cloud storage
3. **Monitor Backup Size** - Ensure sufficient disk space
4. **Document Restore Process** - Keep this README accessible
5. **Automate Backups** - Use cron for regular scheduled backups
6. **Keep Multiple Versions** - The script keeps 5 backups by default
7. **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.