初始化提交

This commit is contained in:
chenxin committed 2026-08-21 16:00:13 +08:00
commit 887c1f449e
963 files changed
+166869

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
#!/bin/sh
# PostgreSQL 备份脚本:pg_dump 导出 + gzip 压缩 + 按天清理过期备份。
# 通过 infra.yml 的 hunjing-pg-backup 服务运行(profile: backup):
# docker compose -f docker/docker-compose.infra.yml --profile backup run --rm hunjing-pg-backup
# 备份产物落在项目根 backups/ 目录(已加入 .gitignore)。
set -eu
PGDATABASE="${PGDATABASE:-hunjing}"
PGHOST="${PGHOST:-hunjing-postgres}"
PGPORT="${PGPORT:-5432}"
mkdir -p /backups
TS=$(date +%Y%m%d-%H%M%S)
OUT="/backups/${PGDATABASE}-${TS}.sql.gz"
echo "[backup] 导出 ${PGDATABASE}@${PGHOST}:${PGPORT} -> ${OUT}"
pg_dump --no-owner --no-privileges | gzip > "$OUT"
# 清理过期备份
retain="${BACKUP_RETAIN_DAYS:-7}"
echo "[backup] 清理 ${retain} 天前的备份"
find /backups -name '*.sql.gz' -type f -mtime +"$retain" -delete 2>/dev/null || true
echo "[backup] 完成。当前备份列表:"
ls -lh /backups 2>/dev/null || true
+31
View File
@@ -0,0 +1,31 @@
#!/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] 完成"