Gitlab備份與恢復、遷移與升級


 

0.Gitlab安裝

1.安裝和配置必要的依賴關系 
在CentOS7,下面的命令將在系統防火牆打開HTTP和SSH訪問。

 
  1. yum install curl openssh-server postfix
  2. systemctl enable sshd postfix
  3. systemctl start sshd postfix
  4. firewall-cmd --permanent --add-service=http
  5. systemctl reload firewalld

https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/

 

版本:Gitlab Community Edition

 

注意: gitlab-ce 鏡像僅支持 x86-64 架構

Debian/Ubuntu 用戶

首先信任 GitLab 的 GPG 公鑰:

curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/null

再選擇你的 Debian/Ubuntu 版本,文本框中內容寫進 /etc/apt/sources.list.d/gitlab-ce.list

 

 

deb http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/debian stretch main

安裝 gitlab-ce:

sudo apt-get update
sudo apt-get install gitlab-ce

RHEL/CentOS 用戶

新建 /etc/yum.repos.d/gitlab-ce.repo,內容為

[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

再執行

sudo yum makecache
sudo yum install gitlab-ce -y

安裝后后,修改

/etc/gitlab/gitlab.rb 文件中external_url為你的域名,然后執行配置

gitlab-ctl reconfigure

 

啟動腳本

cat /etc/systemd/system/gitlab.service
[Unit]
Description=gitlab
 
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/gitlab-ctl start
ExecStop=/bin/gitlab-ctl stop
 
[Install]
WantedBy=multi-user.target

 

cat /usr/lib/systemd/system/gitlab-runsvdir.service

[Unit]
Description=GitLab Runit supervision process
After=basic.target
 
[Service]
ExecStart=/opt/gitlab/embedded/bin/runsvdir-start
Restart=always
 
[Install]
WantedBy=basic.target
 

3.配置並啟動

 
  1. gitlab-ctl reconfigure
  2. gitlab-ctl status
  3. gitlab-ctl stop
  4. gitlab-ctl start

4.瀏覽到主機名和登錄Browse to the hostname and login

 
  1. Username: root
  2. Password: 5iveL!fe

5.更多操作系統的安裝方式,點擊下方鏈接即可 
CentOS6 
CentOS7 
Ubuntu14 
Ubuntu12

 

1.Gitlab備份

使用Gitlab一鍵安裝包安裝Gitlab非常簡單, 同樣的備份恢復與遷移也非常簡單. 使用一條命令即可創建完整的Gitlab備份:

 
  1. gitlab-rake gitlab:backup:create

使用以上命令會在/var/opt/gitlab/backups目錄下創建一個名稱類似為1481598919_gitlab_backup.tar的壓縮包, 這個壓縮包就是Gitlab整個的完整部分, 其中開頭的1481598919是備份創建的日期 
/etc/gitlab/gitlab.rb 配置文件須備份 
/var/opt/gitlab/nginx/conf nginx配置文件 
/etc/postfix/main.cfpostfix 郵件配置備份

 

1.1Gitlab備份目錄

可以通過/etc/gitlab/gitlab.rb配置文件來修改默認存放備份文件的目錄

 
  1. gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"

/var/opt/gitlab/backups修改為你想存放備份的目錄即可, 修改完成之后使用gitlab-ctl reconfigure命令重載配置文件即可.

 

1.2Gitlab自動備份

實現每天凌晨2點進行一次自動備份:通過crontab使用備份命令實現

 
  1. 0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create
 

2.Gitlab恢復

Gitlab的從備份恢復也非常簡單:

 
  1. # 停止相關數據連接服務
  2. gitlab-ctl stop unicorn
  3. gitlab-ctl stop sidekiq
  4. # 從1481598919編號備份中恢復
  5. gitlab-rake gitlab:backup:restore BACKUP=1481598919
  6. # 啟動Gitlab
  7. sudo gitlab-ctl start
 

3.gitlab遷移

要求:新服務器的gitlab版本與舊的服務器相同。

遷移如同備份與恢復的步驟一樣, 只需要將老服務器/var/opt/gitlab/backups目錄下的備份文件拷貝到新服務器上的/var/opt/gitlab/backups即可(如果你沒修改過默認備份目錄的話). 
但是需要注意的是新服務器上的Gitlab的版本必須與創建備份時的Gitlab版本號相同. 比如新服務器安裝的是最新的7.60版本的Gitlab, 那么遷移之前, 最好將老服務器的Gitlab 升級為7.60在進行備份.

/etc/gitlab/gitlab.rb gitlab配置文件須遷移,遷移后需要調整數據存放目錄 
/var/opt/gitlab/nginx/conf nginx配置文件目錄須遷移

/etc/gitlab/gitlab-secrets.json # 復制新服務器相同的目錄下
/etc/ssh/*key*  # 復制到新服務器相同目錄下,解決ssh key認證不成功問題
  1. [root@linux-node1 ~]# gitlab-ctl stop unicorn
  2. ok: down: unicorn: 0s, normally up
  3. [root@linux-node1 ~]# gitlab-ctl stop sidekiq
  4. ok: down: sidekiq: 0s, normally up
  5. [root@linux-node1 ~]# chmod 777 /var/opt/gitlab/backups/1481598919_gitlab_backup.tar  # 或 chown git:git /var/opt/gitlab/backups/1481598919_gitlab_backup.tar
  6. [root@linux-node1 ~]# gitlab-rake gitlab:backup:restore BACKUP=1481598919
 

4.gitlab升級

1.關閉gitlab服務

 
  1. gitlab-ctl stop unicorn
  2. gitlab-ctl stop sidekiq
  3. gitlab-ctl stop nginx

2.備份gitlab

 
  1. gitlab-rake gitlab:backup:create

3.下載gitlab的RPM包並進行升級

 
  1. curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
  2. yum update gitlab-ce
  3. 或者直接安裝高版本
  4. yum install gitlab-ce-8.12.13-ce.0.el7.x86_64
  5. 或者上官網下載最新版本 gitlab對應軟件包 [gitlab官網](https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-8.12.13-ce.0.el7.x86_64.rpm)
  6. 使用 rpm -Uvh gitlab-ce-8.12.13-ce.0.el7.x86_64
  7. 報錯.
  8. Error executing action `run` on resource 'ruby_block[directory resource: /var/opt/gitlab/git-data/repositories]'
  9. 解決方法:
  10. sudo chmod 2770 /var/opt/gitlab/git-data/repositories

4.啟動並查看gitlab版本信息

 
  1. gitlab-ctl reconfigure
  2. gitlab-ctl restart
  3. # head -1 /opt/gitlab/version-manifest.txt
  4. gitlab-ce 8.7.3
 

5.gitlab更改默認Nginx

更換gitlab自帶Nginx,使用自行編譯Nginx來管理gitlab服務。

編輯gitlab配置文件禁用自帶Nignx服務器

 
  1. vi /etc/gitlab/gitlab.rb
  2. ...
  3. #設置nginx為false,關閉自帶Nginx
  4. nginx['enable'] = false
  5. ...

檢查默認nginx配置文件,並遷移至新Nginx服務

 
  1. /var/opt/gitlab/nginx/conf/nginx.conf #nginx配置文件,包含gitlab-http.conf文件
  2. /var/opt/gitlab/nginx/conf/gitlab-http.conf #gitlab核心nginx配置文件

重啟 nginx、gitlab服務

 
  1. $ sudo gitlab-ctl reconfigure
  2. $ sudo service nginx restart

訪問報502。原因是nginx用戶無法訪問gitlab用戶的socket文件。 重啟gitlab需要重新授權

 
    1. chmod -R o+x /var/opt/gitlab/gitlab-rails


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM