一、備份前的準備工作
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
四、注意事項