Gitlab 安裝、升級、備份、恢復、漢化等


一、Gitlab安裝

1. 基於yum方式安裝Gitlab

安裝步驟如下
(1)配置yum源

# vim /etc/yum.repos.d/gitlab-ce.repo

(2)復制如下內容並保存(注意baseurl的配置是centos7的地址,安裝版本一定要正確

[gitlab-ce]
name=gitlab-ce
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
Repo_gpgcheck=0
Enabled=1
Gpgkey=https://packages.gitlab.com/gpg.key

(3)更新本地yum緩存

# sudo yum makecache

(4)yum安裝gitlab,此處安裝版本是11.8.0

# sudo yum install gitlab-ce         #自動安裝最新版  

安裝遇到以上錯誤提示,需要增加一個nogpgcheck參數:

# sudo yum install gitlab-ce --nogpgcheck         #自動安裝最新版  

也可以安裝指定的版本(遷移數據需要原庫版本一致)

# sudo yum install gitlab-ce-11.8.0 --nogpgcheck      #安裝指定版本  
  1. 基於rpm進行安裝Gitlab
    (1)安裝依賴包、下載安裝gitlab rpm包
# yum install curl openssh-server postfix cronie 
# yum -y install policycoreutils-python   #必須安裝該依賴包

(2)下載安裝包

# wget --content-disposition https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-10.8.0-ce.0.el7.x86_64.rpm/download.rpm
# rpm -ivh gitlab-ce-10.8.0-ce.0.el7.x86_64.rpm

(3)對GitLab進行編譯

# gitlab-ctl reconfigure

官方安裝文檔鏈接:https://about.gitlab.com/installation/

二、Gitlab 配置

修改配置文件:

# mkdir -pv /oadata/git-data
# mkdir /oadata/gitlab/backup
# chown -R git.git /oadata/gitlab  # 備份目錄
# chown -R git.git /oadata/git-data  # Git數據保存目錄
# vim /etc/gitlab/gitlab.rb
external_url 'http://***'
gitlab_rails['time_zone'] = 'Asia/Shanghai'
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = '***'
gitlab_rails['gitlab_email_display_name'] = '***'
gitlab_rails['gitlab_email_reply_to'] = '***'
gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'
   main: # 'main' is the GitLab 'provider ID' of this LDAP server
     label: '***'
     host: '***'
     port: 389
     uid: 'uid'
     bind_dn: 'cn=root,***'
     password: '***'
     encryption: 'plain' # "start_tls" or "simple_tls" or "plain"
     verify_certificates: false
     active_directory: true
     allow_username_or_email_login: true
     lowercase_usernames: true
     block_auto_created_users: false
     base: 'ou=users,***'
     user_filter: '(memberOf=cn=gitlab,ou=groups,***)'
EOS
gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/oadata/gitlab/backups"
gitlab_rails['backup_archive_permissions'] = 0644
gitlab_rails['backup_keep_time'] = 129600
git_data_dirs({
   "default" => {
     "path" => "/oadata/git-data"
    }
})
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "***"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_user_name'] = "***"
gitlab_rails['smtp_password'] = "***"
gitlab_rails['smtp_domain'] = "***"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = false

再次進行編譯更新配置

# gitlab-ctl reconfigure

常用相關命令:

# gitlab-ctl stop   # 關閉服務
# gitlab-ctl start  # 開啟服務
# gitlab-ctl restart  # 重啟服務
# gitlab-ctl status  # 查看服務狀態
# gitlab-ctl tail  # 查看日志
# gitlab-rake gitlab:backup:create  # 創建備份
# gitlab-rake gitlab:backup:restore BACKUP=備份文件編號  #  恢復備份
# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION  # 查看版本信息

三、Gitlab漢化

(1)下載對應版本的漢化包

# wget https://gitlab.com/xhang/gitlab/-/archive/v11.8.0-zh/gitlab-v11.8.0-zh.tar

(2)更新語言包文件

# gitlab-ctl stop
# tar xvf gitlab-v11.8.0-zh.tar
# \cp -rf gitlab-v11.8.0-zh/* /opt/gitlab/embedded/service/gitlab-rails/
# chown -R 
# gitlab-ctl reconfigure
# gitlab-ctl restart

四、Gitlab 備份與恢復

(1)備份腳本

# vim /oadata/gitlab/backup/sh/gitlab_backup.sh
#!/bin/bash

now=`date +%Y%m%d`

echo "開始執行gitlab備份..."
/opt/gitlab/bin/gitlab-rake gitlab:backup:create
echo "備份完成."

echo "清理超過10天的備份數據..."
/usr/bin/find /oadata/gitlab/backups/*gitlab_backup.tar -mtime +10 -exec rm -f {} \;
echo "清理完成."

echo "開始備份配置文件: gitlab.rb , gitlab-secrets.json"
tar -cvf /oadata/gitlab/backups/config/${now}_gitlab_config_backup.tar /etc/gitlab/gitlab.rb /etc/gitlab/gitlab-secrets.json

echo "清理超過30天的配置備份數據..."
/usr/bin/find /oadata/gitlab/backups/config/*gitlab_config_backup.tar -mtime +30 -exec rm -f {} \;
echo "清理完成."
# chmod 755 /oadata/gitlab/backup/sh/gitlab_backup.sh

(2)備注定時備份

  • 每天2點50進行備份
50 02 * * * /oadata/gitlab/sh/gitlab_backup.sh

(3)Gitlab恢復

  • 恢復前,首先停止相關數據連接服務
# gitlab-ctl stop unicorn
# gitlab-ctl stop sidekiq
# gitlab-rake gitlab:backup:restore BACKUP=備份文件編號

更換機器后進行恢復,還需要手工恢復文件: /etc/gitlab/gitlab.rb /etc/gitlab/gitlab-secrets.json

五、Gitlab升級

  • 在升級前一定要做好備份,記錄自己當前gitlab-ca的版本號。
# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
# gitlab-rake gitlab:backup:create
  • 升級Gitlab(注意:由於升級不能跨越大版本號,因此只能升級到當前大版本號到最高版本,方可升級到下一個大版本號)
    關閉連接服務
# gitlab-ctl stop unicorn
# gitlab-ctl stop sidekiq

以下為將Gitlab 11.8.0 升級為 12.0.3. 依次執行下面指令逐步升級命令,在每一步安裝成功后如果發現界面500,不可訪問,那么執行 gitlab-ctl reconfigure 指令刷新配置文件。(一定保證數據可以正常訪問方可執行下一步升級指令)。

通過yum升級

# yum install gitlab-ce-11.11.5-ce.0.el7

測試各功能驗證無問題后,再次進行升級操作:

# yum install gitlab-ce-12.0.3-ce.0.el7

或通過rpm升級

# rpm -Uvh gitlab-ce-11.11.5-ce.0.el7.rpm

測試各功能驗證無問題后,再次進行升級操作:

rpm -Uvh gitlab-ce-11.11.5-ce.0.el7.rpm

六、訪問Gitlab postgresql

1. 通過控制台訪問數據庫

[root@localhost ~]# su - gitlab-psql     //登陸用戶
-sh-4.1$ psql -h /var/opt/gitlab/postgresql -d gitlabhq_production   連接到gitlabhq_production庫
psql (9.2.18)
Type "help" for help.
gitlabhq_production=#  \h    查看幫助命令
Available help:
  ABORT                            CREATE FUNCTION                  DROP TABLE
  ALTER AGGREGATE                  CREATE GROUP                     DROP TABLESPACE
  ALTER COLLATION                  CREATE INDEX                     DROP TEXT SEARCH CONFIGURATION
  ALTER CONVERSION                 CREATE LANGUAGE                  DROP TEXT SEARCH DICTIONARY
  ALTER DATABASE                   CREATE OPERATOR                  DROP TEXT SEARCH PARSER
  ALTER DEFAULT PRIVILEGES         CREATE OPERATOR CLASS            DROP TEXT SEARCH TEMPLATE
  ALTER DOMAIN                     CREATE OPERATOR FAMILY           DROP TRIGGER
  ALTER EXTENSION                  CREATE ROLE                      DROP TYPE
……………………………………………………………………………………………………………………
 
gitlabhq_production-# \l     //查看數據庫
                                             List of databases
        Name         |    Owner    | Encoding |   Collate   |    Ctype    |        Access privileges        
---------------------+-------------+----------+-------------+-------------+---------------------------------
 gitlabhq_production | gitlab      | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres            | gitlab-psql | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0           | gitlab-psql | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/"gitlab-psql"               +
                     |             |          |             |             | "gitlab-psql"=CTc/"gitlab-psql"
 template1           | gitlab-psql | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/"gitlab-psql"               +
                     |             |          |             |             | "gitlab-psql"=CTc/"gitlab-psql"
(4 rows)
 
gitlabhq_production-# \dt   //查看多表
                       List of relations
 Schema |                 Name                 | Type  | Owner  
--------+--------------------------------------+-------+--------
 public | abuse_reports                        | table | gitlab
 public | appearances                          | table | gitlab
 public | application_settings                 | table | gitlab
 public | audit_events                         | table | gitlab
 public | award_emoji                          | table | gitlab
 public | boards                               | table | gitlab
 public | broadcast_messages                   | table | gitlab
……………………………………………………………………………………………………………………
 
gitlabhq_production-# \d abuse_reports    //查看單表
                                      Table "public.abuse_reports"
    Column    |            Type             |                         Modifiers                          
--------------+-----------------------------+------------------------------------------------------------
 id           | integer                     | not null default nextval('abuse_reports_id_seq'::regclass)
 reporter_id  | integer                     | 
 user_id      | integer                     | 
 message      | text                        | 
 created_at   | timestamp without time zone | 
 updated_at   | timestamp without time zone | 
 message_html | text                        | 
Indexes:
    "abuse_reports_pkey" PRIMARY KEY, btree (id)
 
gitlabhq_production-# \di    //查看索引
                                                        List of relations
 Schema |                              Name                               | Type  | Owner  |                Table           
      
--------+-----------------------------------------------------------------+-------+--------+--------------------------------
------
 public | abuse_reports_pkey                                              | index | gitlab | abuse_reports
 public | appearances_pkey                                                | index | gitlab | appearances
 public | application_settings_pkey                                       | index | gitlab | application_settings
 public | audit_events_pkey                                               | index | gitlab | audit_events
 public | award_emoji_pkey                                                | index | gitlab | award_emoji
 public | boards_pkey                                                     | index | gitlab | boards
 public | broadcast_messages_pkey                                         | index | gitlab | broadcast_messages
 public | chat_names_pkey                                                 | index | gitlab | chat_names
 public | ci_application_settings_pkey                                    | index | gitlab | ci_application_settings
 public | ci_builds_pkey                                                  | index | gitlab | ci_builds
 public | ci_commits_pkey                                                 | index | gitlab | ci_commits
………………………………………………………………………………………………………………………………………………
 
gitlabhq_production=# SELECT spcname FROM pg_tablespace;  //查看所有表空間
  spcname   
------------
 pg_default
 pg_global
(2 rows)
 
gitlabhq_production-# \q    //退出psql
-sh-4.1$ exit                //退出登錄用戶
logout

2. 開啟臨時遠程無密碼訪問postgresql

(1)修改gitlab.rb

# vim /etc/gitlab/gitlab.rb

配置為:

postgresql['enable'] = true 
postgresql['listen_address'] = '0.0.0.0'
postgresql['port'] = 5432 
postgresql['data_dir'] = "/var/opt/gitlab/postgresql/data"
...
...
postgresql['custom_pg_hba_entries'] = {
  APPLICATION:[ { # APPLICATION should identify what the settings are used for
    type: "host",
    database: "all",
    user: "all",
    cidr: "0.0.0.0/0",
    method: "trust"
    }   
  ]
}
# gitlab-ctl reconfigure

等待報錯。沒辦法,上述修改,必然引發報錯。
(2)修改pg_hba.conf

vim /var/opt/gitlab/postgresql/data/pg_hba.conf

修改為:

host   all    all    0.0.0.0/0    trust

注意:從此,不能再執行gitlab-ctl reconfigure 命令了,因為如果再執行gitlab-ctl reconfigure ,那么pg_hba.conf的修改就會被還原。

# gitlab-ctl restart

七、常見問題及解決參考

  1. 連接gitlab postgresql
  2. 最全官方文檔 # 可以解決大多數你遇到的問題
  3. 數據恢復時遇到的一個問題


免責聲明!

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



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