#!/bin/bash
# filepath: /home/acedanger/docker/gitea/backup-gitea.sh

# Gitea Backup Script
# This script backs up Gitea data and PostgreSQL database

set -e  # Exit on any error

# Configuration
BACKUP_DIR="/home/acedanger/backups/gitea"
DATE=$(date +"%Y%m%d_%H%M%S")
COMPOSE_FILE="/home/acedanger/docker/gitea/docker-compose.yml"
COMPOSE_DIR="/home/acedanger/docker/gitea"

# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

echo "Starting Gitea backup at $(date)"

# Change to compose directory
cd "$COMPOSE_DIR"

# Create timestamped backup directory
BACKUP_PATH="$BACKUP_DIR/gitea_backup_$DATE"
mkdir -p "$BACKUP_PATH"

# Backup PostgreSQL database
echo "Backing up PostgreSQL database..."
docker-compose exec -T db pg_dump -U ${POSTGRES_USER:-gitea} ${POSTGRES_DB:-gitea} > "$BACKUP_PATH/database.sql"

# Backup Gitea data volume
echo "Backing up Gitea data volume..."
docker run --rm \
    -v gitea_gitea:/data:ro \
    -v "$BACKUP_PATH":/backup \
    alpine:latest \
    tar czf /backup/gitea_data.tar.gz -C /data .

# Backup PostgreSQL data volume (optional, as we have the SQL dump)
echo "Backing up PostgreSQL data volume..."
docker run --rm \
    -v gitea_postgres:/data:ro \
    -v "$BACKUP_PATH":/backup \
    alpine:latest \
    tar czf /backup/postgres_data.tar.gz -C /data .

# Copy docker-compose configuration
echo "Backing up configuration files..."
cp "$COMPOSE_FILE" "$BACKUP_PATH/"
if [ -f ".env" ]; then
    cp ".env" "$BACKUP_PATH/"
fi

# Create a restore script
cat > "$BACKUP_PATH/restore.sh" << 'EOF'
#!/bin/bash
# Restore script for Gitea backup

set -e

RESTORE_DIR="$(dirname "$0")"
COMPOSE_DIR="/home/acedanger/docker/gitea"

echo "WARNING: This will stop Gitea and replace all data!"
read -p "Are you sure you want to continue? (yes/no): " confirm

if [ "$confirm" != "yes" ]; then
    echo "Restore cancelled"
    exit 1
fi

cd "$COMPOSE_DIR"

# Stop services
echo "Stopping Gitea services..."
docker-compose down

# Remove existing volumes
echo "Removing existing volumes..."
docker volume rm gitea_gitea gitea_postgres || true

# Recreate volumes
echo "Creating volumes..."
docker volume create gitea_gitea
docker volume create gitea_postgres

# Restore Gitea data
echo "Restoring Gitea data..."
docker run --rm \
    -v gitea_gitea:/data \
    -v "$RESTORE_DIR":/backup:ro \
    alpine:latest \
    tar xzf /backup/gitea_data.tar.gz -C /data

# Start database for restore
echo "Starting database for restore..."
docker-compose up -d db

# Wait for database to be ready
echo "Waiting for database to be ready..."
sleep 10

# Restore database
echo "Restoring database..."
docker-compose exec -T db psql -U ${POSTGRES_USER:-gitea} -d ${POSTGRES_DB:-gitea} < "$RESTORE_DIR/database.sql"

# Start all services
echo "Starting all services..."
docker-compose up -d

echo "Restore completed!"
EOF

chmod +x "$BACKUP_PATH/restore.sh"

# Create info file
cat > "$BACKUP_PATH/backup_info.txt" << EOF
Gitea Backup Information
========================
Backup Date: $(date)
Backup Location: $BACKUP_PATH
Gitea Version: $(docker-compose exec -T server gitea --version | head -1)
PostgreSQL Version: $(docker-compose exec -T db postgres --version)

Files included:
- database.sql: PostgreSQL database dump
- gitea_data.tar.gz: Gitea data volume
- postgres_data.tar.gz: PostgreSQL data volume
- docker-compose.yml: Docker compose configuration
- .env: Environment variables (if exists)
- restore.sh: Restore script

To restore this backup, run:
cd $BACKUP_PATH
./restore.sh
EOF

# Cleanup old backups (keep last 7 days)
echo "Cleaning up old backups..."
find "$BACKUP_DIR" -type d -name "gitea_backup_*" -mtime +7 -exec rm -rf {} + 2>/dev/null || true

echo "Backup completed successfully!"
echo "Backup saved to: $BACKUP_PATH"
echo "Backup size: $(du -sh "$BACKUP_PATH" | cut -f1)"