首页 程序开发centos使用yum安装gitlab以及备份恢复命令

centos使用yum安装gitlab以及备份恢复命令

发布于: 2023-07-05 09:38:00
字数: 3848
评论: 0
阅读: 629

一、安装

本次安装的是gitlab社区版gitlab-ce,系统用的是centos7.9, 以下是安装步骤:

  1. 安装gitlab所需依赖
yum install policycoreutils openssh-server openssh-clients postfix
systemctl enable sshd
systemctl start sshd
systemctl enable postfix
systemctl start postfix
  1. 使用wget下载gitlab-ce安装包
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-16.1.1-ce.0.el7.x86_64.rpm

清华源镜像gitlab的rpm包下载地址: https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

  1. 安装rpm包
# 安装rpm包
rpm -ivh gitlab-ce-16.1.1-ce.0.el7.x86_64.rpm

二、升级

gitlab的升级需要按版本顺序升级,不可跨版本升级。升级路径可参考官方文档:
https://docs.gitlab.com/ee/update/index.html#upgrade-paths

也可以使用Upgrade Path这个小工具来获取升级版本路径:
https://gitlab-com.gitlab.io/support/toolbox/upgrade-path/

可以直接安装新版本完成升级(需按版本路径升级)

yum install gitlab-ce-16.1.1

三、常用命令

1. 手动备份
使用命令会在/var/opt/gitlab/backups目录下创建一个压缩包
这个压缩包就是Gitlab整个的完整部分。

gitlab-rake gitlab:backup:create

扩展知识
/etc/gitlab/gitlab.rb 配置文件须备份
/var/opt/gitlab/nginx/conf nginx配置文件
/etc/postfix/main.cf postfix邮件配置备份

2、更改Gitlab备份目录
gitlab默认的备份目录可能分区比较小,导致分区空间占满,此时我们可以通过修改/etc/gitlab/gitlab.rb配置文件来修改默认存放备份文件的目录

gitlab_rails['backup_path'] = "/home/gitlab-backup"

指定备份后数据存放的路径、权限、时间配置

gitlab_rails['manage_backup_path'] = true // 开启备份功能
gitlab_rails['backup_path'] = "/home/gitlab-backup"// 指定备份的路径
gitlab_rails'['backup_archive_permissions'] = 0644 // 备份文件的权限
gitlab_rails['backup_keep_time'] = 7776000 // 备份保留时间

3、 重新加载配置文件

gitlab-ctl reconfigure

4、crontab定时任务备份
注意:环境变量CRON=1的作用是如果没有任何错误发生时, 抑制备份脚本的所有进度输出

# 实现每天凌晨2点进行一次自动备份
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1

5、恢复备份
注意版本需要和备份文件的版本一致
在开始执行恢复之前需要先对PostgreSQL做一些配置,否则在执行恢复的时候可能会出现类似下面的异常报错。

Restoring PostgreSQL database gitlabhq_production ... ERROR: must be owner of extension pg_trgm
ERROR: must be owner of extension btree_gist
ERROR: must be owner of extension btree_gist
ERROR: must be owner of extension pg_trgm

  1. 修改/var/opt/gitlab/postgresql/data/postgresql.conf,找到属性listen_addresses,修改为:
listen_addresses = "*"
  1. 修改/var/opt/gitlab/postgresql/data/pg_hba.conf,在文件最后添加内容:
local   all         all                               trust
host    all         all                               127.0.0.1/32 trust
  1. 重启服务
gitlab-ctl restart
  1. 将gitlab设置为超级用户
# su - gitlab-psql
$ /opt/gitlab/embedded/bin/psql -h 127.0.0.1 gitlabhq_production
psql (13.6)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

gitlabhq_production=# ALTER USER gitlab WITH SUPERUSER;
ALTER ROLE
gitlabhq_production=# \q
$ exit
#

上面终端内容的大意大致如下:

  • su - gitlab-psql,切换成用户gitlab-psql。
  • /opt/gitlab/embedded/bin/psql -h 127.0.0.1 gitlabhq_production, 打开gitlabhq_production数据库。执行该命令后会打印相应信息并且提示符变成gitlabhq_production=#。
  • ALTER USER gitlab WITH SUPERUSER;,把用户gitlab修改为超级用户。
  • \q,退出SQL。
  • exit,退出gitlab-psql用户。

开始备份
把前面备份的tar文件拷贝到/var/opt/gitlab/backups/;以及把gitlab-secrets.json以及gitlab.rb拷贝到/etc/gitlab,并确保这些文件的权限。

mv /tmp/1673231242_2023_01_09_15.5.4_gitlab_backup.tar /var/opt/gitlab/backups/
chown git:git /var/opt/gitlab/backups/1673231242_2023_01_09_15.5.4_gitlab_backup.tar
chmod g-rwx,o-rwx /var/opt/gitlab/backups/1673231242_2023_01_09_15.5.4_gitlab_backup.tar

mv /tmp/gitlab.rb /tmp/gitlab-secrets.json /etc/gitlab/
chown root:root /etc/gitlab/gitlab-secrets.json /etc/gitlab/gitlab.rb
chmod g-rwx,o-rwx /etc/gitlab/gitlab-secrets.json /etc/gitlab/gitlab.rb

停掉unicorn和sidekiq。

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq

执行恢复

gitlab-backup restore force=yes
# 如果/var/opt/gitlab/backups/下有多个备份文件,可以执行下面命令指定恢复的目标
gitlab-backup restore force=yes BACKUP=1673231242_2023_01_09_15.5.4

重新生成配置文件并检查

gitlab-ctl reconfigure 
gitlab-ctl restart
gitlab-rake gitlab:check SANITIZE=true

自检出现报错这可能是因为重启Gitlab后,部分服务还没完全启动以至于自检出现报错,稍等一会再重新执行自检可能就会顺利通过。
数据恢复的流程至此结束。

四、参考链接

清华源
https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

csdn
https://blog.csdn.net/Marcelo59/article/details/129414334


扫描二维码,在手机上阅读