Gitlab备份与恢复
对gitlab进行备份将会创建一个包含所有库和附件的归档文件。对备份的恢复只能恢复到与备份时的gitlab相同的版本。将gitlab迁移到另一台服务器上的最佳方法就是通过备份和还原。
gitlab提供了一个简单的命令行来备份整个gitlab,并且能灵活的满足需求。
备份文件将保存在配置文件中定义的backup_path中,文件名为TIMESTAMP_gitlab_backup.tar,TIMESTAMP为备份时的时间戳。TIMESTAMP的格式为:EPOCH_YYYY_MM_DD_Gitlab‐version。如果自定义备份目录需要赋予git权限。
01). 备份
配置文件中加入
root@git ~/git_data]# vim /etc/gitlab/gitlab.rb
gitlab_rails['manage_backup_path'] = true #开启备份gitlab_rails['backup_path'] = '/data/backup/gitlab' #备份目录gitlab_rails['backup_archive_permissions'] = 0644 #生成的备份文件权限gitlab_rails['backup_keep_time'] = 604800 #备份保留的时间(以秒为单位,这是七天默认值)
[root@git ~/git_data]# mkdir /data/backup/gitlab
[root@git ~/git_data]# chown ‐R git.git /data/backup/gitlab
#完成后执行下面命令进行生效
[root@git ~/git_data]# gitlab‐ctl reconfigure
#手动备份
[root@git ~/git_data]# gitlab-rake gitlab:backup:create
#检查结果
[root@git ~/git_data]# ll /data/opt/gitlab/total 112
-rw-r--r-- 1 git git 112640 Nov 17 18:22 1573986174_2019_11_17_10.2.2_gitlab_backup.tar
自动备份需要写一个备份脚本及定时任务
#Gitlab恢复操作
Gitlab只能还原到与备份文件相同的gitlab版本。
#误删除数据
[root@git ~/git_test]# rm -rf ./*
#提交推送
[root@git ~/git_test]# git commit -am "rm 123 "
[master 35ef629] add 123
3 files changed, 8 deletions(-)
delete mode 100644 a
delete mode 100644 a.txt
delete mode 100644 test
[root@git ~/git_test]# git push -u origin master
Counting objects: 3, done.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (2/2), 182 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To git@10.0.0.100:OPS/git_test.git
02250b6..35ef629 master -> master
Branch master set up to track remote branch master from origin.
02). 恢复
先停止相关服务
[root@git ~/git_test]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@git ~/git_test]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 1s, normally up
[root@git ~/git_test]# gitlab-ctl status
run: gitaly: (pid 78774) 15714s; run: log: (pid 78466) 15758s
run: gitlab-monitor: (pid 78790) 15713s;run: log: (pid 78565) 15746s
run: gitlab-workhorse: (pid 78762) 15714s;run: log: (pid 78419) 15776s
run: logrotate: (pid 106694) 1364s;run: log: (pid 78447) 15764s
run: nginx: (pid 78432) 15770s; run: log: (pid 78431) 15770s
run: node-exporter: (pid 78515) 15752s; run: log: (pid 78514) 15752s
run: postgres-exporter: (pid 78818) 15712s;run: log: (pid 78692) 15728s
run: postgresql: (pid 78190) 15819s; run: log: (pid 78189) 15819s
run: prometheus: (pid 78806) 15713s; run: log: (pid 78646) 15734s
run: redis: (pid 78130) 15825s; run: log: (pid 78129) 15825s
run: redis-exporter: (pid 78626) 15740s;run: log: (pid 78625) 15740s
down: sidekiq: 8s, normally up; run: log: (pid 78402) 15782s
down: unicorn: 20s, normally up; run: log: (pid 78363) 15788s
#Gitlab的恢复操作会先将当前所有的数据清空,然后再根据备份数据进行恢复
[root@git ~/git_test]# ll /data/opt/gitlab/
total 112
-rw------- 1 git git 112640 Nov 17 18:22 1573986174_2019_11_17_10.2.2_gitlab_backup.tar
[root@git ~/git_test]# gitlab-rake gitlab:backup:restore BACKUP=1573986174_2019_11_17_10.2.2
#启动gitlab
[root@git ~/git_test]# gitlab-ctl start
ok: run: gitaly: (pid 78774) 16168sok: run: gitlab-monitor: (pid 78790) 16167sok: run: gitlab-workhorse: (pid 78762) 16168sok: run: logrotate: (pid 106694) 1818sok: run: nginx: (pid 78432) 16224sok: run: node-exporter: (pid 78515) 16206sok: run: postgres-exporter: (pid 78818) 16166sok: run: postgresql: (pid 78190) 16273sok: run: prometheus: (pid 78806) 16167sok: run: redis: (pid 78130) 16279sok: run: redis-exporter: (pid 78626) 16194sok: run: sidekiq: (pid 109932) 1sok: run: unicorn: (pid 109939) 0s
#检查是否恢复成功
[root@git ~/git_test]# gitlab-rake gitlab:check SANITIZE=true
web界面数据已经恢复,从新克隆到本地
[root@git ~]# git clone git@10.0.0.100:OPS/git_test.git
Cloning into 'git_test'...
remote: Counting objects: 27, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 27 (delta 1), reused 27 (delta 1)
Receiving objects: 100% (27/27), done.
Resolving deltas: 100% (1/1), done.
[root@git ~/git_test]# ll
total 8
-rw-r--r-- 1 root root 27 Nov 17 19:00 a
-rw-r--r-- 1 root root 7 Nov 17 19:00 a.txt
-rw-r--r-- 1 root root 0 Nov 17 19:00 test