在 CentOS 上保障 GitLab 数据安全的可落地方案
一 网络安全与访问控制
firewall-cmd --permanent --add-service=http --add-service=https --add-service=ssh && firewall-cmd --reload。如需调整 SSH 端口,务必同步更新防火墙放行规则。external_url 设为 https:// 并开启 HTTP→HTTPS 重定向;证书可使用 Let’s Encrypt 或企业 CA,确保传输加密与证书自动续期。二 备份与快速恢复
gitlab-backup create(GitLab 12.1+)或 gitlab-rake gitlab:backup:create(旧版)执行备份;建议通过 crontab 定时执行,如每日 02:00 备份。/etc/gitlab/gitlab.rb 中设置 gitlab_rails['backup_keep_time'](如 2592000 秒=30 天)与 gitlab_rails['backup_path'](如 /var/opt/gitlab/backups),执行 gitlab-ctl reconfigure 使配置生效。unicorn/sidekiq),执行 gitlab-backup create 或 gitlab-rake gitlab:backup:restore BACKUP=...,再启动服务并校验数据。三 系统与 GitLab 加固
四 关键配置与命令清单
| 目标 | 关键配置/命令 |
|---|---|
| 防火墙放行 | firewall-cmd --permanent --add-service=http --add-service=https --add-service=ssh && firewall-cmd --reload |
| 启用 HTTPS | external_url 'https://gitlab.example.com';nginx['redirect_http_to_https'] = true;证书路径:/etc/letsencrypt/live/.../fullchain.pem 与 privkey.pem;执行 gitlab-ctl reconfigure |
| 定时备份 | 0 2 * * * /opt/gitlab/bin/gitlab-backup create(路径与保留天数在 gitlab.rb 中设置) |
| 备份保留 | gitlab_rails['backup_keep_time'] = 2592000(30 天) |
| 手动备份 | gitlab-backup create(12.1+)或 gitlab-rake gitlab:backup:create |
| 恢复操作 | 停服务:gitlab-ctl stop unicorn && gitlab-ctl stop sidekiq;恢复:gitlab-backup create 或 gitlab-rake gitlab:backup:restore BACKUP=...;启动:gitlab-ctl start |
| 配置文件备份 | 手动拷贝:/etc/gitlab/gitlab.rb、/etc/gitlab/gitlab-secrets.json |
五 运维与演练建议