GitLab 備份、恢復、升級
gitlab放着企業的源碼,源碼很重要 gitlab備份非常重要
對 gitlab 進行備份將會創建一個包含所有庫和附件的歸檔文件。
對備份的恢復只能恢復到與備份時的 gitlab 相同的版本。
將 gitlab 遷移到另一台服務器上的最佳方法就是通過備份和還原。
gitlab 提供了一個簡單的命令行來備份整個 gitlab,並且能靈活的滿足需求
備份配置
備 份 文 件 將 保 存 在 配 置 文 件 中 定 義 的 backup_path 中 , 文 件 名 為
TIMESTAMP_gitlab_backup.tar,TIMESTAMP 為備份時的時間戳。TIMESTAMP 的格式為:
EPOCH_YYYY_MM_DD_Gitlab-version。
默認的備份文件目錄為:/var/opt/gitlab/backups,如果自定義備份目錄需要賦予目錄 git 權限,具體操作如下:
配置文件/etc/gitlab/gitlab.rb
配置文件中加入
路徑
gitlab_rails['backup_path'] = '/data/backup/gitlab'
gitlab_rails['backup_keep_time'] = 604800 #備份保留的時間(以秒為單位, 這個是七天默認值)
創建備份目錄
[root@ci-node1 git_test]# mkdir -p /data/backup/gitlab
vim /etc/gitlab/gitlab.rb
加到最后
保存 wq退出
改完配置執行gitlab-ctl reconfigure
[root@ci-node1 git_test]# gitlab-ctl reconfigure
如果自定義備份目錄需要賦予目錄 git 權限
chown -R git.git /data/backup/gitlab
執行完gitlab-ctl reconfigure 他會自動幫助我們創建好備份目錄的
[root@ci-node1 git_test]# ll /data/backup/gitlab/
權限也不用改 創建的備份目錄 和默認備份目錄權限一樣
[root@ci-node1 git_test]# ll /data/backup/ total 0 drwx------ 2 git root 6 Apr 5 22:26 gitlab
[root@ci-node1 git_test]# ls -dl /var/opt/gitlab/backups/ drwx------ 2 git root 6 Mar 22 23:25 /var/opt/gitlab/backups/
手動備份
在命令執行:/usr/bin/gitlab-rake gitlab:backup:create 生成一次備份
[root@ci-node1 git_test]# /usr/bin/gitlab-rake gitlab:backup:create Dumping database ... Dumping PostgreSQL database gitlabhq_production ... [DONE] done Dumping repositories ... * test/git_test ... [DONE] * test/git_test.wiki ... [SKIPPED] done Dumping uploads ... done Dumping builds ... done Dumping artifacts ... done Dumping pages ... done Dumping lfs objects ... done Dumping container registry images ... [DISABLED] Creating backup archive: 1586097870_2020_04_05_10.2.2_gitlab_backup.tar ... done Uploading backup archive to remote storage ... skipped Deleting tmp directories ... done done done done done done done done Deleting old backups ... done. (0 removed)
[root@ci-node1 git_test]# ll /data/backup/gitlab/ total 72 -rw------- 1 git git 71680 Apr 5 22:44 1586097870_2020_04_05_10.2.2_gitlab_backup.tar
時間搓 年月日 gitlab版本號
備份包括所有庫,文檔 打包成一個壓縮包
我們看到在設定的目錄中生成了對應的備份文件
定時備份
定時任務crontab中加入
通過在定時任務里添加:
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
我們來實現定時備份,由於代碼是一個企業非常重要的資產,所以我們要重視 GitLab的備份工作。
至少做到每天備份一次,平時要注意檢查備份的完整性。 至少每天一次全量的備份
環境變量 CRON=1 的作用是如果沒有任何錯誤發生時, 抑制備份腳本的所有進度輸出
備份不用去刪除,默認保留7天 配置文件里面可以修改備份保留的時間
gitlab恢復實踐
在gitlab 試下刪庫,然后用備份恢復
進入倉庫
點擊
輸入倉庫名稱
刪除了
GitLab 的恢復只能還原到與備份文件相同的 gitlab 版本的系統中,恢復時,停止連接
到數據庫的進程(也就是停止數據寫入服務),但是保持 GitLab 是運行的
停止數據寫入服務
如果不停止數據寫入,會有變恢復 邊寫數據 會亂
停止數據寫入 執行這兩條命令 停兩個服務
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
[root@ci-node1 git_test]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@ci-node1 git_test]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
確認一下
[root@ci-node1 git_test]# gitlab-ctl status run: gitaly: (pid 6501) 501642s; run: log: (pid 6494) 501642s run: gitlab-monitor: (pid 6492) 501642s; run: log: (pid 6487) 501642s run: gitlab-workhorse: (pid 6491) 501642s; run: log: (pid 6486) 501642s run: logrotate: (pid 109050) 423s; run: log: (pid 6479) 501642s run: nginx: (pid 6503) 501642s; run: log: (pid 6496) 501642s run: node-exporter: (pid 6482) 501642s; run: log: (pid 6481) 501642s run: postgres-exporter: (pid 6504) 501642s; run: log: (pid 6497) 501642s run: postgresql: (pid 6490) 501642s; run: log: (pid 6485) 501642s run: prometheus: (pid 6499) 501642s; run: log: (pid 6498) 501642s run: redis: (pid 6489) 501642s; run: log: (pid 6484) 501642s run: redis-exporter: (pid 6502) 501642s; run: log: (pid 6495) 501642s down: sidekiq: 46s, normally up; run: log: (pid 6493) 501642s down: unicorn: 115s, normally up; run: log: (pid 6483) 501642s
接下來執行 gitlab 恢復操作:
gitlab-rake gitlab:backup:restore BACKUP=備份文件名
文件名只要數字部分就可以了
[root@ci-node1 git_test]# gitlab-rake gitlab:backup:restore BACKUP=1586097870_2020_04_05_10.2.2 Unpacking backup ... done Before restoring the database, we will remove all existing tables to avoid future upgrade problems. Be aware that if you have custom tables in the GitLab database these tables and all data will be removed. Do you want to continue (yes/no)?
一直按yes
整個恢復執行過程中,我們可以看到基本是在刪除表,創建表。
完成后把gitlab 服務重啟
gitlab-ctl restart
[root@ci-node1 git_test]# gitlab-ctl restart ok: run: gitaly: (pid 109574) 0s ok: run: gitlab-monitor: (pid 109590) 1s ok: run: gitlab-workhorse: (pid 109593) 0s ok: run: logrotate: (pid 109636) 0s ok: run: nginx: (pid 109642) 1s ok: run: node-exporter: (pid 109647) 0s ok: run: postgres-exporter: (pid 109651) 1s ok: run: postgresql: (pid 109658) 0s ok: run: prometheus: (pid 109666) 1s ok: run: redis: (pid 109673) 0s ok: run: redis-exporter: (pid 109677) 0s ok: run: sidekiq: (pid 109693) 1s ok: run: unicorn: (pid 109700) 0s
確認下 兩個服務啟動了
[root@ci-node1 git_test]# gitlab-ctl status run: gitaly: (pid 109574) 28s; run: log: (pid 6494) 502050s run: gitlab-monitor: (pid 109590) 26s; run: log: (pid 6487) 502050s run: gitlab-workhorse: (pid 109593) 25s; run: log: (pid 6486) 502050s run: logrotate: (pid 109636) 25s; run: log: (pid 6479) 502050s run: nginx: (pid 109642) 25s; run: log: (pid 6496) 502050s run: node-exporter: (pid 109647) 24s; run: log: (pid 6481) 502050s run: postgres-exporter: (pid 109651) 24s; run: log: (pid 6497) 502050s run: postgresql: (pid 109658) 23s; run: log: (pid 6485) 502050s run: prometheus: (pid 109666) 23s; run: log: (pid 6498) 502050s run: redis: (pid 109673) 22s; run: log: (pid 6484) 502050s run: redis-exporter: (pid 109677) 22s; run: log: (pid 6495) 502050s run: sidekiq: (pid 109693) 22s; run: log: (pid 6493) 502050s run: unicorn: (pid 109700) 21s; run: log: (pid 6483) 502050s
再看看gitlab頁面 看看倉庫回來了嗎
恢復完成
升級
首先,下載新版本的 RPM 包,可以通過官網或者清華鏡像站獲取。
其次關閉部分 gitlab 服務
關閉數據寫入服務 關閉nginx 這三個服務
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-ctl stop nginx
執行升級操作
rpm -Uvh gitlab-ce-10.0.4-ce.0.el7.x86_64.rpm
完成后重新配置 gitlab
gitlab-ctl reconfigure
重啟 gitlab 服務
gitlab-ctl restart
注:升級操作不建議進行。 有風險
如果確實需要,也可以采取在一台新的服務器上安裝新版本的 Gitlab,然后采用導入庫的方式將舊系統的代碼倉庫導入到新 Gitlab 上。
如果不行 可以撤回來