#!/bin/bash
# Daily backup of viral_social.db
# - Keeps last 30 days of local backups
# - Pushes latest backup to GitHub automatically

BACKUP_DIR="/home/glauko/viral-social/backups"
PROJECT_DIR="/home/glauko/viral-social"
DB_FILE="/home/glauko/viral-social/data/viral_social.db"
DATE=$(date +%Y-%m-%d_%H%M)
LATEST="$BACKUP_DIR/viral_social_latest.db.gz"

# Create backup with date
cp "$DB_FILE" "$BACKUP_DIR/viral_social_${DATE}.db"

# Compress the backup
gzip "$BACKUP_DIR/viral_social_${DATE}.db"

# Create/overwrite latest backup (this one goes to GitHub)
cp "$BACKUP_DIR/viral_social_${DATE}.db.gz" "$LATEST"

# Delete local backups older than 30 days (keep latest)
find "$BACKUP_DIR" -name "viral_social_2*.db.gz" -mtime +30 -delete

# Push to GitHub
cd "$PROJECT_DIR"
git add backups/viral_social_latest.db.gz
git add -A
git commit -m "backup: database $(date +%Y-%m-%d)" --allow-empty 2>/dev/null
git push origin main 2>/dev/null

echo "[$(date)] Backup completed: viral_social_${DATE}.db.gz + pushed to GitHub"
