meltedchoco commited on
Commit
909accc
Β·
verified Β·
1 Parent(s): d924e42

Update entrypoint.sh

Browse files
Files changed (1) hide show
  1. entrypoint.sh +41 -30
entrypoint.sh CHANGED
@@ -1,49 +1,60 @@
1
  #!/usr/bin/env bash
2
  set -euo pipefail
3
 
4
- PGDATA="${PGDATA:-/data/postgres}"
5
- PGHOST="${PGHOST:-/tmp}"
6
- PG_PORT="${PG_PORT:-5432}"
 
 
7
  SEKAI_DB_URL="postgresql://sekai:sekai@/sekai?host=${PGHOST}&port=${PG_PORT}"
8
 
9
- # ── Self-Healing: Check for corrupted state ──────────────────────────
10
- # If the directory exists AND is not empty, but is missing PG_VERSION, it's likely corrupted.
11
- if [ -d "${PGDATA}" ] && [ "$(ls -A ${PGDATA})" ] && [ ! -f "${PGDATA}/PG_VERSION" ]; then
12
- echo "Detected corrupted or incomplete PostgreSQL data (missing PG_VERSION). Wiping for a fresh start..."
13
- rm -rf "${PGDATA:?}"/*
14
- fi
15
-
16
- # ── Initialize Postgres if needed ────────────────────────────────────
17
- if [ ! -f "${PGDATA}/PG_VERSION" ]; then
18
- echo "Initializing PostgreSQL database in ${PGDATA}..."
19
- mkdir -p "${PGDATA}"
20
- chmod 700 "${PGDATA}" # ← postgres requires exactly 0700
21
- initdb --auth=trust --no-locale --encoding=UTF8 "${PGDATA}"
22
- fi
23
 
24
- chmod 700 "${PGDATA}" # ← also fix on restart if perms drifted
25
 
26
- # ── Cleanup stale pid if container restarted uncleanly ───────────────
27
- rm -f "${PGDATA}/postmaster.pid"
 
 
 
28
 
29
- # ── Start Postgres ───────────────────────────────────────────────────
30
- pg_ctl start -l "${PGDATA}/postgres.log" \
31
- -o "-k ${PGHOST} -p ${PG_PORT}" -w || {
32
- echo "--- postgres.log ---"
33
  cat "${PGDATA}/postgres.log"
34
  exit 1
35
  }
36
 
37
- # ── Create user and database if they don't exist ─────────────────────
38
  if ! psql -h "${PGHOST}" -p "${PG_PORT}" -U sekai -d sekai -c '\q' 2>/dev/null; then
39
- echo "Creating sekai database..."
40
- psql -h "${PGHOST}" -p "${PG_PORT}" -d postgres -c "CREATE DATABASE sekai OWNER sekai;"
41
  fi
42
 
43
- # ── Set database URL for the app ─────────────────────────────────────
44
- export SEKAI_DATABASE_URL="${SEKAI_DB_URL}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
 
46
  echo "PostgreSQL ready at ${PGHOST}:${PG_PORT}"
47
 
48
- # ── Start the app ────────────────────────────────────────────────────
49
  exec uv run python main.py
 
1
  #!/usr/bin/env bash
2
  set -euo pipefail
3
 
4
+ # 1. Configuration
5
+ # Live database lives in the fast, ephemeral container storage
6
+ PGDATA="/tmp/postgres"
7
+ PGHOST="/tmp"
8
+ PG_PORT="5432"
9
  SEKAI_DB_URL="postgresql://sekai:sekai@/sekai?host=${PGHOST}&port=${PG_PORT}"
10
 
11
+ # Our Hugging Face bucket is now safely used just for backups
12
+ BACKUP_DIR="/data/db_backups"
13
+ BACKUP_FILE="${BACKUP_DIR}/sekai_backup.sql"
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ mkdir -p "${BACKUP_DIR}"
16
 
17
+ # 2. Initialize a fresh, ephemeral database
18
+ echo "Initializing ephemeral PostgreSQL in ${PGDATA}..."
19
+ mkdir -p "${PGDATA}"
20
+ chmod 700 "${PGDATA}"
21
+ initdb --auth=trust --no-locale --encoding=UTF8 "${PGDATA}" >/dev/null
22
 
23
+ # 3. Start Postgres
24
+ echo "Starting PostgreSQL..."
25
+ pg_ctl start -l "${PGDATA}/postgres.log" -o "-k ${PGHOST} -p ${PG_PORT}" -w >/dev/null || {
 
26
  cat "${PGDATA}/postgres.log"
27
  exit 1
28
  }
29
 
30
+ # 4. Create sekai user and database
31
  if ! psql -h "${PGHOST}" -p "${PG_PORT}" -U sekai -d sekai -c '\q' 2>/dev/null; then
32
+ psql -h "${PGHOST}" -p "${PG_PORT}" -d postgres -c "CREATE DATABASE sekai OWNER sekai;" >/dev/null
 
33
  fi
34
 
35
+ # 5. Restore from persistent backup if it exists
36
+ if [ -f "${BACKUP_FILE}" ]; then
37
+ echo "Found persistent backup! Restoring database state..."
38
+ psql -h "${PGHOST}" -p "${PG_PORT}" -d sekai < "${BACKUP_FILE}" >/dev/null
39
+ else
40
+ echo "No existing backup found. Starting fresh."
41
+ fi
42
+
43
+ # 6. Start Background Auto-Backup Process
44
+ (
45
+ while true; do
46
+ sleep 300 # Wait 5 minutes
47
+
48
+ # Dump to a temporary file first, then move it.
49
+ # This prevents the backup from corrupting if the container crashes mid-save.
50
+ pg_dump -h "${PGHOST}" -p "${PG_PORT}" -U sekai -d sekai --clean --if-exists > "${BACKUP_FILE}.tmp"
51
+ mv "${BACKUP_FILE}.tmp" "${BACKUP_FILE}"
52
+ echo "[Backup] Database automatically saved to persistent storage."
53
+ done
54
+ ) &
55
 
56
+ export SEKAI_DATABASE_URL="${SEKAI_DB_URL}"
57
  echo "PostgreSQL ready at ${PGHOST}:${PG_PORT}"
58
 
59
+ # 7. Start the app
60
  exec uv run python main.py