#!/bin/bash # init_pg.sh — Inicia PostgreSQL + restaura backup + sobe app FastAPI com 2 workers set -e echo "🔧 [INIT] Iniciando PostgreSQL..." su - postgres -c "pg_ctl -D $PGDATA start" 2>/dev/null || pg_ctlcluster 17 main start 2>/dev/null || service postgresql start for i in $(seq 1 30); do if pg_isready -q 2>/dev/null; then echo "✅ [INIT] PostgreSQL pronto" break fi sleep 1 done su - postgres -c "psql -tc \"SELECT 1 FROM pg_roles WHERE rolname='akira'\" | grep -q 1" || \ su - postgres -c "psql -c \"CREATE USER akira WITH PASSWORD 'akira' SUPERUSER;\"" su - postgres -c "psql -tc \"SELECT 1 FROM pg_database WHERE datname='akira'\" | grep -q 1" || \ su - postgres -c "psql -c \"CREATE DATABASE akira OWNER akira;\"" BACKUP_FILE="/akira/data/cloud_sync/akira_dump.sql" if [ -f "$BACKUP_FILE" ]; then echo "📥 [INIT] Restaurando backup..." PGPASSWORD=akira psql -h localhost -U akira -d akira -f "$BACKUP_FILE" 2>/dev/null || true fi echo "🚀 [INIT] Iniciando Akira FastAPI com 1 worker (async)..." exec uvicorn main:app \ --host 0.0.0.0 \ --port 7860 \ --timeout-keep-alive 30 \ --limit-concurrency 100