一、备份前的准备工作
sudo gitlab-rake gitlab:env:info | grep "GitLab version"
或cat /opt/gitlab/version-manifest.txt | grep "gitlab-ce"
二、Linux下GitLab备份步骤
使用GitLab内置的Rake任务创建全量备份,包含仓库、数据库、用户/组、密钥、权限等信息:
sudo gitlab-rake gitlab:backup:create
/var/opt/gitlab/backups/
目录,文件名格式为TIMESTAMP_gitlab_backup.tar
(如1710000000_2025_09_25_16.11.10_gitlab_backup.tar
)。/etc/gitlab/gitlab.rb
文件,设置:gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/path/to/custom/backups" # 自定义路径(如/mnt/gitlab_backups)
保存后运行sudo gitlab-ctl reconfigure
使配置生效。通过Cron Job设置定时任务,实现每日自动备份(例如每天凌晨2点):
# 编辑当前用户的crontab
crontab -e
添加以下内容(需替换为实际备份路径):
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create
保存退出后,Cron会自动执行每日备份。
/etc/gitlab/gitlab.rb
,设置备份保留天数(如7天):gitlab_rails['backup_keep_time'] = 604800 # 单位:秒(604800秒=7天)
运行sudo gitlab-ctl reconfigure
后,GitLab会自动删除过期备份。find /var/opt/gitlab/backups -type f -mtime +7 -exec rm -rf {} \;
三、Linux下GitLab恢复步骤
sudo gitlab-ctl stop puma # 停止Web服务
sudo gitlab-ctl stop sidekiq # 停止后台任务
或一键停止所有服务:sudo gitlab-ctl stop
将备份文件复制到目标服务器的backup_path
目录(如/var/opt/gitlab/backups/
),然后运行恢复命令:
sudo gitlab-rake gitlab:backup:restore BACKUP=TIMESTAMP # 替换为备份文件的时间戳(如1710000000)
yes
并按回车键继续。/etc/gitlab
目录(参考备份步骤中的“打包配置文件”),需将其解压并重新配置:sudo tar -xf gitlab_conf.tar -C / # 解压到根目录
sudo gitlab-ctl reconfigure # 重新加载配置
sudo gitlab-ctl start # 启动所有服务
sudo gitlab-ctl status
git clone http://your-gitlab-server.com/username/repository.git
四、注意事项