如何进行Gitlab备份与恢复?
OS:Centos 7.4 64位,gitlab版本:12.2.3 以上环境为例进行Gitlab备份和恢复操作的相关配置。
1、配置Gitlab的备份路径及权限
[root@gitlab ~]# vi /etc/gitlab/gitlab.rb
gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/data/gitlab/backups" //gitlab备份路径
gitlab_rails['backup_archive_permissions'] = 0644 //生成的备份文件的权限
gitlab_rails['backup_keep_time'] = 7776000 //备份保留天数为3个月(即90天,这里是7776000秒)
[root@gitlab ~]# mkdir -p /data/gitlab/backups //新建gitlab备份路径
[root@gitlab ~]# chown -R git.git /data/gitlab/backups //设置备份目录的所有者
[root@gitlab ~]# chmod -R 777 /data/gitlab/backups /设置备份目录的权限
[root@gitlab ~]# gitlab-ctl reconfigure //执行重载gitlab配置命令,是上述配置生效!
2、GItlab备份操作
1)、手动备份gitlab
[root@gitlab backups]# gitlab-rake gitlab:backup:create
手动备份gitlab以后,然后查看下备份文件(文件权限是644)
[root@gitlab backups]# ll
total 41496
-rw-r–r–. 1 git git 42485760 Sep 15 18:00 1568541612_2019_09_15_12.2.3_gitlab_backup.tar
2)、 自动备份gitlab
首先要编写备份脚本,使用crontab实现自动定时备份,比如每天0点、12点、19点各备份一次,备份路径是:/data/gitlab/backups
[root@gitlab backups]# mkdir -p /data/cron //创建备份脚本目录
[root@gitlab backups]# cd /data/cron/
[root@gitlab cron]# vi gitlab_backup.sh //创建备份脚本文件gitlab_backup.sh
#!/bin/bash
/usr/bin/gitlab-rake gitlab:backup:create CRON=1
注意:环境变量CRON=1的作用是在没有任何错误发生时,不显示备份进度输出
0 0,12,19 * * * /bin/bash -x /data/cron/gitlab_backup.sh > /dev/null 2>&1
3、Gitlab恢复操作
注:GItlab只能还原到与备份文件相同的gitlab版本。
假设上面gitlab备份之前创建了webapp项目,然后不小心误删了webapp项目,现在就进行gitlab恢复操作如下:
1)、停止gitlab相关服务
[root@gitlab /]# gitlab-ctl stop
ok: down: alertmanager: 0s, normally up
ok: down: gitaly: 1s, normally up
ok: down: gitlab-monitor: 0s, normally up
ok: down: gitlab-workhorse: 0s, normally up
ok: down: grafana: 0s, normally up
ok: down: logrotate: 0s, normally up
ok: down: nginx: 1s, normally up
ok: down: node-exporter: 0s, normally up
ok: down: postgres-exporter: 1s, normally up
ok: down: postgresql: 0s, normally up
ok: down: prometheus: 0s, normally up
ok: down: redis: 0s, normally up
ok: down: redis-exporter: 0s, normally up
ok: down: sidekiq: 0s, normally up
ok: down: unicorn: 0s, normally up
[root@gitlab /]# gitlab-ctl status
down: alertmanager: 17s, normally up; run: log: (pid 1442) 1998795s
down: gitaly: 17s, normally up; run: log: (pid 1500) 1998794s
down: gitlab-monitor: 16s, normally up; run: log: (pid 1451) 1998794s
down: gitlab-workhorse: 16s, normally up; run: log: (pid 1450) 1998794s
down: grafana: 15s, normally up; run: log: (pid 1504) 1998794s
down: logrotate: 15s, normally up; run: log: (pid 1498) 1998794s
down: nginx: 15s, normally up; run: log: (pid 1446) 1998795s
down: node-exporter: 14s, normally up; run: log: (pid 1439) 1998795s
down: postgres-exporter: 14s, normally up; run: log: (pid 1443) 1998795s
down: postgresql: 14s, normally up; run: log: (pid 1428) 1998796s
down: prometheus: 14s, normally up; run: log: (pid 1458) 1998795s
down: redis: 13s, normally up; run: log: (pid 1452) 1998795s
down: redis-exporter: 13s, normally up; run: log: (pid 1448) 1998796s
down: sidekiq: 12s, normally up; run: log: (pid 1453) 1998795s
down: unicorn: 11s, normally up; run: log: (pid 1454) 1998795s
2)、查看备份路径/data/gitlab/backups下的备份文件并进行恢复操作
[root@gitlab backups]# ll
total 41496
-rw-r--r--. 1 git git 42485760 Sep 15 18:00 1568541612_2019_09_15_12.2.3_gitlab_backup.tar //通过备份可知gitlab版本是12.2.3,恢复当前gitlab必须为该版本。
[root@gitlab backups]# gitlab-rake gitlab:backup:restore BACKUP=1568541612_2019_09_15_12.2.3
注:Gitlab的恢复操作会先将当前所有的数据清空,然后再根据备份数据进行恢复。
3)、启动Gitlab(使恢复数据生效)
[root@gitlab backups]# gitlab-ctl start
ok: run: alertmanager: (pid 15905) 1s
ok: run: gitaly: (pid 15921) 0s
ok: run: gitlab-monitor: (pid 15943) 1s
ok: run: gitlab-workhorse: (pid 15958) 0s
ok: run: grafana: (pid 15972) 1s
ok: run: logrotate: (pid 15994) 0s
ok: run: nginx: (pid 16000) 0s
ok: run: node-exporter: (pid 16078) 1s
ok: run: postgres-exporter: (pid 16094) 0s
ok: run: postgresql: (pid 16101) 1s
ok: run: prometheus: (pid 16103) 0s
ok: run: redis: (pid 16120) 1s
ok: run: redis-exporter: (pid 16125) 0s
ok: run: sidekiq: (pid 16138) 0s
ok: run: unicorn: (pid 16144) 1s
[root@gitlab backups]# gitlab-ctl status
run: alertmanager: (pid 15905) 150s; run: log: (pid 1442) 1999598s
run: gitaly: (pid 15921) 149s; run: log: (pid 1500) 1999597s
run: gitlab-monitor: (pid 15943) 149s; run: log: (pid 1451) 1999597s
run: gitlab-workhorse: (pid 15958) 148s; run: log: (pid 1450) 1999597s
run: grafana: (pid 15972) 148s; run: log: (pid 1504) 1999597s
run: logrotate: (pid 15994) 147s; run: log: (pid 1498) 1999597s
run: nginx: (pid 16000) 147s; run: log: (pid 1446) 1999598s
run: node-exporter: (pid 16078) 147s; run: log: (pid 1439) 1999598s
run: postgres-exporter: (pid 16094) 146s; run: log: (pid 1443) 1999598s
run: postgresql: (pid 16101) 146s; run: log: (pid 1428) 1999598s
run: prometheus: (pid 16103) 145s; run: log: (pid 1458) 1999597s
run: redis: (pid 16120) 145s; run: log: (pid 1452) 1999597s
run: redis-exporter: (pid 16125) 144s; run: log: (pid 1448) 1999598s
run: sidekiq: (pid 16138) 144s; run: log: (pid 1453) 1999597s
run: unicorn: (pid 16144) 144s; run: log: (pid 1454) 1999597s
[root@gitlab backups]#
恢复命令执行完成后,可以check检查一下数据恢复情况命令如下:
[root@gitlab backups]# gitlab-rake gitlab:check SANITIZE=true
Checking GitLab subtasks ...
Checking GitLab Shell ...
GitLab Shell: ... GitLab Shell version >= 9.3.0 ? ... OK (9.3.0)
Running /opt/gitlab/embedded/service/gitlab-shell/bin/check
Check GitLab API access: OK
Redis available via internal API: OK
Access to /var/opt/gitlab/.ssh/authorized_keys: OK
gitlab-shell self-check successful
Checking GitLab Shell ... Finished
Checking Gitaly ...
Gitaly: ... default ... OK
Checking Gitaly ... Finished
Checking Sidekiq ...
Sidekiq: ... Running? ... yes
Number of Sidekiq processes ... 1
Checking Sidekiq ... Finished
Checking Incoming Email ...
Incoming Email: ... Reply by email is disabled in config/gitlab.yml
Checking Incoming Email ... Finished
Checking LDAP ...
LDAP: ... LDAP is disabled in config/gitlab.yml
Checking LDAP ... Finished
Checking GitLab App ...
Git configured correctly? ... yes
Database config exists? ... yes
All migrations up? ... yes
Database contains orphaned GroupMembers? ... no
GitLab config exists? ... yes
GitLab config up to date? ... yes
Log directory writable? ... yes
Tmp directory writable? ... yes
Uploads directory exists? ... yes
Uploads directory has correct permissions? ... yes
Uploads directory tmp has correct permissions? ... yes
Init script exists? ... skipped (omnibus-gitlab has no init script)
Init script up-to-date? ... skipped (omnibus-gitlab has no init script)
Projects have namespace: ...
7/24 ... yes
7/25 ... yes
7/26 ... yes
7/27 ... yes
7/28 ... yes
7/29 ... yes
7/30 ... yes
Redis version >= 2.8.0? ... yes
Ruby version >= 2.5.3 ? ... yes (2.6.3)
Git version >= 2.22.0 ? ... yes (2.22.0)
Git user has default SSH configuration? ... yes
Active users: ... 2
Checking GitLab App ... Finished
Checking GitLab subtasks ... Finished
看到以上提示信息,证明数据已恢复成功,重新登录Gitlab,就会发现之前误删除的webapp项目已经恢复,大功告成了!!!
版权声明:
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自
阳光•雨!
喜欢就支持一下吧