mirrorGit/README.md
songtianlun 54abd6106f docs: update README for email notification feature
- Add section for email notification configuration
- Include environment variables required for SMTP setup
- Provide examples for complete configuration and crontab usage

This update enhances the documentation by detailing how to
configure email notifications after script execution. It
ensures users can easily set up and utilize this feature,
providing clear examples and necessary environment variables.
2025-01-03 17:45:42 +08:00

4.7 KiB
Raw Permalink Blame History

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
ENABLE_MAIL 是否启用邮件通知 truefalse
SMTP_SERVER SMTP 服务器地址 smtp.gmail.com
SMTP_PORT SMTP 端口 587
SMTP_USER SMTP 用户名 your-email@gmail.com
SMTP_PASS SMTP 密码 your-password
MAIL_TO 接收通知的邮箱 your-email@example.com
MAIL_FROM 发件人地址 noreply@example.com

日志文件

脚本会自动创建日志文件,包含完整的运行记录:

  • 默认日志目录:/tmp/github-mirror-logs
  • 日志文件名格式:mirror-YYYYMMDD-HHMMSS.log
  • 每次运行创建新的日志文件

使用方法

直接运行

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

配置环境变量后运行

# 设置环境变量
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

设置定时任务

编辑 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

跳过特定仓库

GITHUB_USER=username \
GITEA_URL=https://git.example.com \
GITEA_USER=username \
GITEA_TOKEN=xxx \
SKIP_REPOS="repo1,repo2,repo3" \
bash mirror.sh

邮件通知配置

脚本支持在运行完成后发送邮件通知,需要配置以下环境变量:

变量名 必需 说明 示例
SMTP_SERVER SMTP 服务器地址 smtp.gmail.com
SMTP_PORT SMTP 端口 587
SMTP_USER SMTP 用户名 your-email@gmail.com
SMTP_PASS SMTP 密码 your-password
MAIL_TO 接收通知的邮箱 your-email@example.com
MAIL_FROM 发件人地址(默认为 SMTP_USER noreply@example.com

邮件通知使用示例

完整配置示例

GITHUB_USER=username \
GITHUB_TOKEN=xxx \
GITEA_URL=https://git.example.com \
GITEA_USER=username \
GITEA_TOKEN=xxx \
SMTP_SERVER=smtp.gmail.com \
SMTP_PORT=587 \
SMTP_USER=your-email@gmail.com \
SMTP_PASS=your-password \
MAIL_TO=your-email@example.com \
bash mirror.sh

Crontab 配置示例

0 2 * * * GITHUB_USER=username GITHUB_TOKEN=xxx GITEA_URL=https://git.example.com GITEA_USER=username GITEA_TOKEN=xxx SMTP_SERVER=smtp.gmail.com SMTP_PORT=587 SMTP_USER=your-email@gmail.com SMTP_PASS=your-password MAIL_TO=your-email@example.com /path/to/mirror.sh

常见问题

  1. 获取 GitHub Token

    • 访问 GitHub Settings -> Developer settings -> Personal access tokens
    • 创建新的 Token至少需要 repo 权限
  2. 获取 Gitea Token

    • 访问 Gitea 设置 -> 应用 -> 创建新的令牌
    • 需要仓库的读写权限
  3. 错误处理

    • 检查令牌权限是否正确
    • 确保 Gitea 实例可访问
    • 验证用户名和 URL 是否正确
  4. 调试模式

添加 -x 参数启用调试模式:

bash -x mirror.sh

注意事项

  • 建议使用专门的目录存放脚本和日志
  • 定期检查日志确保同步正常
  • 谨慎保管 Token不要泄露
  • 建议先使用测试账号验证配置
  • 大型仓库同步可能需要较长时间

License

MIT License

贡献

欢迎提交 Issue 和 Pull Request