GitLab 簡易指引(三):備份與恢復

本文爲[原創]文章,轉載請標明出處。
原文連接: https://weyunx.com/2019/01/30...
原文出自 微雲的技術博客

GitLab 的備份工做主要包含配置文件備份和應用備份。git

配置文件備份

配置文件備份須要備份/etc/gitlab目錄。web

# 壓縮文件夾
sudo sh -c 'umask 0077; tar -cf $(date "+etc-gitlab-%s.tar") -C / etc/gitlab'

crontab中建立定時任務shell

sudo crontab -e -u root

新增一條:windows

# 天天1點執行
0 1 * * * umask 0077; tar cfz /secret/gitlab/backups/$(date "+etc-gitlab-\%s.tgz") -C / etc/gitlab

也能夠將語句寫成腳本,經過腳本執行,好比備份的共享目錄裏。bash

強烈建議配置文件備份目錄和應用備份目錄分開!服務器

應用備份

GitLab 的備份命令以下:ide

# 本文的操做步驟僅適用於 Omnibus 一鍵安裝方式安裝的 GitLab,下同。
sudo gitlab-rake gitlab:backup:create

# 樣例結果
Dumping database tables:
- Dumping table events... [DONE]
- Dumping table issues... [DONE]
- Dumping table keys... [DONE]
- Dumping table merge_requests... [DONE]
- Dumping table milestones... [DONE]
- Dumping table namespaces... [DONE]
- Dumping table notes... [DONE]
- Dumping table projects... [DONE]
- Dumping table protected_branches... [DONE]
- Dumping table schema_migrations... [DONE]
- Dumping table services... [DONE]
- Dumping table snippets... [DONE]
- Dumping table taggings... [DONE]
- Dumping table tags... [DONE]
- Dumping table users... [DONE]
- Dumping table users_projects... [DONE]
- Dumping table web_hooks... [DONE]
- Dumping table wikis... [DONE]
Dumping repositories:
- Dumping repository abcd... [DONE]
Creating backup archive: $TIMESTAMP_gitlab_backup.tar [DONE]
Deleting tmp directories...[DONE]
Deleting old backups... [SKIPPING]

執行後,備份的tar包放置在默認的備份目錄/var/opt/gitlab/backups 下。gitlab

一樣,咱們能夠編輯/etc/gitlab/gitlab.rb來修改默認的備份目錄。ui

# 默認的備份路徑
gitlab_rails['backup_path'] = '/mnt/backups'

一樣這裏咱們強烈建議雙機備份,官網提供了將備份上傳到雲以及上傳到 mount 共享文件夾下,這裏介紹一下上傳到共享文件夾下的配置。 spa

好比我在本地 windows 環境下建立了一個共享文件夾gitlab_backups ,而後將文件夾掛載到服務器上:

# 在根目錄下建立文件夾
mkdir gitlab_backups

# 掛載
mount -t cifs -o uid=996,gid=993,username=user,password=pass //22.189.30.101/gitlab_backups /gitlab_backups

其中uidgid是服務器上git用戶的uidgid,若是不加上極可能會報權限異常。userpass就是你本地的用戶名密碼,後面的 ip 和目錄就是本地的共享目錄。

掛載成功後修改配置:

# 自動將備份文件上傳
 gitlab_rails['backup_upload_connection'] = {
   :provider => 'Local',
   :local_root => '/gitlab_backups'
 }

 # 配置備份文件放至在掛載文件夾裏的子目錄名稱,若是備份文件直接放至在掛載目錄裏,使用 ‘.’ 
 gitlab_rails['backup_upload_remote_directory'] = '.'

修改完成後執行sudo gitlab-ctl reconfigure使配置生效,此時再執行備份命令則會自動將備份文件複製到掛載的共享目錄裏。

一樣,咱們能夠加到定時任務中:

# 天天2點執行
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create

備份文件清理

由於 GitLab 備份的文件較大,會佔用過多的存儲,咱們能夠定時的進行清理,編輯 gitlab.rb,找到:

###! The duration in seconds to keep backups before they are allowed to be deleted
gitlab_rails['backup_keep_time'] = 604800

默認爲保留7天,修改後執行 sudo gitlab-ctl reconfigure 便可。

GitLab 恢復

配置文件的恢復很簡單,直接將備份文件替換,而後執行sudo gitlab-ctl reconfigure便可。

下面說一下應用備份的恢復:

首先是確認工做:

  • You have installed the exact same version and type (CE/EE) of GitLab Omnibus with which the backup was created.
  • You have run sudo gitlab-ctl reconfigure at least once.
  • GitLab is running. If not, start it using sudo gitlab-ctl start.

開始恢復:

# 複製備份文件
sudo cp 11493107454_2018_04_25_10.6.4-ce_gitlab_backup.tar /var/opt/gitlab/backups/

# 權限變動
sudo chown git.git /var/opt/gitlab/backups/11493107454_2018_04_25_10.6.4-ce_gitlab_backup.tar

# 中止服務
sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq

# 確認服務中止
sudo gitlab-ctl status

# 恢復
sudo gitlab-rake gitlab:backup:restore BACKUP=1493107454_2018_04_25_10.6.4-ce

# 重啓和檢查
sudo gitlab-ctl restart
sudo gitlab-rake gitlab:check SANITIZE=true

(完)

參考

相關文章
相關標籤/搜索