5-Gitlab的数据备份和恢复

1.0备份相关配置文件

官方帮助

https://docs.gitlab.com/ee/raketasks/backup_restore.html
/etc/gitlab/gitlab.rb
/etc/gitlab/gitlab-secrets.json
	双因子验证等使用此文件


2.0备份一下gitlab配置的相关文件

备份配置文件命令

gitlab-ctl backup-etc --backup-path <DIRECTORY>
#如果不指定--backup-path <DIRECTORY>,则默认备份至/etc/gitlab/config_backup/


备份一下gitlab配置的相关文件,没有写备份目录

默认备份到/etc/gitlab/config_backup

并生成一个打包文件

/etc/gitlab/config_backup/gitlab_config_1658284051_2022_07_20.tar

root@gitlab:~# gitlab-ctl backup-etc
Could not find '/etc/gitlab/config_backup' directory. Creating.
Running configuration backup
Creating configuration backup archive: gitlab_config_1658284051_2022_07_20.tar
/etc/gitlab/
/etc/gitlab/trusted-certs/
/etc/gitlab/gitlab.rb.bak
/etc/gitlab/initial_root_password
/etc/gitlab/gitlab-secrets.json
/etc/gitlab/gitlab.rb
Configuration backup archive complete: /etc/gitlab/config_backup/gitlab_config_1658284051_2022_07_20.tar
Keeping all older configuration backups


预览一下压缩包里面有什么东西

root@gitlab:~# tar xvf /etc/gitlab/config_backup/gitlab_config_1658284051_2022_07_20.tar
tar: Removing leading `/' from member names
/etc/gitlab/
/etc/gitlab/trusted-certs/
/etc/gitlab/gitlab.rb.bak
/etc/gitlab/initial_root_password
/etc/gitlab/gitlab-secrets.json
/etc/gitlab/gitlab.rb


3.0手动备份数据

不同版本的备份数据命令

#GitLab 12.2之后版本
gitlab-backup create
#GitLab 12.1之前版本
gitlab-rake gitlab:backup:create


备份相关配置

默认在/etc/gitlab/gitlab.rb文件中指定备份路径,如果目录空间不足,可以修改新的目录
注意:修改完配置需要执行gitlab-ctl reconfigure
# gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
备份的文件权限,所有者和所属组为git
# gitlab_rails['backup_archive_permissions'] = 0644
默认备份过期时长为7天,单位为s, 之后会被自动删除
# gitlab_rails['backup_keep_time'] = 604800


备份相关数据,默认备份目录/var/opt/gitlab/backups

root@gitlab:~# gitlab-backup create 
2022-07-20 10:42:20 +0800 -- Dumping database ... 
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2022-07-20 10:42:23 +0800 -- done
2022-07-20 10:42:23 +0800 -- Dumping repositories ...
time="2022-07-20T10:42:23+08:00" level=info msg="started create" command=create gl_project_path=gitlab-instance-fe824ed1/Monitoring relative_path=@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.git storage_name=default
time="2022-07-20T10:42:23+08:00" level=warning msg="skipped create" command=create error="manager: repository empty: repository skipped" gl_project_path=gitlab-instance-fe824ed1/Monitoring relative_path=@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.git storage_name=default
time="2022-07-20T10:42:23+08:00" level=info msg="started create" command=create gl_project_path=gitlab-instance-fe824ed1/Monitoring.wiki relative_path=@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.wiki.git storage_name=default
time="2022-07-20T10:42:23+08:00" level=info msg="started create" command=create gl_project_path=gitlab-instance-fe824ed1/Monitoring relative_path=@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.design.git storage_name=default
time="2022-07-20T10:42:23+08:00" level=warning msg="skipped create" command=create error="manager: repository empty: repository skipped" gl_project_path=gitlab-instance-fe824ed1/Monitoring relative_path=@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.design.git storage_name=default
time="2022-07-20T10:42:23+08:00" level=info msg="started create" command=create gl_project_path=9527edu/test1 relative_path=@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.git storage_name=default
time="2022-07-20T10:42:23+08:00" level=warning msg="skipped create" command=create error="manager: repository empty: repository skipped" gl_project_path=gitlab-instance-fe824ed1/Monitoring.wiki relative_path=@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.wiki.git storage_name=default
time="2022-07-20T10:42:23+08:00" level=info msg="started create" command=create gl_project_path=9527edu/test1.wiki relative_path=@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.wiki.git storage_name=default
time="2022-07-20T10:42:23+08:00" level=warning msg="skipped create" command=create error="manager: repository empty: repository skipped" gl_project_path=9527edu/test1.wiki relative_path=@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.wiki.git storage_name=default
time="2022-07-20T10:42:23+08:00" level=info msg="started create" command=create gl_project_path=9527edu/test1 relative_path=@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.design.git storage_name=default
time="2022-07-20T10:42:23+08:00" level=warning msg="skipped create" command=create error="manager: repository empty: repository skipped" gl_project_path=9527edu/test1 relative_path=@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.design.git storage_name=default
time="2022-07-20T10:42:23+08:00" level=info msg="started create" command=create gl_project_path=9527help/test2 relative_path=@hashed/4e/07/4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce.git storage_name=default
time="2022-07-20T10:42:24+08:00" level=info msg="completed create" command=create gl_project_path=9527help/test2 relative_path=@hashed/4e/07/4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce.git storage_name=default
time="2022-07-20T10:42:24+08:00" level=info msg="completed create" command=create gl_project_path=9527edu/test1 relative_path=@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.git storage_name=default
time="2022-07-20T10:42:24+08:00" level=info msg="started create" command=create gl_project_path=9527help/test2.wiki relative_path=@hashed/4e/07/4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce.wiki.git storage_name=default
time="2022-07-20T10:42:24+08:00" level=info msg="started create" command=create gl_project_path=9527help/test2 relative_path=@hashed/4e/07/4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce.design.git storage_name=default
time="2022-07-20T10:42:24+08:00" level=warning msg="skipped create" command=create error="manager: repository empty: repository skipped" gl_project_path=9527help/test2 relative_path=@hashed/4e/07/4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce.design.git storage_name=default
time="2022-07-20T10:42:24+08:00" level=info msg="started create" command=create gl_project_path=9527edu/test3 relative_path=@hashed/4b/22/4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a.git storage_name=default
time="2022-07-20T10:42:24+08:00" level=warning msg="skipped create" command=create error="manager: repository empty: repository skipped" gl_project_path=9527help/test2.wiki relative_path=@hashed/4e/07/4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce.wiki.git storage_name=default
time="2022-07-20T10:42:24+08:00" level=info msg="started create" command=create gl_project_path=9527edu/test3.wiki relative_path=@hashed/4b/22/4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a.wiki.git storage_name=default
time="2022-07-20T10:42:24+08:00" level=warning msg="skipped create" command=create error="manager: repository empty: repository skipped" gl_project_path=9527edu/test3.wiki relative_path=@hashed/4b/22/4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a.wiki.git storage_name=default
time="2022-07-20T10:42:24+08:00" level=info msg="started create" command=create gl_project_path=9527edu/test3 relative_path=@hashed/4b/22/4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a.design.git storage_name=default
time="2022-07-20T10:42:24+08:00" level=warning msg="skipped create" command=create error="manager: repository empty: repository skipped" gl_project_path=9527edu/test3 relative_path=@hashed/4b/22/4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a.design.git storage_name=default
time="2022-07-20T10:42:24+08:00" level=info msg="completed create" command=create gl_project_path=9527edu/test3 relative_path=@hashed/4b/22/4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a.git storage_name=default
2022-07-20 10:42:24 +0800 -- done
2022-07-20 10:42:24 +0800 -- Dumping uploads ... 
2022-07-20 10:42:24 +0800 -- done
2022-07-20 10:42:24 +0800 -- Dumping builds ... 
2022-07-20 10:42:24 +0800 -- done
2022-07-20 10:42:24 +0800 -- Dumping artifacts ... 
2022-07-20 10:42:24 +0800 -- done
2022-07-20 10:42:24 +0800 -- Dumping pages ... 
2022-07-20 10:42:24 +0800 -- done
2022-07-20 10:42:24 +0800 -- Dumping lfs objects ... 
2022-07-20 10:42:24 +0800 -- done
2022-07-20 10:42:24 +0800 -- Dumping container registry images ... 
2022-07-20 10:42:24 +0800 -- [DISABLED]
Creating backup archive: 1658284944_2022_07_20_14.3.2_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.
报错是提醒你这里只备份数据,但是不备份配置文件,自己手动备份一下就行

查看一下备份的数据

root@gitlab:~# ls /var/opt/gitlab/backups
1658284944_2022_07_20_14.3.2_gitlab_backup.tar


范例: 旧版备份数据

#在任意目录即可备份当前gitlab数据
[root@ubuntu1804 ~]#gitlab-rake gitlab:backup:create
2020-03-07 14:34:56 +0800 -- Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2020-03-07 14:34:57 +0800 -- done
2020-03-07 14:34:57 +0800 -- Dumping repositories ...
 * testgroup/testproject ... [DONE]
[SKIPPED] Wiki
2020-03-07 14:34:58 +0800 -- done
2020-03-07 14:34:58 +0800 -- Dumping uploads ...
2020-03-07 14:34:58 +0800 -- done
2020-03-07 14:34:58 +0800 -- Dumping builds ...
2020-03-07 14:34:58 +0800 -- done
2020-03-07 14:34:58 +0800 -- Dumping artifacts ...
2020-03-07 14:34:58 +0800 -- done
2020-03-07 14:34:58 +0800 -- Dumping pages ...
2020-03-07 14:34:58 +0800 -- done
2020-03-07 14:34:58 +0800 -- Dumping lfs objects ...
2020-03-07 14:34:58 +0800 -- done
2020-03-07 14:34:58 +0800 -- Dumping container registry images ...
2020-03-07 14:34:58 +0800 -- [DISABLED]
Creating backup archive: 1583562898_2020_03_07_11.11.8_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
[root@ubuntu1804 ~]#ll /var/opt/gitlab/backups/
-rw-------  1 git git 102400 Mar  7 14:34 
1583562898_2020_03_07_11.11.8_gitlab_backup.tar


4.0模拟删除项目

把test1这个项目完全的删掉,这样才能模拟恢复
用管理员登录删掉test1项

image-20220720105542761.png


找到test1的项目点进去

image-20220720105618198.png


image-20220720110052811.png

找到设置-通用-高级(展开)最下面

image-20220720110157509.png

拉到最下面找到删除项目

image-20220720110252762.png



image-20220720110610418.png



这个时候可以看到test1的这个项目已经删除掉了

image-20220720110652078.png


5.0还原删除的数据

恢复的前提条件

备份和恢复使用的版本一致

还原相关配置文件后,执行gitlab-ctl reconfigure

确保gitlab正在运行状态

旧版恢复方法

#恢复前先停止两个服务 
[root@ubuntu1804 ~]#gitlab-ctl stop unicorn 
[root@ubuntu1804 ~]#gitlab-ctl stop sidekiq  
#恢复时指定备份文件的时间部分,不需要指定文件的全名 
[root@ubuntu1804 ~]# gitlab-rake gitlab:backup:restore BACKUP=备份文件名


新版恢复方法

查看服务状态
root@gitlab:~# gitlab-ctl status

恢复前需要先停掉两个服务
root@gitlab:~# gitlab-ctl stop puma
ok: down: puma: 0s, normally up
root@gitlab:~# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up

#恢复时指定备份的时间部分,不需要指定文件的全名
需要按两次yes,会有报错不用管
root@gitlab:~# gitlab-backup restore BACKUP=1658284944_2022_07_20_14.3.2

恢复好以后启动服务
root@gitlab:~# gitlab-ctl start puma
ok: run: puma: (pid 612) 1s
root@gitlab:~# gitlab-ctl start sidekiq
ok: run: sidekiq: (pid 632) 1s

也可以把所有服务重启一下,确保所有服务都能启动
root@gitlab:~# gitlab-ctl restart
ok: run: alertmanager: (pid 881) 0s
ok: run: gitaly: (pid 892) 0s
ok: run: gitlab-exporter: (pid 914) 0s
ok: run: gitlab-workhorse: (pid 916) 0s
ok: run: grafana: (pid 925) 1s
ok: run: logrotate: (pid 940) 0s
ok: run: nginx: (pid 948) 1s
ok: run: node-exporter: (pid 957) 0s
ok: run: postgres-exporter: (pid 967) 1s
ok: run: postgresql: (pid 983) 0s
ok: run: prometheus: (pid 986) 0s
ok: run: puma: (pid 1006) 0s
ok: run: redis: (pid 1092) 0s
ok: run: redis-exporter: (pid 1098) 1s
ok: run: sidekiq: (pid 1108) 1s



查看项目里面已经把test1的项目恢复了

image-20220720112523653.png



image-20220720112548158.png

1

Powered By Z-BlogPHP 1.7.3

 Theme By 优美模版

本博客为开源博客,本博客联系QQ:372097243