#!/bin/bash

# Docker configurations backup script with retention policy
# Retains: 3 daily, 3 weekly, 3 monthly backups

BACKUP_DIR="/home/will/docker_backups"
SOURCE_DIR="/home/will/docker"
REPO_URL="https://git.will123song.xyz/will/docker-configs.git"
DATE=$(date +%Y%m%d_%H%M%S)
DAY_OF_WEEK=$(date +%u)  # 1-7 (Monday is 1)
DAY_OF_MONTH=$(date +%d)

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

# Function to create git backup
create_git_backup() {
    echo "Creating git backup for $DATE"
    cd "$SOURCE_DIR"
    
    # Add any new files and commit changes
    git add .
    if git diff --cached --quiet; then
        echo "No changes to backup"
    else
        git commit -m "Automated backup - $DATE"
        git push origin master 2>/dev/null || echo "Failed to push to remote (may need manual setup)"
    fi
}

# Function to clean old backups
cleanup_backups() {
    echo "Cleaning up old backups..."
    cd "$BACKUP_DIR"
    
    # Keep last 3 daily backups (delete older daily backups)
    ls -t daily_*.tar.gz 2>/dev/null | tail -n +4 | xargs rm -f 2>/dev/null
    
    # Keep last 3 weekly backups (delete older weekly backups)
    ls -t weekly_*.tar.gz 2>/dev/null | tail -n +4 | xargs rm -f 2>/dev/null
    
    # Keep last 3 monthly backups (delete older monthly backups)
    ls -t monthly_*.tar.gz 2>/dev/null | tail -n +4 | xargs rm -f 2>/dev/null
    
    echo "Cleanup completed"
}

# Create tar backup based on schedule
create_tar_backup() {
    cd "$SOURCE_DIR"
    
    # Determine backup type
    if [ "$DAY_OF_MONTH" = "01" ]; then
        # Monthly backup on 1st of month
        BACKUP_TYPE="monthly"
        BACKUP_FILE="$BACKUP_DIR/monthly_$DATE.tar.gz"
    elif [ "$DAY_OF_WEEK" = "1" ]; then
        # Weekly backup on Monday
        BACKUP_TYPE="weekly"
        BACKUP_FILE="$BACKUP_DIR/weekly_$DATE.tar.gz"
    else
        # Daily backup
        BACKUP_TYPE="daily"
        BACKUP_FILE="$BACKUP_DIR/daily_$DATE.tar.gz"
    fi
    
    echo "Creating $BACKUP_TYPE backup: $BACKUP_FILE"
    
    # Create tar backup excluding data directories
    tar -czf "$BACKUP_FILE" \
        --exclude='*/data/*' \
        --exclude='*/postgres/*' \
        --exclude='*/vw-data/*' \
        --exclude='*/db_data/*' \
        --exclude='*/caddy_data/*' \
        --exclude='*/caddy_config/*' \
        --exclude='*/config/*' \
        --exclude='HA/config/*' \
        --exclude='HA/db_data/*' \
        --exclude='.git' \
        --exclude='gitea/postgres' \
        --exclude='HA/db_data' \
        --warning=no-file-changed \
        --warning=no-file-removed \
        . 2>/dev/null || true
    
    if [ -f "$BACKUP_FILE" ]; then
        echo "$BACKUP_TYPE backup created successfully: $BACKUP_FILE"
        ls -lh "$BACKUP_FILE"
    else
        echo "Error creating $BACKUP_TYPE backup"
        exit 1
    fi
}

# Main execution
echo "Starting backup process at $(date)"

# Create git backup
create_git_backup

# Create tar backup
create_tar_backup

# Clean up old backups
cleanup_backups

echo "Backup process completed at $(date)"