温馨提示×

GitLab在CentOS上如何备份与恢复数据

小樊
33
2025-12-21 10:50:51
栏目: 智能运维

在 CentOS 上使用 Omnibus 包部署的 GitLab,备份与恢复可按下列步骤进行


一 备份

  • 手动创建全量备份
    执行命令生成包含仓库、数据库与上传文件的归档:

    • 方式一:gitlab-rake gitlab:backup:create
    • 方式二(较新版本):gitlab-backup create
      备份文件默认位于:/var/opt/gitlab/backups,文件名形如:<时间戳>_gitlab_backup.tar<时间戳><日期><版本>_gitlab_backup.tar
  • 修改备份目录与保留策略
    编辑 /etc/gitlab/gitlab.rb

    • 备份目录:gitlab_rails['backup_path'] = "/opt/gitlab-backups"
    • 保留天数:gitlab_rails['backup_keep_time'] = 2592000(单位:秒,示例为30天
      修改后执行:gitlab-ctl reconfigure 使配置生效。
  • 定时自动备份(crontab)
    示例:每天 02:00 执行备份

    • 0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create
      如使用 gitlab-backup 命令,可替换为相应路径。
  • 备份清单(迁移/灾备强烈建议一并备份)

    • 配置文件:/etc/gitlab/gitlab.rb
    • 密钥与敏感信息:/etc/gitlab/gitlab-secrets.json
    • 其他(按需):/var/opt/gitlab/nginx/conf/etc/postfix/main.cf 等。

二 恢复

  • 前置条件

    • 目标机器安装与备份时完全一致的 GitLab 版本与发行类型(CE/EE)。
    • 至少执行过一次:gitlab-ctl reconfigure
    • 将备份文件放到配置中指定的备份目录(默认 /var/opt/gitlab/backups),并确保权限正确:
      • chown git:root /var/opt/gitlab/backups
      • chown git.git /var/opt/gitlab/backups/<备份文件>.tar
      • chmod -R 755 /var/opt/gitlab/backups
  • 执行恢复

    1. 停止写入相关进程(保持其他组件运行):
      • 旧版组件名:gitlab-ctl stop unicorngitlab-ctl stop sidekiq
      • 新版本常见:gitlab-ctl stop pumagitlab-ctl stop sidekiq
    2. 执行恢复(两种写法等价,二选一):
      • 使用时间戳/编号:gitlab-rake gitlab:backup:restore BACKUP=1700647561_2023_11_22_13_12_15
      • 使用完整文件名:gitlab-rake gitlab:backup:restore BACKUP=1700647561_2023_11_22_13_12_15_gitlab_backup.tar
        恢复过程中会有两次交互式确认,输入 yes 继续。
    3. 启动与重建:
      • gitlab-ctl start
      • gitlab-ctl reconfigure
      • 如有修改域名/端口,检查 /etc/gitlab/gitlab.rbexternal_url 并重新配置。

三 迁移场景要点

  • 将旧服务器的备份文件复制到新服务器的备份目录(默认 /var/opt/gitlab/backups),并确保权限与所有权正确。
  • 新服务器 GitLab 版本需与备份时一致(CE/EE 与版本号均需匹配)。
  • 恢复完成后,按需调整 external_url、反向代理/防火墙等网络配置,再执行 gitlab-ctl reconfigure 与重启。

四 常见问题与最佳实践

  • 版本不一致会导致恢复失败或异常,迁移/恢复前务必对齐 GitLab 版本与发行类型
  • 备份文件较大,请确保备份目录所在磁盘空间充足;可按需调整 backup_keep_time滚动保留
  • 恢复前尽量避免新写入,或在维护窗口内操作,减少数据不一致风险。
  • 建议定期演练恢复流程并保留离线/异地副本,提升可用性。

0