Gitlab|安裝-遷移-刪除

GitLab 由烏克蘭程序員 Dmitriy Zaporozhets 和 Valery Sizov 開(kāi)發(fā),它由 Ruby 寫(xiě)成。后來(lái),一些部分用 Go 語(yǔ)言重寫(xiě)。截止 2018 年 5 月,該公司約有 290 名團(tuán)隊(duì)成員,以及 2000 多名開(kāi)源貢獻(xiàn)者。 GitLab 被 IBM,Sony,Jülich Research Center,NASA,Alibaba,Invincea,O’Reilly Media,Leibniz-Rechenzentrum (LRZ),CERN,SpaceX 等組織使用。

1 安裝

1.1 Omnibus package installation

這是Gitlab官網(wǎng)推薦的安裝方式。官網(wǎng)文檔鏈接位于Gitlab Installation。不過(guò),現(xiàn)在直接去官網(wǎng)默認(rèn)給出的是企業(yè)版,即gitlab-ee的安裝方式(付費(fèi)的),而個(gè)人版其實(shí)用gitlab-ce就夠了。gitlab-ce安裝方式如下

1.1.1 安裝并配置依賴

sudo apt-get install -y curl openssh-server ca-certificates 

然后安裝Postfix來(lái)啟動(dòng)郵件提醒功能。(如果你使用了第三方的郵件服務(wù),可以跳過(guò)這一步并且參照配置外部SMTP服務(wù)器)。

sudo apt-get install -y postfix

在接下來(lái)的配置過(guò)程中,選擇'Internet Site'選項(xiàng)。使用你的服務(wù)器的域名來(lái)作為'mail name'。如果還有后續(xù)的選項(xiàng),輸入Enter直至安裝完成。

1.1.2 安裝Gitlab-EE

添加Gitlab Package倉(cāng)庫(kù):

curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash 

注意這里安裝的是CE版本,故是gitlab-ce,企業(yè)版對(duì)應(yīng)的是gitlab-ee

接下來(lái)安裝Gitlab:

sudo EXTERNAL_URL="http://gitlab.example.com" apt-get install gitlab-ce 

這里的EXTERNAL_URL是你的Gitlab服務(wù)要使用的域名。如果你只使用http,或者后續(xù)要使用已有的Nginx,可以在這里使用http。如果使用https,gitlab會(huì)調(diào)用Let's encrtpy的服務(wù)為你的網(wǎng)站添加ssl證書(shū)。

1.1.3 登錄Gtilab

進(jìn)入你在安裝階段的域名,你會(huì)被重定向到密碼重置界面。在這個(gè)頁(yè)面你要設(shè)置管理員賬戶的密碼,然后回到登錄界面。在這個(gè)登錄界面,使用root用戶名和上一步設(shè)置的密碼登錄。

1.2 使用已有的Nginx

這個(gè)章節(jié)我們參考官方文檔給出使用已有的Nginx的方法。

1.2.1 禁用Gitlab自帶的Nginx

編輯/etc/gitlab/gitlab.rb文件,設(shè)置

nginx['enable'] = false

1.2.2 設(shè)置外部服務(wù)器的用戶

這一步是為了保證外部服務(wù)器用戶能夠訪問(wèn)gitlab。使用Nginx時(shí),可以通過(guò)/etc/nginx/nginx.conf文件查看到nginx用戶。一般情況下這個(gè)用戶名是www-data。修改/etc/gitlab/gitlab.rb

web_server['external_users'] = ['www-data']

然后使用sudo gitlab-ctl reconfigure來(lái)使得更改生效。

1.2.3 Trusted proxies

如果你的反向代理服務(wù)器和gitlab不是在同一臺(tái)機(jī)器上,那么你還需要設(shè)置Trusted proxies。

gitlab_rails['trusted_proxies'] = ['192.168.1.0/24', '192.168.2.1', '2001:0db8::/32']

1.2.4 Nginx示例配置文件

# gitlab socket 文件地址
upstream gitlab {
  # 7.x 版本在此位置
  # server unix:/var/opt/gitlab/gitlab-rails/tmp/sockets/gitlab.socket;
  # 8.0 位置
  server unix:/var/opt/gitlab/gitlab-rails/sockets/gitlab.socket;
}

server {
  listen *:80;

  server_name gitlab.example.com;   # 請(qǐng)修改為你的域名

  server_tokens off;     # don't show the version number, a security best practice
  root /opt/gitlab/embedded/service/gitlab-rails/public;

  # Increase this if you want to upload large attachments
  # Or if you want to accept large git objects over http
  client_max_body_size 250m;

  # individual nginx logs for this gitlab vhost
  access_log  /var/log/gitlab/nginx/gitlab_access.log;
  error_log   /var/log/gitlab/nginx/gitlab_error.log;

  location / {
    # serve static files from defined root folder;.
    # @gitlab is a named location for the upstream fallback, see below
    try_files $uri $uri/index.html $uri.html @gitlab;
  }

  # if a file, which is not found in the root folder is requested,
  # then the proxy pass the request to the upsteam (gitlab unicorn)
  location @gitlab {
    # If you use https make sure you disable gzip compression
    # to be safe against BREACH attack

    proxy_read_timeout 300; # Some requests take more than 30 seconds.
    proxy_connect_timeout 300; # Some requests take more than 30 seconds.
    proxy_redirect     off;

    proxy_set_header   X-Forwarded-Proto https;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;
    proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header   X-Frame-Options   SAMEORIGIN;

    proxy_pass http://gitlab;
  }

  # Enable gzip compression as per rails guide: http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
  # WARNING: If you are using relative urls do remove the block below
  # See config/application.rb under "Relative url support" for the list of
  # other files that need to be changed for relative url support
  location ~ ^/(assets)/  {
    root /opt/gitlab/embedded/service/gitlab-rails/public;
    # gzip_static on; # to serve pre-gzipped version
    expires max;
    add_header Cache-Control public;
  }

  # error_page 502 /502.html;
}

2 遷移

2.1 備份

遷移首先要做的是備份。在git學(xué)習(xí)------> Gitlab如何進(jìn)行備份恢復(fù)與遷移?這篇文章中詳細(xì)講述了備份的問(wèn)題。我們這里介紹的是最為直接和簡(jiǎn)單的步驟。?如果要更加詳細(xì)的信息請(qǐng)閱讀這篇參考。

備份使用如下命令:

gitlab-rake gitlab:backup:create

備份會(huì)生成在/var/opt/gitlab/backups目錄下。名稱類似于1502357536_2017_08_10_9.4.3_gitlab_backup.tar。下面這些配置信息,沒(méi)有包含在backup文件里面。需要手動(dòng)遷移。

  • /etc/gitlab/gitlab.rb 配置文件須備份
  • /var/opt/gitlab/nginx/conf nginx配置文件
  • /etc/postfix/main.cfpostfix 郵件配置備份
備份命令的執(zhí)行

2.2 在目標(biāo)機(jī)器上安裝gitlab

遷移過(guò)程中要求源機(jī)器和目標(biāo)機(jī)器上安裝的gitlab版本是相同的。如果不同,其實(shí)最好的?做法是先將源機(jī)器上的gitlab升級(jí)到最新的版本。然后再生成備份。

如何查看Gitlab版本

2.3 上傳備份

使用scp命令將備份文件上傳到目標(biāo)機(jī)器的/var/opt/gitlab/backups。

如果scp上傳目標(biāo)文件文件夾的權(quán)限不夠,可以先上傳到自己的home目錄下,然后ssh登錄到服務(wù)器使用sudo進(jìn)行移動(dòng)。

2.4 應(yīng)用備份文件

首先為了避免潛在的權(quán)限問(wèn)題,將備份文件的權(quán)限設(shè)置為777

chmod 777 1502357536_2017_08_10_9.4.3_gitlab_backup.tar 

然后停止gitlab的相關(guān)數(shù)據(jù)連接服務(wù)

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq

然后用下面的命令讀取備份:

gitlab-rake gitlab:backup:restore BACKUP=1502357536_2017_08_10_9.4.3

在后續(xù)出現(xiàn)的所有詢問(wèn)中輸入yes,等待執(zhí)行完畢,即完成了遷移過(guò)程,接下來(lái)再次啟動(dòng)?gitlab

sudo gitlab-ctl start

3 刪除

下面的刪除過(guò)程在Ubuntu 16上得到驗(yàn)證:

3.1 移除gitlab服務(wù)

sudo gitlab-ctl uninstall

3.2 清楚Gitlab產(chǎn)生的數(shù)據(jù)

sudo gitlab-ctl cleanse

3.3 刪除Gitlab生成的系統(tǒng)?賬戶

sudo gitlab-ctl remove-accounts

3.4 刪除gitlab

sudo dpkg -P gitlab-ce

3.5 其他文件的刪除

?除了上述操作,Gitlab使用的其他文件夾還需要手動(dòng)刪除,包括:

  • /opt/gitlab: 包含了Gitlab的應(yīng)用代碼和依賴
  • /var/opt/gitlab: 包含了應(yīng)用的數(shù)據(jù)和配置信息(gitlab-ctl reconfigure的寫(xiě)入內(nèi)容)
  • /etc/gitlab: omnibus gitlab的配置信息。這里的文件是唯一允許你手動(dòng)編輯的部分
  • /var/log/gitlab: 日志文件

在你完成了開(kāi)始的四個(gè)步驟后,這里的四個(gè)文件夾可以安全地手動(dòng)刪除。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容