gitea的备份仓库从一台机迁移到另一台机,发现本地迁移不被允许,通过git push mirror方式本地同步速度还是挺快的,相关脚本如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash

# ===== 配置参数 =====
GITEA_URL="https://git.dev.abc.xyz"  #gitea访问域名
GITEA_URL_1="git.dev.abc.xyz"        #gitea迁移地址
GITEA_ORG="java"		     #gitea组织名称
GITEA_USER="admin"		     #gitea 用户名
GITEA_TOKEN="abcdefghjijklmnopqrstuvwxyz"   # 替换为你的 Gitea Access Token
LOCAL_REPOS_DIR="/data/gitea/code/java/repos/java"  #要同步的本地仓库备份,须为裸仓库


GITEA_API="$GITEA_URL/api/v1"

# ===== 开始迁移裸仓库 =====
for dir in "$LOCAL_REPOS_DIR"/*.git; do
  [ -d "$dir" ] || continue

  repo_basename=$(basename "$dir")              # e.g. project1.git
  repo_name="${repo_basename%.git}"             # 去除 .git 后缀

  echo -e "\n➡️ 正在处理裸仓库: $repo_name"

  # 加入 Git 安全目录(针对裸仓库)
  git config --global --add safe.directory "$dir"

  # 1️⃣ 创建 Gitea 仓库
  response=$(curl -s -w "\n%{http_code}" -X POST "$GITEA_API/orgs/$GITEA_ORG/repos" \
    -H "Authorization: token $GITEA_TOKEN" \
    -H "Content-Type: application/json" \
    -d "{\"name\": \"$repo_name\", \"private\": true, \"auto_init\": false}")

  body=$(echo "$response" | head -n -1)
  code=$(echo "$response" | tail -n1)

  if [ "$code" = "201" ]; then
    echo "✅ 仓库 $repo_name 创建成功"
  elif [ "$code" = "409" ]; then
    echo "ℹ️ 仓库 $repo_name 已存在"
  elif [ "$code" = "422" ]; then
    echo "❌ 创建失败 (HTTP 422): 仓库名非法或冲突: $repo_name"
    continue
  else
    echo "❌ 创建失败 (HTTP $code): $body"
    continue
  fi

  # 2️⃣ 推送
  cd "$dir" || continue
  git remote remove gitea 2>/dev/null
  git remote add gitea "https://$GITEA_USER:$GITEA_TOKEN@$GITEA_URL_1/$GITEA_ORG/$repo_name.git"

  echo "⬆️ 推送 $repo_name 中..."
  git push --mirror gitea && echo "✅ 推送完成" || echo "❌ 推送失败"

done