gitea-dr/dr-bundle.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

45 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# dr-bundle.sh — L2 快照: 每仓库生成当日全量 bundle + GFS 保留 + verify
# bundle 自包含、可校验、可直接 git clone 恢复(防误删/勒索传播的时间点隔离)
set -uo pipefail
source "$(cd "$(dirname "$0")" && pwd)/dr.conf"
mkdir -p "$BUNDLE_DIR"
stamp=$(date +%F)
fail=0
# 1) 生成当日全量 bundle
for d in "$MIRROR_DIR"/*.git; do
[ -d "$d" ] || continue
repo=$(basename "$d" .git)
out="$BUNDLE_DIR/$repo-$stamp.bundle"
if git -C "$d" bundle create "$out" --all 2>/dev/null && git -C "$d" bundle verify "$out" >/dev/null 2>&1; then
echo "bundle OK $repo-$stamp"
else
echo "bundle 失败 $repo"; rm -f "$out"; fail=$((fail+1))
fi
done
# 2) GFS 保留(每日/每周/每月各留 N 份)
for repo in $(ls "$BUNDLE_DIR" 2>/dev/null | sed -E "s/-[0-9]{4}-[0-9]{2}-[0-9]{2}\.bundle$//" | sort -u); do
mapfile -t list < <(ls -1 "$BUNDLE_DIR/$repo-"*.bundle 2>/dev/null | sort)
[ "${#list[@]}" -eq 0 ] && continue
declare -A sd=() sw=() sm=(); keep=()
dn=0; wn=0; mn=0
for ((i=${#list[@]}-1; i>=0; i--)); do
f=${list[i]}; base=$(basename "$f"); day=${base#"$repo-"}; day=${day%.bundle}
wk=$(date -d "$day" +%G-W%V 2>/dev/null || echo x); mo=${day%-*}
kp=0
[ -z "${sd[$day]:-}" ] && { sd[$day]=1; dn=$((dn+1)); [ "$dn" -le "$KEEP_DAILY" ] && kp=1; }
[ -z "${sw[$wk]:-}" ] && { sw[$wk]=1; wn=$((wn+1)); [ "$wn" -le "$KEEP_WEEKLY" ] && kp=1; }
[ -z "${sm[$mo]:-}" ] && { sm[$mo]=1; mn=$((mn+1)); [ "$mn" -le "$KEEP_MONTHLY" ] && kp=1; }
[ "$kp" = 1 ] && keep+=("$f")
done
for f in "${list[@]}"; do
[[ " ${keep[*]:-} " == *" $f "* ]] || { echo " 清理 $(basename "$f")"; rm -f "$f"; }
done
unset sd sw sm keep
done
echo "完成: bundle 失败 ${fail}"
[ "$fail" -eq 0 ]