Spaces:
Paused
Paused
Fix commited on
Commit ·
0f70664
1
Parent(s): f2efadc
feat: add automated database backup and restore
Browse files- Add GitHub Actions workflows for sync (10min) and cleanup (15min)
- Update Dockerfile to include Python and sync scripts
- Update entrypoint to restore auth data on startup
- Add cleanup mode to sync_auths.py
Files changed (5)
- .github/workflows/cleanup.ymlAdded+34
- .github/workflows/sync.ymlAdded+46
- Dockerfile+4-1
- entrypoint.sh+8
- sync_auths.py+18-3
12345678910111213141516171819202122232425262728293031323334name: Cleanup Stale Auth
on: schedule: # Every 15 minutes: delete stale PG records - cron: '*/15 * * * *' workflow_dispatch:
env: REPO_ID: mansui52001/grok2api
jobs: cleanup: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4
- name: Setup Python uses: actions/setup-python@v5 with: python-version: '3.11'
- name: Install dependencies run: pip install psycopg2-binary requests huggingface_hub
- name: Cleanup stale records env: HF_TOKEN: ${{ secrets.HF_TOKEN }} PGSTORE_DSN: ${{ secrets.PGSTORE_DSN }} REMOTE_MGMT_KEY: ${{ secrets.REMOTE_MGMT_KEY }} run: | echo "Running cleanup mode..." python sync_auths.py cleanup12345678910111213141516171819202122232425262728293031323334353637383940414243444546name: Auth Sync
on: schedule: # Every 10 minutes: backup HF → PG - cron: '*/10 * * * *' workflow_dispatch: inputs: mode: description: 'Sync mode' required: true default: 'sync' type: choice options: - sync - backup - restore - cleanup
env: REPO_ID: mansui52001/grok2api
jobs: sync: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4
- name: Setup Python uses: actions/setup-python@v5 with: python-version: '3.11'
- name: Install dependencies run: pip install psycopg2-binary requests huggingface_hub
- name: Run sync env: HF_TOKEN: ${{ secrets.HF_TOKEN }} PGSTORE_DSN: ${{ secrets.PGSTORE_DSN }} REMOTE_MGMT_KEY: ${{ secrets.REMOTE_MGMT_KEY }} run: | MODE="${{ github.event.inputs.mode || 'sync' }}" echo "Running sync in mode: $MODE" python sync_auths.py "$MODE"123456712 unmodified lines202122232425FROM debian:bookworm-slimRUN apt-get update && apt-get install -y --no-install-recommends \ca-certificates curl tzdata && \rm -rf /var/lib/apt/lists/*WORKDIR /CLIProxyAPI12 unmodified linesCOPY config.yaml /CLIProxyAPI/config.yamlCOPY config.yaml /CLIProxyAPI/config.example.yamlRUN mkdir -p /data/auths123456712 unmodified lines202122232425262728FROM debian:bookworm-slimRUN apt-get update && apt-get install -y --no-install-recommends \ca-certificates curl tzdata python3 python3-pip && \rm -rf /var/lib/apt/lists/*WORKDIR /CLIProxyAPI12 unmodified linesCOPY config.yaml /CLIProxyAPI/config.yamlCOPY config.yaml /CLIProxyAPI/config.example.yamlCOPY sync_auths.py /CLIProxyAPI/sync_auths.pyRUN pip3 install --no-cache-dir psycopg2-binary requests huggingface_hubRUN mkdir -p /data/auths
18 unmodified lines192021222318 unmodified linesexit 1fiecho "Starting cli-proxy-api..."exec ./cli-proxy-api18 unmodified lines1920212223242526272829303118 unmodified linesexit 1fi# Restore auth data from database on startupif [ -n "$PGSTORE_DSN" ]; thenecho "Restoring auth data from database..."python3 /CLIProxyAPI/sync_auths.py restore 2>&1 || echo "Warning: Restore failed, continuing startup"elseecho "PGSTORE_DSN not set, skipping database restore"fiecho "Starting cli-proxy-api..."exec ./cli-proxy-api
147 unmodified lines1481491501511521531 unmodified line1551561571581591601611621631641 unmodified line166167168169170171172173174175176177147 unmodified linesprint(f"Restored {restored}/{len(pg_files)} auth files from PG to HF")def main():mode = sys.argv[1] if len(sys.argv) > 1 else "sync"print(f"=== Auth Sync Mode: {mode} ===")1 unmodified lineconn = get_pg_conn()ensure_table(conn)hf_auth_files = get_hf_auth_files()print(f"HF Space has {len(hf_auth_files)} auth files")if mode in ("backup", "sync"):backup_to_pg(conn, hf_auth_files)if mode in ("sync",):1 unmodified lineif mode in ("restore", "sync"):# Only restore if HF has 0 files (indicating restart with data loss)if len(hf_auth_files) == 0:print("HF has no auth files - restoring from PG...")restore_from_pg(conn)else:print(f"HF has {len(hf_auth_files)} files, no restore needed")conn.close()print("=== Sync Complete ===")147 unmodified lines1481491501511521531541551561571581591601611621631641 unmodified line1661671681691701711721731741 unmodified line176177178179180181182183184185186187188189190191192147 unmodified linesprint(f"Restored {restored}/{len(pg_files)} auth files from PG to HF")def cleanup_stale(conn):"""Delete PG records that no longer exist on HF (cleanup mode)."""hf_auth_files = get_hf_auth_files()print(f"HF Space has {len(hf_auth_files)} auth files")if not hf_auth_files:print("HF has 0 auth files, skipping cleanup to avoid accidental data loss")returndelete_stale_from_pg(conn, set(hf_auth_files.keys()))def main():mode = sys.argv[1] if len(sys.argv) > 1 else "sync"print(f"=== Auth Sync Mode: {mode} ===")1 unmodified lineconn = get_pg_conn()ensure_table(conn)if mode in ("backup", "sync"):hf_auth_files = get_hf_auth_files()print(f"HF Space has {len(hf_auth_files)} auth files")backup_to_pg(conn, hf_auth_files)if mode in ("sync",):1 unmodified lineif mode in ("restore", "sync"):# Only restore if HF has 0 files (indicating restart with data loss)if mode == "restore":hf_auth_files = get_hf_auth_files()if len(hf_auth_files) == 0:print("HF has no auth files - restoring from PG...")restore_from_pg(conn)else:print(f"HF has {len(hf_auth_files)} files, no restore needed")if mode == "cleanup":cleanup_stale(conn)conn.close()print("=== Sync Complete ===")
.github/workflows/cleanup.ymlAdded+34
.github/workflows/sync.ymlAdded+46
Dockerfile+4-1
entrypoint.sh+8
sync_auths.py+18-3