0%

Gitlab安装与数据备份和恢复

安装社区版

查看GitLab官方安装文档,得到如下内容:

1
2
3
4
5
6
sudo yum install -y curl policycoreutils-python openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo systemctl reload firewalld
1
2
3
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
1
2
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce.12.6.2/script.rpm.sh | sudo bash
1
sudo EXTERNAL_URL="https://gitlab.example.com" yum install -y gitlab-ce

目录

1
2
3
4
主目录:/opt/gitlab
配置文件地址:/etc/gitlab/gitlab.rb
各个组件地址:/var/opt/gitlab
日志地址:/var/log/gitlab

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#启动状态
gitlab-ctl status
#重新加载配置
sudo gitlab-ctl reconfigure
#重新启动
gitlab-ctl restart
#启动
gitlab-ctl start
#停止
gitlab-ctl stop
#停止某个组件
gitlab-ctl stop unicorn
#各组件日志查看
gitlab-ctl tail nginx

备份准备

两台服务器gitlab版本必须一致,本例均为12.7.8

Gitlab创建备份

1
2
3
4
5
6
[root@localhost]# gitlab-rake gitlab:backup:create

备份完成最后提示:
Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data and are not included in this backup. You will need these files to restore a backup. Please back them up manually.Backup task is done.

红字部分表示 gitlab.rb 和 gitlab-secrets.json 两个文件包含敏感信息。未被备份到备份文件中。需要手动备份。

需手动备份的文件:

Ø /etc/gitlab/gitlab.rb 配置文件须备份

Ø /var/opt/gitlab/nginx/conf nginx 配置文件

Ø /etc/postfix/main.cfpostfix 邮件配置备份

Ø /etc/gitlab/gitlab-secrets.json 存储了gitlab的db secret信息

如果想修改备份路径,可在/etc/gitlab/gitlab.rb文件下编辑

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

定时备份

计划每天凌晨2点进行备份

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

备份文件恢复

修改文件权限

1
chmod 777 1629196316_2021_08_17_12.7.8_gitlab_backup.tar 

关闭相关服务

1
2
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq

执行恢复

gitlab-rake gitlab:backup:restore BACKUP=备份文件编号

1
gitlab-rake gitlab:backup:restore BACKUP=1629196316_2021_08_17_12.7.8

注意:

l 此处命令是文件编号,不是文件名称

l 交互界面会有两次让输入“yes”

开启服务

以下几个文件需手动恢复:

Ø /etc/gitlab/gitlab.rb 配置文件

Ø /var/opt/gitlab/nginx/conf nginx配置文件

Ø /etc/postfix/main.cfpostfix 邮件配置备份

Ø /etc/gitlab/gitlab-secrets.json 存储了gitlab的db secret信息

1
gitlab-ctl start

安装问题

gitlab-ctl reconfigure 出现 No such file or directory

1
2
/opt/gitlab/embedded/bin/gitlab-logrotate-wrapper: No such file or directory
/opt/gitlab/embedded/service/gitlab-rails/bin/sidekiq-cluster: No such file or directory

可以从其他机器拷贝一份,也可以通过来回修改unicorn端口号自动生成文件

恢复出现Backup::Error: Backup failed

1
2
3
4
5
6
7
8
9
10
11
2021-08-19 08:13:31 +0800 -- Restoring uploads ... 
rake aborted!
Backup::Error: Backup failed
/opt/gitlab/embedded/service/gitlab-rails/lib/backup/files.rb:81:in `run_pipeline!'
/opt/gitlab/embedded/service/gitlab-rails/lib/backup/files.rb:44:in `restore'
/opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/backup.rake:152:in `block (4 levels) in <top (required)>'
/opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/backup.rake:66:in `block (3 levels) in <top (required)>'
/opt/gitlab/embedded/bin/bundle:23:in `load'
/opt/gitlab/embedded/bin/bundle:23:in `<main>'
Tasks: TOP => gitlab:backup:uploads:restore
(See full trace by running task with --trace)