# ===== 在 start.sh 中集成 restore / backup 的关键片段 ===== # 1) 数据库环境变量里,继续保留本地 PostgreSQL export DATABASE_HOST="127.0.0.1" export DATABASE_PORT="5432" export DATABASE_USER="${POSTGRES_USER}" export DATABASE_PASSWORD="${POSTGRES_PASSWORD}" export DATABASE_DBNAME="${POSTGRES_DB}" export DATABASE_SSLMODE="disable" # 2) 初始化 PostgreSQL 前后,增加 fresh_db 标记 fresh_db=false if [[ ! -s "$PGDATA/PG_VERSION" ]]; then fresh_db=true fi # 3) 在创建数据库/角色成功后、停止 bootstrap PostgreSQL 之前,增加自动恢复 if [[ "$fresh_db" == "true" && "${AUTO_RESTORE_FROM_DATASET:-true}" == "true" ]]; then echo "[restore] fresh database detected, attempting restore from dataset..." if [[ -x "$VENV_PATH/bin/python" ]]; then "$VENV_PATH/bin/python" /app/restore_from_dataset.py --restore-latest || true else echo "[restore] python venv not found, skip restore" fi fi # 4) supervisor 里增加 backup-worker [program:backup-worker] command=/bin/bash -lc "exec /usr/local/bin/backup_worker.sh" autostart=true autorestart=true startsecs=5 stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0 priority=40 # 5) Dockerfile 里要复制这些文件并安装依赖 COPY --chown=user:user backup_to_dataset.py /app/backup_to_dataset.py COPY --chown=user:user restore_from_dataset.py /app/restore_from_dataset.py COPY --chown=user:user backup_worker.sh /usr/local/bin/backup_worker.sh COPY --chown=user:user requirements.txt /app/requirements.txt # 6) HF Space Variables / Secrets 建议新增 # Secrets: # HF_TOKEN=你的 HF 写入 Token # DATASET_REPO_ID=你的用户名/你的dataset仓库 # Variables: # BACKUP_INTERVAL_MINUTES=60 # BACKUP_KEEP_LAST=10 # AUTO_RESTORE_FROM_DATASET=true