Gitlab备份和恢复

原创

公司的服务器出问题了,才想起能不能把gitlab迁移到别的地方去,网上查了些资料,实操了一把,搞定了。

1.gitlab备份目录设置

[root@iZ2ze9vi1jdssanmwlovsyZ ~]# vim /etc/gitlab/gitlab.rb

进入到配置文件后找到

# gitlab_rails['manage_backup_path'] = true
# gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"

默认的路径为/var/opt/gitlab/backups,可自己修改。

2.备份

1)手动备份gitlab

执行gitlab-rake gitlab:backup:create

到备份目录下检查一下备份文件,名称格式为:1510472027_2017_11_12_9.4.5_gitlab_backup.tar

2)编写备份脚本,结合crontab实施自动定时备份,比如每天0点、6点、12点、18点各备份一次

在备份目录下创建备份文件

[root@code-server backups]# vim gitlab_backup.sh

#!/bin/bash
/usr/bin/gitlab-rake gitlab:backup:create CRON=1

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

配置执行时间

[root@code-server backups]# crontab -l
0 0,6,12,18 * * * /bin/bash -x /data/gitlab/backups/gitlab_backup.sh > /dev/null 2>&1

3.恢复备份

注意:GItlab只能还原到与备份文件相同的gitlab版本。

gitlab恢复操作:

1)停止相关数据连接服务

[root@code-server backups]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@code-server backups]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 1s, normally up
[root@code-server backups]# gitlab-ctl status
run: gitaly: (pid 98087) 1883s; run: log: (pid 194202) 163003s
run: gitlab-monitor: (pid 98101) 1883s; run: log: (pid 194363) 163002s
run: gitlab-workhorse: (pid 98104) 1882s; run: log: (pid 194362) 163002s
run: logrotate: (pid 98117) 1882s; run: log: (pid 5793) 160832s
run: nginx: (pid 98123) 1881s; run: log: (pid 194359) 163002s
run: node-exporter: (pid 98167) 1881s; run: log: (pid 194360) 163002s
run: postgres-exporter: (pid 98173) 1881s; run: log: (pid 194204) 163003s
run: postgresql: (pid 98179) 1880s; run: log: (pid 194365) 163002s
run: prometheus: (pid 98187) 1880s; run: log: (pid 194364) 163002s
run: redis: (pid 98230) 1879s; run: log: (pid 194358) 163002s
run: redis-exporter: (pid 98234) 1879s; run: log: (pid 194208) 163003s
down: sidekiq: 8s, normally up; run: log: (pid 194437) 163001s
down: unicorn: 21s, normally up; run: log: (pid 194443) 163001s

2)通过之前的备份文件进行恢复

Gitlab的恢复操作会先将当前所有的数据清空,然后再根据备份数据进行恢复

[root@code-server backups]# gitlab-rake gitlab:backup:restore BACKUP=1510472027_2017_11_12_9.4.5
Unpacking backup ... done
Before restoring the database we recommend removing all existing
tables to avoid future upgrade problems. Be aware that if you have
custom tables in the GitLab database these tables and all data will be
removed.
  
Do you want to continue (yes/no)?
........
ALTER TABLE
ALTER TABLE
ALTER TABLE
ALTER TABLE
WARNING:  no privileges were granted for "public"
GRANT
[DONE]
done
Restoring repositories ...
 * treesign/treesign ... [DONE]
 * gateway/gateway ... [DONE]
 * treesign/treesign-doc ... [DONE]
 * qwsign/qwsign ... [DONE]
 * qwsign/qwsign-doc ... [DONE]
 * test/test ... [DONE]
Put GitLab hooks in repositories dirs [DONE]
done
Restoring uploads ...
done
Restoring builds ...
done
Restoring artifacts ...
done
Restoring pages ...
done
Restoring lfs objects ...
done
This will rebuild an authorized_keys file.
You will lose any data stored in authorized_keys file.
Do you want to continue (yes/no)? yes
  
  
Deleting tmp directories ... done
done
done
done
done
done
done
done

启动Gitlab

[root@code-server backups]# gitlab-ctl start

然后稍等一会(如果启动gitlab后,访问出现500,这是因为redis等程序还没完全启动,等一会儿访问就ok了),再次登录Gitlab,就会发现之前误删除的test项目已经恢复了!

正文到此结束