songtianlun
bf753e1255
- Create a new shell script for mirroring GitHub repositories to Gitea. - Implement configuration options for GitHub and Gitea credentials. - Add functionality to skip specific repositories during synchronization. - Include error handling and logging for better debugging. - Provide detailed usage instructions and environment variable setup. This commit introduces a new feature that automates the process of mirroring GitHub repositories to a Gitea instance. It supports batch synchronization, allows skipping certain repositories, and includes a robust error handling mechanism. The script is designed to be used with crontab for scheduled execution, making it suitable for regular backups of GitHub repositories to Gitea.
2.9 KiB
2.9 KiB
GitHub to Gitea Mirror Script
这是一个自动将 GitHub 仓库镜像到 Gitea 的 Shell 脚本。支持批量同步所有仓库,可以设置跳过特定仓库,并具有良好的错误处理机制。
功能特性
- 自动同步 GitHub 所有仓库到 Gitea
- 支持设置跳过特定仓库
- 分级推送策略(先尝试 mirror,失败后逐个推送分支)
- 详细的进度显示和错误提示
- 支持通过环境变量配置
- 适合配合 crontab 使用
必要条件
- Git
- curl
- jq
- GitHub Token(如需访问私有仓库)
- Gitea Token
环境变量
变量名 | 必需 | 说明 | 示例 |
---|---|---|---|
GITHUB_USER | 是 | GitHub 用户名 | songtianlun |
GITHUB_TOKEN | 否 | GitHub 访问令牌 | ghp_xxxxxxxxxxxx |
GITEA_URL | 是 | Gitea 实例地址 | https://git.example.com |
GITEA_USER | 是 | Gitea 用户名 | username |
GITEA_TOKEN | 是 | Gitea 访问令牌 | d4209xxxxxxxxxxxxx |
SKIP_REPOS | 否 | 跳过的仓库列表(逗号分隔) | repo1,repo2,repo3 |
WORK_DIR | 否 | 临时工作目录 | /tmp/git-mirror |
使用方法
1. 直接运行
GITHUB_USER=username \
GITHUB_TOKEN=your-github-token \
GITEA_URL=https://git.example.com \
GITEA_USER=username \
GITEA_TOKEN=your-gitea-token \
bash mirror.sh
2. 配置环境变量后运行
# 设置环境变量
export GITHUB_USER=username
export GITHUB_TOKEN=your-github-token
export GITEA_URL=https://git.example.com
export GITEA_USER=username
export GITEA_TOKEN=your-gitea-token
# 运行脚本
bash mirror.sh
3. 设置定时任务
编辑 crontab:
crontab -e
添加定时任务(每天凌晨 2 点运行):
0 2 * * * GITHUB_USER=username GITHUB_TOKEN=xxx GITEA_URL=https://git.example.com GITEA_USER=username GITEA_TOKEN=xxx /path/to/mirror.sh >> /path/to/mirror.log 2>&1
4. 跳过特定仓库
GITHUB_USER=username \
GITEA_URL=https://git.example.com \
GITEA_USER=username \
GITEA_TOKEN=xxx \
SKIP_REPOS="repo1,repo2,repo3" \
bash mirror.sh
常见问题
-
获取 GitHub Token
- 访问 GitHub Settings -> Developer settings -> Personal access tokens
- 创建新的 Token,至少需要
repo
权限
-
获取 Gitea Token
- 访问 Gitea 设置 -> 应用 -> 创建新的令牌
- 需要仓库的读写权限
-
日志查看
# 如果配置了日志输出 tail -f /path/to/mirror.log
-
错误处理
- 检查令牌权限是否正确
- 确保 Gitea 实例可访问
- 验证用户名和 URL 是否正确
调试模式
添加 -x
参数启用调试模式:
bash -x mirror.sh
注意事项
- 建议使用专门的目录存放脚本和日志
- 定期检查日志确保同步正常
- 谨慎保管 Token,不要泄露
- 建议先使用测试账号验证配置
- 大型仓库同步可能需要较长时间
License
MIT License
贡献
欢迎提交 Issue 和 Pull Request!