在 CentOS 上备份与恢复 GitLab 的完整实践
一 环境准备与关键配置
gitlab-ctl status 查看服务;如需查看版本,可执行 cat /opt/gitlab/embedded/service/gitlab-rails/VERSION。gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"(默认备份目录)gitlab_rails['backup_keep_time'] = 604800(保留 7 天,单位为秒)gitlab-ctl reconfigure(或 gitlab-ctl restart)。二 手动备份步骤
sudo gitlab-rake gitlab:backup:create。备份完成后,归档会出现在 /var/opt/gitlab/backups(或自定义的 backup_path)。scp 或 rsync),并确保目标机器 GitLab 版本与源机器一致,以避免兼容性问题。三 定时自动备份
crontab 定时执行备份(示例为每天 02:00 执行):
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:createbackup_keep_time),避免磁盘被历史备份占满。四 恢复步骤
gitlab-ctl stop unicorngitlab-ctl stop sidekiqgitlab-rake gitlab:backup:restore BACKUP=1690391227gitlab-rake gitlab:backup:restore BACKUP=1690391227_2023_07_27_12.3.4-eegitlab-ctl start(或 gitlab-ctl restart)五 迁移与异地容灾建议
external_url、邮件等配置,然后执行 gitlab-ctl reconfigure && gitlab-ctl restart。scp/rsync;备份文件属主建议为 git:git,权限不低于 0600,避免因权限不足导致恢复失败。cron 将备份自动上传至异地存储(如对象存储/备份服务器),并在异地设置定期清理策略(如保留 14 天)。