From 323150460aedef533070bf5f25ea7867a5621223 Mon Sep 17 00:00:00 2001 From: LiuEnder Date: Thu, 7 May 2026 22:11:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ociflab-backup.sh | 54 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/ociflab-backup.sh b/ociflab-backup.sh index 14b0020..af29040 100644 --- a/ociflab-backup.sh +++ b/ociflab-backup.sh @@ -4,10 +4,12 @@ set -euo pipefail # ============================================================ # OCIFLAB 仓库备份脚本 # 每天从 whitelist.md 拉取仓库列表,克隆/更新仓库,压缩备份 +# 需要使用 Access Token 进行认证 # ============================================================ # ---------- 配置区 ---------- -WHITELIST_URL="" +GIT_ACCESS_TOKEN="" # 填写你的 Git 访问令牌 +WHITELIST_REPO_URL="" # 白名单所在仓库地址 (例: https://git.ociflab.icu/user/repo.git) SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" BACKUP_DIR="${SCRIPT_DIR}/backup" WORK_DIR="${SCRIPT_DIR}/work" @@ -42,17 +44,43 @@ send_warning() { fi } -fetch_whitelist() { - log "正在拉取 whitelist.md ..." - local http_code - http_code=$(curl -s -o "${WHITELIST_CACHE}" -w "%{http_code}" --connect-timeout 10 --max-time 30 "${WHITELIST_URL}") +git_auth_opts() { + # 返回认证选项数组,若未配置 TOKEN 则返回空 + if [[ -n "${GIT_ACCESS_TOKEN}" ]]; then + echo '-c' "http.https://git.ociflab.icu/.extraHeader=Authorization: Bearer ${GIT_ACCESS_TOKEN}" + fi +} - if [[ "${http_code}" == "200" ]] && [[ -s "${WHITELIST_CACHE}" ]]; then - log "whitelist.md 拉取成功 (HTTP ${http_code})" +fetch_whitelist() { + local whitelist_repo_dir="${SCRIPT_DIR}/whitelist_repo" + local auth_opts + auth_opts=($(git_auth_opts)) # 获取认证参数 + + log "正在更新白名单仓库..." + + # 尝试拉取/克隆 + if [[ -d "${whitelist_repo_dir}/.git" ]]; then + # 已存在则更新 + if ! git "${auth_opts[@]}" -C "${whitelist_repo_dir}" pull --ff-only --quiet 2>&1; then + log "更新白名单仓库失败,尝试重新克隆..." + rm -rf "${whitelist_repo_dir}" + fi + fi + + if [[ ! -d "${whitelist_repo_dir}/.git" ]]; then + if ! git "${auth_opts[@]}" clone --depth 1 --quiet "${WHITELIST_REPO_URL}" "${whitelist_repo_dir}" 2>&1; then + log "克隆白名单仓库失败" + fi + fi + + # 读取 whitelist.md 到缓存 + if [[ -f "${whitelist_repo_dir}/whitelist.md" ]]; then + cp "${whitelist_repo_dir}/whitelist.md" "${WHITELIST_CACHE}" + log "whitelist.md 已更新" return 0 fi - log "whitelist.md 拉取失败 (HTTP ${http_code}),尝试使用前一天缓存..." + log "whitelist.md 获取失败,尝试使用前一天缓存..." if [[ -f "${SCRIPT_DIR}/whitelist_cache.md.yesterday" ]]; then cp "${SCRIPT_DIR}/whitelist_cache.md.yesterday" "${WHITELIST_CACHE}" log "已使用前一天缓存" @@ -72,13 +100,15 @@ clone_or_pull_repo() { local repo_name repo_name=$(echo "${repo_url}" | sed 's|https://git\.ociflab\.icu/||' | tr '/' '_') local repo_path="${WORK_DIR}/${repo_name}" + local auth_opts + auth_opts=($(git_auth_opts)) if [[ -d "${repo_path}/.git" ]]; then log " 更新: ${repo_url}" - if ! git -C "${repo_path}" pull --ff-only 2>&1; then + if ! git "${auth_opts[@]}" -C "${repo_path}" pull --ff-only --quiet 2>&1; then log " 更新失败: ${repo_url},尝试重新克隆..." rm -rf "${repo_path}" - if ! git clone --depth 1 "${repo_url}" "${repo_path}" 2>&1; then + if ! git "${auth_opts[@]}" clone --depth 1 --quiet "${repo_url}" "${repo_path}" 2>&1; then log " 重新克隆也失败: ${repo_url}" return 1 fi @@ -86,7 +116,7 @@ clone_or_pull_repo() { else log " 克隆: ${repo_url}" rm -rf "${repo_path}" - if ! git clone --depth 1 "${repo_url}" "${repo_path}" 2>&1; then + if ! git "${auth_opts[@]}" clone --depth 1 --quiet "${repo_url}" "${repo_path}" 2>&1; then log " 克隆失败: ${repo_url}" return 1 fi @@ -189,4 +219,4 @@ main() { log "========== OCIFLAB 备份完成 ==========" } -main "$@" +main "$@" \ No newline at end of file