GitLab 的数据备份和恢复

1、备份相关配置文件

/etc/gitlab/gitlab.rb
/etc/gitlab/gitlab-secrets.json #双因子验证等使用此文件

备份配置文件命令

gitlab-ctl backup-etc --backup-path <DIRECTORY>
# 如果不指定--backup-path <DIRECTORY>,则默认备份至/etc/gitlab/config_backup
# 备份配置文件命令
[root@git01 ~]# gitlab-ctl backup-etc
Could not find '/etc/gitlab/config_backup' directory. Creating.
Running configuration backup
Creating configuration backup archive: gitlab_config_1671363660_2022_12_18.tar
/etc/gitlab/
/etc/gitlab/gitlab.rb.bak
/etc/gitlab/gitlab.rb
/etc/gitlab/gitlab-secrets.json
/etc/gitlab/trusted-certs/
Configuration backup archive complete: /etc/gitlab/config_backup/gitlab_config_1671363660_2022_12_18.tar
Keeping all older configuration backups

[root@git01 ~]# ll /etc/gitlab/config_backup/
total 300
-rw------- 1 root root 307200 Dec 18 19:41 gitlab_config_1671363660_2022_12_18.tar

[root@git01 ~]# tar tvf /etc/gitlab/config_backup/gitlab_config_1671363660_2022_12_18.tar
tar: Removing leading `/' from member names
drwxrwxr-x root/root         0 2022-12-18 19:41 /etc/gitlab/
-rw------- root/root    139779 2022-12-18 18:21 /etc/gitlab/gitlab.rb.bak
-rw------- root/root    140260 2022-12-18 18:21 /etc/gitlab/gitlab.rb
-rw------- root/root     19345 2022-12-18 18:21 /etc/gitlab/gitlab-secrets.json
drwxr-xr-x root/root         0 2022-12-18 18:21 /etc/gitlab/trusted-certs/

2、手动备份数据

不同版本的备份数据命令

# 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
# 新版备份
[root@git01 ~]# gitlab-backup create
2022-12-18 19:44:06 +0800 -- Dumping main_database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2022-12-18 19:44:09 +0800 -- Dumping main_database ... done
2022-12-18 19:44:09 +0800 -- Dumping ci_database ... [DISABLED]
2022-12-18 19:44:09 +0800 -- Dumping repositories ...
{"command":"create","gl_project_path":"testgroup/testproject","level":"info","msg":"started create","relative_path":"@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.git","storage_name":"default","time":"2022-12-18T11:44:09.718Z"}
{"command":"create","gl_project_path":"testgroup/testproject.wiki","level":"info","msg":"started create","relative_path":"@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.wiki.git","storage_name":"default","time":"2022-12-18T11:44:09.834Z"}
{"command":"create","gl_project_path":"testgroup/testproject","level":"info","msg":"completed create","relative_path":"@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.git","storage_name":"default","time":"2022-12-18T11:44:09.837Z"}
{"command":"create","gl_project_path":"testgroup/testproject","level":"info","msg":"started create","relative_path":"@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.design.git","storage_name":"default","time":"2022-12-18T11:44:09.837Z"}
{"command":"create","error":"manager: repository empty: repository skipped","gl_project_path":"testgroup/testproject","level":"warning","msg":"skipped create","relative_path":"@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.design.git","storage_name":"default","time":"2022-12-18T11:44:09.844Z"}
{"command":"create","error":"manager: repository empty: repository skipped","gl_project_path":"testgroup/testproject.wiki","level":"warning","msg":"skipped create","relative_path":"@hashed/d4/73/d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.wiki.git","storage_name":"default","time":"2022-12-18T11:44:09.850Z"}
2022-12-18 19:44:09 +0800 -- Dumping repositories ... done
2022-12-18 19:44:09 +0800 -- Dumping uploads ...
2022-12-18 19:44:09 +0800 -- Dumping uploads ... done
2022-12-18 19:44:09 +0800 -- Dumping builds ...
2022-12-18 19:44:09 +0800 -- Dumping builds ... done
2022-12-18 19:44:09 +0800 -- Dumping artifacts ...
2022-12-18 19:44:09 +0800 -- Dumping artifacts ... done
2022-12-18 19:44:09 +0800 -- Dumping pages ...
2022-12-18 19:44:09 +0800 -- Dumping pages ... done
2022-12-18 19:44:09 +0800 -- Dumping lfs objects ...
2022-12-18 19:44:09 +0800 -- Dumping lfs objects ... done
2022-12-18 19:44:09 +0800 -- Dumping terraform states ...
2022-12-18 19:44:09 +0800 -- Dumping terraform states ... done
2022-12-18 19:44:09 +0800 -- Dumping container registry images ... [DISABLED]
2022-12-18 19:44:09 +0800 -- Dumping packages ...
2022-12-18 19:44:09 +0800 -- Dumping packages ... done
2022-12-18 19:44:09 +0800 -- Creating backup archive: 1671363846_2022_12_18_15.6.2_gitlab_backup.tar ...
2022-12-18 19:44:09 +0800 -- Creating backup archive: 1671363846_2022_12_18_15.6.2_gitlab_backup.tar ... done
2022-12-18 19:44:09 +0800 -- Uploading backup archive to remote storage  ... [SKIPPED]
2022-12-18 19:44:09 +0800 -- Deleting old backups ... [SKIPPED]
2022-12-18 19:44:09 +0800 -- Deleting tar staging files ...
2022-12-18 19:44:09 +0800 -- Cleaning up /var/opt/gitlab/backups/backup_information.yml
2022-12-18 19:44:09 +0800 -- Cleaning up /var/opt/gitlab/backups/db
2022-12-18 19:44:09 +0800 -- Cleaning up /var/opt/gitlab/backups/repositories
2022-12-18 19:44:09 +0800 -- Cleaning up /var/opt/gitlab/backups/uploads.tar.gz
2022-12-18 19:44:09 +0800 -- Cleaning up /var/opt/gitlab/backups/builds.tar.gz
2022-12-18 19:44:09 +0800 -- Cleaning up /var/opt/gitlab/backups/artifacts.tar.gz
2022-12-18 19:44:09 +0800 -- Cleaning up /var/opt/gitlab/backups/pages.tar.gz
2022-12-18 19:44:09 +0800 -- Cleaning up /var/opt/gitlab/backups/lfs.tar.gz
2022-12-18 19:44:09 +0800 -- Cleaning up /var/opt/gitlab/backups/terraform_state.tar.gz
2022-12-18 19:44:09 +0800 -- Cleaning up /var/opt/gitlab/backups/packages.tar.gz
2022-12-18 19:44:09 +0800 -- Deleting tar staging files ... done
2022-12-18 19:44:09 +0800 -- Deleting backups/tmp ...
2022-12-18 19:44:09 +0800 -- Deleting backups/tmp ... done
2022-12-18 19:44:09 +0800 -- 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.
2022-12-18 19:44:09 +0800 -- Backup 1671363846_2022_12_18_15.6.2 is done.
2022-12-18 19:44:09 +0800 -- Deleting backup and restore lock file

[root@git01 ~]# ll /var/opt/gitlab/backups/
total 492
-rw------- 1 git git 501760 Dec 18 19:44 1671363846_2022_12_18_15.6.2_gitlab_backup.tar
# 旧版备份数据
gitlab-rake gitlab:backup:create
ll /var/opt/gitlab/backups/

3、模拟删除项目

图片[1]-GitLab 的数据备份和恢复-李佳程的个人主页

4、执行恢复

恢复的前提条件

备份和恢复使用的版本要一致
还原相关配置文件后,执行gitlab-ctl reconfigure
确保gitlab正在运行状态

新版恢复方法

# 恢复前先停止两个服务
[root@git01 ~]# gitlab-ctl stop puma
ok: down: puma: 0s, normally up
[root@git01 ~]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up

# 恢复时指定备份文件的时间部分,不需要指定文件的全名

[root@git01 ~]# gitlab-backup restore BACKUP=1671363846_2022_12_18_15.6.2

[root@git01 ~]# gitlab-ctl reconfigure
[root@git01 ~]# gitlab-ctl restart
ok: run: alertmanager: (pid 32618) 1s
ok: run: gitaly: (pid 32626) 0s
ok: run: gitlab-exporter: (pid 32640) 1s
ok: run: gitlab-kas: (pid 32727) 0s
ok: run: gitlab-workhorse: (pid 32736) 0s
ok: run: logrotate: (pid 32746) 0s
ok: run: nginx: (pid 32752) 1s
ok: run: node-exporter: (pid 32758) 0s
ok: run: postgres-exporter: (pid 32763) 1s
ok: run: postgresql: (pid 32775) 0s
ok: run: prometheus: (pid 32786) 1s
ok: run: puma: (pid 32794) 0s
ok: run: redis: (pid 32799) 0s
ok: run: redis-exporter: (pid 32805) 1s
ok: run: sidekiq: (pid 32811) 0s

# 观察日志,等待恢复完成,需要再等一会儿再验证是否恢复
[root@git01 ~]# gitlab-ctl tail

# 后续检查可选做
[root@git01 ~]# gitlab-rake gitlab:check SANITIZE=true
[root@git01 ~]# gitlab-rake gitlab:doctor:secrets

旧版恢复方法

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-rake gitlab:backup:restore BACKUP=备份文件名的时间部分
gitlab-ctl restart
图片[2]-GitLab 的数据备份和恢复-李佳程的个人主页

5、确保还原完成

恢复后,项目及用户信息都已还原

注意:可能需要等一段时间才能打开浏览器进行访问

图片[3]-GitLab 的数据备份和恢复-李佳程的个人主页

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享