diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..6b1e9c1 --- /dev/null +++ b/readme.md @@ -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. \ No newline at end of file