Skip to content

恢复数据备份

Docker 的备份还原操作与一般安装的操作流程一致,注意版本的一致性即可; 除日常备份外,其服务器升级、迁移等操作都需要提前做好备份,以保数据安全

1)首先找到备份文件

$ cd /opt/gitlab/data/backups

因为是 Docker 容器挂载,路径可能不一样,其默认一般在 /var/opt/gitlab/backups

2)给予备份文件权限,并停止连接数据库的进程

1
2
3
4
5
6
7
8
9
# 查看备份文件归属
$ ls -l
total 1460432
-rw------- 1 chrony polkitd 215040 Feb 24 13:52 1677217616_2023_02_24_13.3.5_gitlab_backup.tar

$ docker exec -it gitlab chmod 777 /var/opt/gitlab/backups/1677217616_2023_02_24_13.3.5_gitlab_backup.tar
$ docker exec -it gitlab gitlab-ctl stop unicorn
$ docker exec -it gitlab gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up

3)使用命令还原数据,中途按提示输入 Yes 即可

$ docker exec -it gitlab gitlab-rake gitlab:backup:restore BACKUP=1677217616_2023_02_24_13.3.5
Unpacking backup ... done
Before restoring the database, we will remove 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)?
...
This task will now rebuild the authorized_keys file.
You will lose any data stored in the authorized_keys file.
Do you want to continue (yes/no)? yes

Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data 
and are not included in this backup. You will need to restore these files manually.
Restore task is done.

以上表示备份恢复完成,但此时并不代表完全恢复,还需要以下操作方可

4)还原相关配置文件

  • gitlab.rb:如同服务器重建,保留配置时可用,新建可不还原
  • gitlab-secrets.json:账密文件,必须还原,新建/迁移都需要
# 此时因是新建服务,定制的配置文件可不还原
$ \cp gitlab-secrets.json /etc/gitlab/gitlab-secrets.json

注:再次重申 Gitlab 密钥文件-此密钥包含了数据库加密密钥和密钥变量。如不能恢复此文件,那么用户的密码就无法访问 gitlab 服务器,或者打开 gitlab 项目会报 500 错误

5)确认无误后,重置配置并重启相关服务

1
2
3
$ docker exec -it gitlab gitlab-ctl reconfigure
$ docker exec -it gitlab gitlab-ctl restart
$ docker exec -it gitlab gitlab-ctl status 

6)验证服务,打开浏览器进行登录,此时管理员的账密应该为备份时的账密,并且查看项目内是否能正常打开、拉取、上传

  • 注意:如 gitlab-secrets.json 文件未还原,程序可以打开,但私人项目可能会报 500 错误