1. 程式人生 > >Git系列七之備份遷移 升級 恢復管理

Git系列七之備份遷移 升級 恢復管理

nss 安裝git host load centos -perm 文件拷貝 node chm

0.Gitlab安裝

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

yum install curl openssh-server postfix
systemctl enable sshd postfix
systemctl start sshd postfix
firewall-cmd --permanent --add-service=http
systemctl reload firewalld

2.添加gitlab包服務器安裝包

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

3.配置並啟動

gitlab-ctl reconfigure
gitlab-ctl status
gitlab-ctl stop
gitlab-ctl start

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

Username: root 
Password: 5iveL!fe

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

1.Gitlab備份

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

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配置文件來修改默認存放備份文件的目錄

gitlab_rails[‘backup_path‘] = "/var/opt/gitlab/backups"

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

1.2Gitlab自動備份

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

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

2.Gitlab恢復

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

# 停止相關數據連接服務
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq

# 從1481598919編號備份中恢復
gitlab-rake gitlab:backup:restore BACKUP=1481598919

# 啟動Gitlab
sudo gitlab-ctl start

3.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配置文件目錄須遷移

[[email protected] ~]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[[email protected] ~]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
[[email protected] ~]# chmod 777 /var/opt/gitlab/backups/1481598919_gitlab_backup.tar
[[email protected] ~]# gitlab-rake gitlab:backup:restore BACKUP=1481598919

4.gitlab升級

1.關閉gitlab服務

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-ctl stop nginx

2.備份gitlab

gitlab-rake gitlab:backup:create

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

curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
yum update gitlab-ce
或者直接安裝高版本
yum install gitlab-ce-8.12.13-ce.0.el7.x86_64

或者上官網下載最新版本 gitlab對應軟件包  [gitlab官網](https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-8.12.13-ce.0.el7.x86_64.rpm)
使用 rpm -Uvh gitlab-ce-8.12.13-ce.0.el7.x86_64


報錯.
Error executing action `run` on resource ‘ruby_block[directory resource: /var/opt/gitlab/git-data/repositories]‘

解決方法:
sudo chmod 2770 /var/opt/gitlab/git-data/repositories

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

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

5.gitlab更改默認Nginx

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

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

vi /etc/gitlab/gitlab.rb
...
#設置nginx為false,關閉自帶Nginx
nginx[‘enable‘] = false
...

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

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

重啟 nginx、gitlab服務

$ sudo gitlab-ctl reconfigure
$ sudo service nginx restart

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

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

Git系列七之備份遷移 升級 恢復管理