之前搭建gitlab的vps即将到期,换了一家服务商,因此需要迁移原gitlab,原gitlab是基于docker搭建的,开整
主要是2部分,仓库相关的和账号相关,账号相关的包含敏感信息需要单独备份
备份仓库数据
docker exec -it <docker_container_name> gitlab-rake gitlab:backup:create
docker_container_name替换为真实的容器名称
执行这条命令后,会在/var/opt/gitlab/backups目录下生成成以_gitlab_backup.tar 结尾的备份文件,因为我的docker容器做了映射 (/home/gitlab/data:/var/opt/gitlab),所以在/home/gitlab/data/backups目录下可以找到备份文件
备份账号信息
主要就gitlab.rb和gitlab-secrets.json这2个文件,容器映射的配置目录为/home/gitlab/config:/etc/gitlab,所以直接找到/home/gitlab/config目录下的gitlab.rb和gitlab-secrets.json
环境准备
在新服务器先搭建docker等基础环境,然后拉取gitlab镜像,这里需要注意,拉取的镜像需要跟原来的版本保持一致,否则可能会无法恢复,因为新服务器又多个docker容器,所以这里通过docker-compose来统一管理,编辑 docker-compose.yml
1version: '3' 2 3services: 4 gitlab: 5 image: gitlab/gitlab-ce:16.0.1-ce.0 6 restart: always 7 container_name: gitlab 8 hostname: gitlab.com 9 environment: 10 GITLAB_OMNIBUS_CONFIG: | 11 external_url 'http://gitlab.com' 12 ports: 13 - "8080:80" 14 - "4433:443" 15 - "222:22" 16 volumes: 17 - /srv/gitlab/config:/etc/gitlab 18 - /srv/gitlab/logs:/var/log/gitlab 19 - /srv/gitlab/data:/var/opt/gitlab
启动容器
docker-compose up -d
启动gitlab可能会有点慢,查看容器状态,直到status为up
docker-compose ps
恢复仓库备份
将上述步骤获得的xx_gitlab_backup.tar文件copy到/srv/gitlab/data/backups目录下,然后执行恢复命令:
docker exec -it gitlab gitlab-rake gitlab:backup:restore BACKUP=xx
ps: 假如你的备份文件为:xx_gitlab_backup.tar,则恢复命令的BACKUP=xx需要对应上,不然识别不了
恢复过程有2次确认,输入yes回车即可
部分日志如下:
1This task will now rebuild the authorized_keys file. 2You will lose any data stored in the authorized_keys file. 3Do you want to continue (yes/no)? yes 4 52024-02-15 07:26:28 UTC -- Deleting tar staging files ... 62024-02-15 07:26:28 UTC -- Cleaning up /var/opt/gitlab/backups/backup_information.yml 72024-02-15 07:26:28 UTC -- Cleaning up /var/opt/gitlab/backups/db 82024-02-15 07:26:28 UTC -- Cleaning up /var/opt/gitlab/backups/repositories 92024-02-15 07:26:28 UTC -- Cleaning up /var/opt/gitlab/backups/uploads.tar.gz 102024-02-15 07:26:28 UTC -- Cleaning up /var/opt/gitlab/backups/builds.tar.gz 112024-02-15 07:26:28 UTC -- Cleaning up /var/opt/gitlab/backups/artifacts.tar.gz 122024-02-15 07:26:28 UTC -- Cleaning up /var/opt/gitlab/backups/pages.tar.gz 132024-02-15 07:26:28 UTC -- Cleaning up /var/opt/gitlab/backups/lfs.tar.gz 142024-02-15 07:26:28 UTC -- Cleaning up /var/opt/gitlab/backups/terraform_state.tar.gz 152024-02-15 07:26:28 UTC -- Cleaning up /var/opt/gitlab/backups/packages.tar.gz 162024-02-15 07:26:28 UTC -- Deleting tar staging files ... done 172024-02-15 07:26:28 UTC -- Deleting backups/tmp ... 182024-02-15 07:26:28 UTC -- Deleting backups/tmp ... done 192024-02-15 07:26:28 UTC -- Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data 20and are not included in this backup. You will need to restore these files manually. 212024-02-15 07:26:28 UTC -- Restore task is done. 222024-02-15 07:26:28 UTC -- Deleting backup and restore PID file ... done
提示:Restore task is done则代表恢复成功
恢复账号信息
将上述步骤获得的gitlab.rb和gitlab-secrets.json文件copy到/srv/gitlab/config目录下,然后执行恢复命令
docker exec -it gitlab gitlab-ctl reconfigure
恢复成功以后,提示: gitlab Reconfigured!
验证恢复结果
浏览器使用 http://ip:8080 打开网页版gitlab,使用原账号登陆检查数据是否都已恢复
重置gitlab的root密码
1# 进入 GitLab 容器 2docker exec -it gitlab /bin/bash 3 4# 打开 Rails 控制台 5gitlab-rails console 6 7# 找到并重置 root 用户的密码 8user = User.find(1) 9user.password = 'your_new_password' 10user.password_confirmation = 'your_new_password' 11user.save! 12 13# 退出控制台 14exit