一、 备份之前
两台服务器gitlab版本必须一致,本例均为12.10.11
二、 Gitlab创建备份
#创建备份文件
[root@localhost backups]# gitlab-rake gitlab:backup:create
2021-05-21 12:02:04 -0400 -- Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2021-05-21 12:02:06 -0400 -- done
2021-05-21 12:02:06 -0400 -- Dumping repositories ...
* root/demo (@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b) ... [DONE]
[SKIPPED] Wiki
2021-05-21 12:02:06 -0400 -- done
2021-05-21 12:02:06 -0400 -- Dumping uploads ...
2021-05-21 12:02:06 -0400 -- done
2021-05-21 12:02:06 -0400 -- Dumping builds ...
2021-05-21 12:02:06 -0400 -- done
2021-05-21 12:02:06 -0400 -- Dumping artifacts ...
2021-05-21 12:02:06 -0400 -- done
2021-05-21 12:02:06 -0400 -- Dumping pages ...
2021-05-21 12:02:06 -0400 -- done
2021-05-21 12:02:06 -0400 -- Dumping lfs objects ...
2021-05-21 12:02:06 -0400 -- done
2021-05-21 12:02:06 -0400 -- Dumping container registry images ...
2021-05-21 12:02:06 -0400 -- [DISABLED]
Creating backup archive: 1621612926_2021_05_21_12.10.11_gitlab_backup.tar ... done
Uploading backup archive to remote storage ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
done
Deleting old backups ... skipping
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 两个文件包含敏感信息。未被备份到备份文件中。需要手动备份。
使用以上命令会在/var/opt/gitlab/backups目录下创建一个名称类似为1621612926_2021_05_21_12.10.11_gitlab_backup.tar的压缩包, 这个压缩包就是Gitlab整个的完整部分。
[root@localhost backups]# ls -l
-rw------- 1 git git 2969600 May 21 10:40 1621608054_2021_05_21_12.10.11_gitlab_backup.tar
-rw------- 1 git git 2969600 May 21 12:02 1621612926_2021_05_21_12.10.11_gitlab_backup.tar
需手动备份的文件:
Ø /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文件下编辑:
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
三、 定时备份
计划每天凌晨五点进行备份
[root@localhost ~]# crontab -l
0 5 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
四、 备份迁移
本例是通过scp命令将源备份数据复制至新服务器之下,由于有端口限制,故对端口进行了处理:
源端服务器
[root@localhost backups]# scp -P 12018 /var/opt/gitlab/backups/1621846817_2021_05_24_12.10.11_gitlab_backup.tar root@10.78.56.115:/var/opt/gitlab/backups/
root@10.78.56.115's password:
1621612926_2021_05_21_12.10.11_gitlab_backup.tar 100% 2900KB 3.5MB/s 00:00
注意:
l 新服务器的gitlab版本和老服务器的gitlab版本一致
l scp -P 端口,这个P是大写
五、 备份文件恢复
1. 修改文件权限
[root@cqgitlab backups]# chmod 777 1621846817_2021_05_24_12.10.11_gitlab_backup.tar
2. 关闭相关服务
关闭数据库连接服务
[root@cqgitlab backups]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@cqgitlab backups]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
[root@cqgitlab backups]# gitlab-ctl stop nginx
ok: down: nginx: 1s, normally up
这里注意不要关闭gitlab所有服务(gitlab-ctl stop),否则会报错:
3. 执行恢复
gitlab-rake gitlab:backup:restore BACKUP=备份文件编号
[root@cqgitlab backups]# gitlab-rake gitlab:backup:restore BACKUP=1621846817_2021_05_24_12.10.11
注意:
l 此处命令是文件编号,不是文件名称
l 交互界面会有两次让输入“yes”
4. 开启服务
以下几个文件需手动恢复:
Ø /etc/gitlab/gitlab.rb 配置文件
Ø /var/opt/gitlab/nginx/conf nginx配置文件
Ø /etc/postfix/main.cfpostfix 邮件配置备份
Ø /etc/gitlab/gitlab-secrets.json 存储了gitlab的db secret信息
[root@cqgitlab gitlab]# mv /tmp/gitlab* . #将备份在tmp目录下的文件手动进行恢复
[root@cqgitlab backups]# gitlab-ctl start
ok: run: alertmanager: (pid 12840) 1696s
ok: run: gitaly: (pid 12857) 1696s
ok: run: gitlab-exporter: (pid 12888) 1695s
ok: run: gitlab-workhorse: (pid 12893) 1695s
ok: run: grafana: (pid 12904) 1694s
ok: run: logrotate: (pid 12924) 1694s
ok: run: nginx: (pid 12947) 1693s
ok: run: node-exporter: (pid 12965) 1693s
ok: run: postgres-exporter: (pid 12971) 1693s
ok: run: postgresql: (pid 12984) 1692s
ok: run: prometheus: (pid 12993) 1692s
ok: run: redis: (pid 13014) 1691s
ok: run: redis-exporter: (pid 13025) 1691s
ok: run: sidekiq: (pid 25051) 0s
ok: run: unicorn: (pid 25057) 1s
检查状态
都无问题,两边的Gitlab同步完成,数据一致