#!/bin/sh
set -e

echo "🔄 PostgreSQL Backup Service Starting..."
echo "📅 Backup Schedule: ${BACKUP_SCHEDULE}"
echo "🗑️  Retention: ${BACKUP_RETENTION_DAYS} days"

# Install cronie for scheduled backups
apk add --no-cache dcron

# Create cron job
echo "${BACKUP_SCHEDULE} /scripts/backup.sh >> /var/log/backup.log 2>&1" > /etc/crontabs/root

# Ensure backup directory exists
mkdir -p /backups

# Run initial backup
echo "🚀 Running initial backup..."
/scripts/backup.sh

# Start cron in foreground
echo "✅ Backup service ready - cron daemon starting"
crond -f -l 2
