Gitlab|安装-迁移-删除

GitLab 由乌克兰程序员 Dmitriy Zaporozhets 和 Valery Sizov 开发,它由 Ruby 写成。后来,一些部分用 Go 语言重写。截止 2018 年 5 月,该公司约有 290 名团队成员,以及 2000 多名开源贡献者。 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官网推荐的安装方式。官网文档链接位于Gitlab Installation。不过,现在直接去官网默认给出的是企业版,即gitlab-ee的安装方式(付费的),而个人版其实用gitlab-ce就够了。gitlab-ce安装方式如下

1.1.1 安装并配置依赖

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

然后安装Postfix来启动邮件提醒功能。(如果你使用了第三方的邮件服务,可以跳过这一步并且参照配置外部SMTP服务器)。

sudo apt-get install -y postfix

在接下来的配置过程中,选择'Internet Site'选项。使用你的服务器的域名来作为'mail name'。如果还有后续的选项,输入Enter直至安装完成。

1.1.2 安装Gitlab-EE

添加Gitlab Package仓库:

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

注意这里安装的是CE版本,故是gitlab-ce,企业版对应的是gitlab-ee

接下来安装Gitlab:

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

这里的EXTERNAL_URL是你的Gitlab服务要使用的域名。如果你只使用http,或者后续要使用已有的Nginx,可以在这里使用http。如果使用https,gitlab会调用Let's encrtpy的服务为你的网站添加ssl证书。

1.1.3 登录Gtilab

进入你在安装阶段的域名,你会被重定向到密码重置界面。在这个页面你要设置管理员账户的密码,然后回到登录界面。在这个登录界面,使用root用户名和上一步设置的密码登录。

1.2 使用已有的Nginx

这个章节我们参考官方文档给出使用已有的Nginx的方法。

1.2.1 禁用Gitlab自带的Nginx

编辑/etc/gitlab/gitlab.rb文件,设置

nginx['enable'] = false

1.2.2 设置外部服务器的用户

这一步是为了保证外部服务器用户能够访问gitlab。使用Nginx时,可以通过/etc/nginx/nginx.conf文件查看到nginx用户。一般情况下这个用户名是www-data。修改/etc/gitlab/gitlab.rb

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

然后使用sudo gitlab-ctl reconfigure来使得更改生效。

1.2.3 Trusted proxies

如果你的反向代理服务器和gitlab不是在同一台机器上,那么你还需要设置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;   # 请修改为你的域名

  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学习------> Gitlab如何进行备份恢复与迁移?这篇文章中详细讲述了备份的问题。我们这里介绍的是最为直接和简单的步骤。�如果要更加详细的信息请阅读这篇参考。

备份使用如下命令:

gitlab-rake gitlab:backup:create

备份会生成在/var/opt/gitlab/backups目录下。名称类似于1502357536_2017_08_10_9.4.3_gitlab_backup.tar。下面这些配置信息,没有包含在backup文件里面。需要手动迁移。

  • /etc/gitlab/gitlab.rb 配置文件须备份
  • /var/opt/gitlab/nginx/conf nginx配置文件
  • /etc/postfix/main.cfpostfix 邮件配置备份
备份命令的执行

2.2 在目标机器上安装gitlab

迁移过程中要求源机器和目标机器上安装的gitlab版本是相同的。如果不同,其实最好的�做法是先将源机器上的gitlab升级到最新的版本。然后再生成备份。

如何查看Gitlab版本

2.3 上传备份

使用scp命令将备份文件上传到目标机器的/var/opt/gitlab/backups

如果scp上传目标文件文件夹的权限不够,可以先上传到自己的home目录下,然后ssh登录到服务器使用sudo进行移动。

2.4 应用备份文件

首先为了避免潜在的权限问题,将备份文件的权限设置为777

chmod 777 1502357536_2017_08_10_9.4.3_gitlab_backup.tar 

然后停止gitlab的相关数据连接服务

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq

然后用下面的命令读取备份:

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

在后续出现的所有询问中输入yes,等待执行完毕,即完成了迁移过程,接下来再次启动�gitlab

sudo gitlab-ctl start

3 删除

下面的删除过程在Ubuntu 16上得到验证:

3.1 移除gitlab服务

sudo gitlab-ctl uninstall

3.2 清楚Gitlab产生的数据

sudo gitlab-ctl cleanse

3.3 删除Gitlab生成的系统�账户

sudo gitlab-ctl remove-accounts

3.4 删除gitlab

sudo dpkg -P gitlab-ce

3.5 其他文件的删除

�除了上述操作,Gitlab使用的其他文件夹还需要手动删除,包括:

  • /opt/gitlab: 包含了Gitlab的应用代码和依赖
  • /var/opt/gitlab: 包含了应用的数据和配置信息(gitlab-ctl reconfigure的写入内容)
  • /etc/gitlab: omnibus gitlab的配置信息。这里的文件是唯一允许你手动编辑的部分
  • /var/log/gitlab: 日志文件

在你完成了开始的四个步骤后,这里的四个文件夹可以安全地手动删除。

你可能感兴趣的:(Gitlab|安装-迁移-删除)