#!/bin/sh # PostgreSQL 恢复脚本:从指定备份文件恢复到数据库(覆盖现有数据)。 # 通过 infra.yml 的 hunjing-pg-backup 服务运行,需覆盖 entrypoint 并传入 RESTORE_FILE: # docker compose -f docker/docker-compose.infra.yml --profile backup run --rm \ # -e RESTORE_FILE=hunjing-20260620-000000.sql.gz \ # --entrypoint /bin/sh hunjing-pg-backup /backup/backup-restore.sh set -eu PGDATABASE="${PGDATABASE:-hunjing}" file="${RESTORE_FILE:-}" if [ -z "$file" ]; then echo "[restore] 请通过 RESTORE_FILE 指定备份文件名,例如:" echo " -e RESTORE_FILE=${PGDATABASE}-20260620-000000.sql.gz" echo "[restore] 可用备份:" ls -1 /backups/*.sql.gz 2>/dev/null | sed 's|.*/||' || echo " (无)" exit 1 fi target="/backups/$file" if [ ! -f "$target" ]; then echo "[restore] 备份文件不存在: $target" exit 1 fi echo "[restore] 从 $file 恢复到 ${PGDATABASE}@${PGHOST:-hunjing-postgres}:${PGPORT:-5432}" echo "[restore] ⚠️ 将覆盖现有数据,5 秒后开始(Ctrl+C 取消)" sleep 5 gunzip -c "$target" | psql -v ON_ERROR_STOP=1 echo "[restore] 完成"