- 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
64 lines
2.2 KiB
Bash
Executable File
64 lines
2.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# dr-sync.sh — L1 热备: 连续镜像所有源仓库 + fsck 校验 + 与源清单漂移检测
|
|
# 源库只读(仅 GET API + clone/fetch, 禁用向源推送)
|
|
set -uo pipefail
|
|
source "$(cd "$(dirname "$0")" && pwd)/dr.conf"
|
|
LOCK="/tmp/dr-sync.lock"; exec 9>"$LOCK"; flock -n 9 || { echo "另一实例运行中"; exit 1; }
|
|
mkdir -p "$MIRROR_DIR"
|
|
|
|
# 私有库认证: GIT_ASKPASS 从环境变量取 token(不落盘、不进 ps)
|
|
export SOURCE_TOKEN
|
|
if [ -n "$SOURCE_TOKEN" ]; then
|
|
AP=$(mktemp); cat > "$AP" <<'EOF'
|
|
#!/bin/sh
|
|
case "$1" in *Username*) printf '%s' oauth2;; *) printf '%s' "$SOURCE_TOKEN";; esac
|
|
EOF
|
|
chmod 700 "$AP"; export GIT_ASKPASS="$AP"; trap 'rm -f "$AP"' EXIT
|
|
fi
|
|
|
|
# 列出源全部仓库(自动翻页, 带 token 含私有)
|
|
list_src() {
|
|
local page=1 out n
|
|
local hdr=()
|
|
[ -n "$SOURCE_TOKEN" ] && hdr=( -H "Authorization: token $SOURCE_TOKEN" )
|
|
while :; do
|
|
out=$(curl -sf "${hdr[@]}" "${SOURCE_URL}/api/v1/repos/search?limit=50&page=${page}") || break
|
|
n=$(echo "$out" | jq -r '.data | length' 2>/dev/null) || break
|
|
case "$n" in ''|*[!0-9]*) break;; esac
|
|
[ "$n" -eq 0 ] && break
|
|
echo "$out" | jq -r '.data[].clone_url'
|
|
page=$((page+1))
|
|
done
|
|
}
|
|
|
|
fail=0; synced=0
|
|
for url in $(list_src); do
|
|
# 用 SOURCE_URL(局域网) 重建 clone_url, 避免走公网 ROOT_URL
|
|
path="${url#*://}"; path="${path#*/}"
|
|
url="$SOURCE_URL/${path}"
|
|
name=$(basename "$url")
|
|
d="$MIRROR_DIR/$name"
|
|
if [ ! -d "$d" ]; then
|
|
git clone --mirror "$url" "$d" || { echo "clone 失败 $name"; rm -rf "$d"; fail=$((fail+1)); continue; }
|
|
else
|
|
git -C "$d" remote set-url origin "$url" || true # 旧镜像重指到 LAN
|
|
git -C "$d" remote update --prune || { echo "fetch 失败 $name"; fail=$((fail+1)); continue; }
|
|
fi
|
|
git -C "$d" remote set-url --push origin "no-push://ro" 2>/dev/null || true
|
|
synced=$((synced+1))
|
|
done
|
|
|
|
# fsck 完整性校验
|
|
echo "--- git fsck ---"
|
|
for d in "$MIRROR_DIR"/*.git; do
|
|
[ -d "$d" ] || continue
|
|
if git -C "$d" fsck --no-progress --full >/dev/null 2>&1; then
|
|
echo " OK $(basename "$d")"
|
|
else
|
|
echo " FSCK 异常 $(basename "$d")"; fail=$((fail+1))
|
|
fi
|
|
done
|
|
|
|
echo "完成: 同步 ${synced}, 失败 ${fail}"
|
|
[ "$fail" -eq 0 ]
|