gitea-dr/dr-restore.sh
tonycao 00a378178f Add Gitea DR toolkit: LAN mirror, bundle snapshots, dump, verify, restore
- dr-sync: mirror all repos (incl private via token) over LAN, fsck, source read-only guard
- dr-bundle: weekly full bundles with GFS retention
- dr-dump: gitea dump --skip-repository via docker exec, zero source residue
- dr-verify: drift + HEAD-sync + bundle integrity checks
- dr-restore: one-command recovery to a fresh Gitea
- systemd timers; secrets/data gitignored, dr.conf.example provided
2026-11-21 13:16:02 +08:00

30 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# dr-restore.sh — L4 恢复: 把本地镜像 push 回一个新 Gitea(主节点损坏后重建)
# 用法: ./dr-restore.sh <TARGET_URL> <OWNER> [TOKEN] (token 留空则需预建空库)
# 元数据(用户/issue)另用: 新 Gitea 上 gitea restore dumps/gitea-dump-*.zip
set -uo pipefail
source "$(cd "$(dirname "$0")" && pwd)/dr.conf"
T="${1:-$RESTORE_TARGET_URL}"; OWNER="${2:-$RESTORE_TARGET_OWNER}"; TOK="${3:-}"
{ [ -z "$T" ] || [ -z "$OWNER" ]; } && { echo "用法: $0 <TARGET_URL> <OWNER> [TOKEN]"; exit 1; }
T="${T%/}"
scheme="http"; case "$T" in https://*) scheme="https";; esac
push_base="${scheme}://${T#*://}/${OWNER}"
[ -n "$TOK" ] && export MIRROR_PUSH_TOKEN="$TOK" MIRROR_PUSH_USER="oauth2"
AP=$(mktemp); cat > "$AP" <<'EOF'
#!/bin/sh
case "$1" in *Username*) printf '%s' "oauth2";; *) printf '%s' "$MIRROR_PUSH_TOKEN";; esac
EOF
chmod 700 "$AP"; export GIT_ASKPASS="$AP"; trap 'rm -f "$AP"' EXIT
for d in "$MIRROR_DIR"/*.git; do
[ -d "$d" ] || continue
name=$(basename "$d" .git)
if [ -n "$TOK" ]; then
code=$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: token $TOK" "$T/api/v1/repos/$OWNER/$name") || code=err
[ "$code" = "404" ] && curl -s -o /dev/null -H "Authorization: token $TOK" -H "Content-Type: application/json" \
-X POST "$T/api/v1/user/repos" -d "{\"name\":\"$name\",\"private\":false,\"auto_init\":false}"
fi
if git -C "$d" push --mirror "$push_base/$name.git"; then echo "恢复 OK $name"; else echo "恢复失败 $name"; fi
done
echo "代码恢复完成; 元数据请在新机执行: gitea restore $DUMP_DIR/<最新>.zip"