- 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
141 lines
5.6 KiB
Bash
Executable File
141 lines
5.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# gitea-mirror.sh — 把源 Gitea 的所有公开仓库批量镜像到目标 Gitea
|
|
# 支持增量同步:已存在的仓库 fetch 更新,再 push --mirror 到目标
|
|
# 源库全程只读: 仅 GET API + git clone/fetch, 且禁用向源(origin)推送
|
|
#
|
|
# 用法:
|
|
# 1. 修改下面 “配置” 区
|
|
# 2. chmod +x gitea-mirror.sh && ./gitea-mirror.sh
|
|
# 3. 定时: crontab -e -> */30 * * * * /home/tonycao/work/repo/gitea-mirror.sh >> /home/tonycao/work/repo/mirror.log 2>&1
|
|
#
|
|
set -euo pipefail
|
|
|
|
# ============ 配置 ============
|
|
SOURCE_URL="http://www.kanghexin.work:3000" # 源 Gitea 地址
|
|
SOURCE_TOKEN="" # 源 token(公开仓库可留空)
|
|
|
|
TARGET_URL="http://your-gitea.example.com:3000" # 目标 Gitea 地址
|
|
TARGET_TOKEN="" # 目标 token(用于建库+推送)
|
|
TARGET_OWNER="mirror" # 目标命名空间(用户或组织)
|
|
TARGET_IS_ORG="0" # 1=TARGET_OWNER 是组织(建库用 org 接口)
|
|
TARGET_PUSH_USER="oauth2" # 推送时 HTTP basic 的用户名,token 当密码
|
|
|
|
MIRROR_DIR="$HOME/gitea-mirror-data" # 本地裸镜像存放目录
|
|
CREATE_MISSING="1" # 1=目标没有的仓库自动创建
|
|
DRY_RUN="0" # 1=只打印不执行
|
|
# ==============================
|
|
|
|
LOCK="/tmp/gitea-mirror.lock"
|
|
exec 9>"$LOCK"
|
|
flock -n 9 || { echo "另一个实例正在运行,退出"; exit 1; }
|
|
|
|
SOURCE_URL="${SOURCE_URL%/}"; TARGET_URL="${TARGET_URL%/}"
|
|
for u in "$SOURCE_URL" "$TARGET_URL"; do
|
|
case "$u" in http://*|https://*) ;; *) echo "错误: 地址需以 http:// 或 https:// 开头: $u"; exit 1;; esac
|
|
done
|
|
if [ "$DRY_RUN" != "1" ] && [ -z "$TARGET_TOKEN" ]; then
|
|
echo "错误: 非 dry-run 必须设置 TARGET_TOKEN"; exit 1
|
|
fi
|
|
|
|
mkdir -p "$MIRROR_DIR"
|
|
|
|
log() { echo "[$(date '+%F %T')] $*"; }
|
|
|
|
# 推送凭证走 GIT_ASKPASS: token 只存在于环境变量,不进 ps/日志/报错URL
|
|
AP="$(mktemp)"
|
|
cat > "$AP" <<'EOF'
|
|
#!/bin/sh
|
|
case "$1" in *Username*) printf '%s' "$MIRROR_PUSH_USER";; *) printf '%s' "$MIRROR_PUSH_TOKEN";; esac
|
|
EOF
|
|
chmod 700 "$AP"
|
|
export MIRROR_PUSH_USER="$TARGET_PUSH_USER" MIRROR_PUSH_TOKEN="$TARGET_TOKEN" GIT_ASKPASS="$AP"
|
|
trap 'rm -f "$AP"' EXIT
|
|
# 推送 URL 保留完整 TARGET_URL(兼容子路径部署),不内嵌凭证
|
|
scheme="http"; case "$TARGET_URL" in https://*) scheme="https";; esac
|
|
push_base="${scheme}://${TARGET_URL#*://}/${TARGET_OWNER}"
|
|
|
|
# 列出源上所有仓库(自动翻页,每页 50)
|
|
list_repos() {
|
|
local page=1 out
|
|
while :; do
|
|
local args=( -s "${SOURCE_URL}/api/v1/repos/search?limit=50&page=${page}" )
|
|
[ -n "$SOURCE_TOKEN" ] && args+=( -H "Authorization: token ${SOURCE_TOKEN}" )
|
|
out=$(curl -sf "${args[@]}") || { log " 列库请求失败(page ${page})"; break; }
|
|
local n
|
|
n=$(echo "$out" | jq -r '.data | length' 2>/dev/null) || break
|
|
case "$n" in ''|*[!0-9]*) break;; esac # null/异常响应 -> 停止翻页
|
|
[ "$n" -eq 0 ] && break
|
|
echo "$out" | jq -r '.data[] | .full_name + "\t" + .clone_url'
|
|
page=$((page+1))
|
|
done
|
|
}
|
|
|
|
# 目标上确保仓库存在,不存在则创建(空库,无 README)
|
|
ensure_target_repo() {
|
|
local name="$1"
|
|
[ "$CREATE_MISSING" = "1" ] || return 0
|
|
[ "$DRY_RUN" = "1" ] && return 0
|
|
local code
|
|
code=$(curl -s -o /dev/null -w '%{http_code}' \
|
|
-H "Authorization: token ${TARGET_TOKEN}" \
|
|
"${TARGET_URL}/api/v1/repos/${TARGET_OWNER}/${name}") \
|
|
|| { log " 目标查询失败 ${name}(网络?)"; return 0; }
|
|
if [ "$code" = "404" ]; then
|
|
log " 创建目标仓库 ${TARGET_OWNER}/${name}"
|
|
local ep
|
|
if [ "$TARGET_IS_ORG" = "1" ]; then
|
|
ep="${TARGET_URL}/api/v1/orgs/${TARGET_OWNER}/repos"
|
|
else
|
|
ep="${TARGET_URL}/api/v1/user/repos"
|
|
fi
|
|
local resp c
|
|
resp=$(curl -s -w '\nHTTPCODE:%{http_code}' \
|
|
-H "Authorization: token ${TARGET_TOKEN}" \
|
|
-H "Content-Type: application/json" \
|
|
-X POST "$ep" \
|
|
-d "{\"name\":\"${name}\",\"private\":false,\"auto_init\":false}") \
|
|
|| { log " 建库请求失败 ${name}"; return 0; }
|
|
c="${resp##*HTTPCODE:}"
|
|
[ "$c" = "201" ] || log " 建库异常(${c}): ${resp%%$'\n'HTTPCODE:*}"
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
count=0 fail=0
|
|
while IFS=$'\t' read -r full_name clone_url; do
|
|
[ -z "$full_name" ] && continue
|
|
count=$((count+1))
|
|
# 目标仓库名: 把 owner/repo 的斜杠换成 __ ,避免命名冲突
|
|
dest_name="${full_name//\//__}"
|
|
dest="${MIRROR_DIR}/${dest_name}.git"
|
|
log "同步 ${full_name}"
|
|
|
|
if [ "$DRY_RUN" = "1" ]; then echo " [dry] clone/fetch ${clone_url} -> push ${push_base}/${dest_name}.git"; continue; fi
|
|
|
|
# 增量:没有则 clone --mirror,有则 fetch 更新所有分支/标签(失败不中断整批)
|
|
if [ ! -d "$dest" ]; then
|
|
if ! git clone --mirror "$clone_url" "$dest"; then
|
|
rm -rf "$dest" # 清掉半途失败的残缺目录,避免后续每轮卡死
|
|
log " clone 失败 ${full_name}"; fail=$((fail+1)); continue
|
|
fi
|
|
elif ! git -C "$dest" remote update --prune; then
|
|
log " fetch 失败 ${full_name}"; fail=$((fail+1)); continue
|
|
fi
|
|
|
|
# 硬保护: 禁用向源(origin)推送,确保源库只读;向目标推送用的是显式 URL 不受影响
|
|
git -C "$dest" remote set-url --push origin "no-push://source-is-read-only" 2>/dev/null || true
|
|
|
|
ensure_target_repo "$dest_name"
|
|
|
|
# 推送到目标(mirror 推送所有 refs)
|
|
if git -C "$dest" push --mirror "${push_base}/${dest_name}.git"; then
|
|
log " OK"
|
|
else
|
|
log " 推送失败 ${full_name}"
|
|
fail=$((fail+1))
|
|
fi
|
|
done < <(list_repos)
|
|
|
|
log "完成: 共 ${count} 个仓库, 失败 ${fail} 个"
|