docker gitlab 备份 恢复 版本升级(16.1.1到18.2.0)
docker 启动
# 在线
docker pull gitlab/gitlab-ce:latest
# 离线
docker save -o gitlab-ce-latest.tar gitlab/gitlab-ce:latest
docker load -i gitlab-ce-latest.tar
docker run --detach \--publish 8021:80 --publish 8023:22 \ --name gitlab_test \--restart always \--volume /data/docker/gitlab_test/config:/etc/gitlab \--volume /data/docker/gitlab_test/logs:/var/log/gitlab \--volume /data/docker/gitlab_test/data:/var/opt/gitlab \gitlab/gitlab-ce:latest
备份
- 50_backup.sh
- 0 1 * * 1 nohup sh /data/zhengqi_data/shanghai_backup/50_backup.sh >> /data/zhengqi_data/shanghai_backup/50_backup.out 2>1&
#!/bin/sh
# 获取当前日期
current_date=$(date +%Y%m%d%H%M%S)
backup_path=/data/zhengqi_data/shanghai_backup
echo "开始执行备份脚本,当前时间$current_date"echo "创建gitlab备份包"
git_path=/data/docker/gitlab
git_new_backup_path=$backup_path/gitlab/$current_date
git_old_backup_path=$(find "$backup_path"/gitlab -type d -mindepth 1 -maxdepth 1 | head -n 1)
echo "旧gitlab备份文件路径====>$git_old_backup_path"
git_path_backup=$git_path/data/backups/$current_date
sudo docker exec -t gitlab gitlab-rake gitlab:backup:create BACKUP=$git_path_backup
echo "创建完成"
echo "转移至备份文件夹"
mkdir -p $git_new_backup_path/data
mkdir -p $git_new_backup_path/configmv "$git_path_backup"_gitlab_backup.tar $git_new_backup_path/data
cp $git_path/config/gitlab.rb $git_new_backup_path/config
cp $git_path/config/gitlab-secrets.json $git_new_backup_path/config
echo "转移完成"
echo "清理本机旧备份内容"
# 检查文件夹是否存在
if [ -d "$git_old_backup_path" ]; then# 如果存在,删除文件夹及其内容rm -r "$git_old_backup_path"echo "文件夹删除成功 $git_old_backup_path"
elseecho "文件夹不存在: $git_old_backup_path"
fi
echo "备份结束"
恢复
# 进入容器
# 停止 GitLab 容器
docker stop gitlab_test # 启动 GitLab 容器
docker start gitlab_test docker exec -it gitlab_test /bin/bash
chmod 777 /var/opt/gitlab/backups/20250714100001_gitlab_backup.tar
# gitlab-backup restore BACKUP=20250714100001
root@ea0b9740515c:/# gitlab-backup restore BACKUP=20250714100001
2025-07-18 07:00:04 UTC -- Unpacking backup ...
2025-07-18 07:02:58 UTC -- Unpacking backup ... done
2025-07-18 07:02:58 UTC -- Restoring database ...
2025-07-18 07:02:58 UTC -- Be sure to stop Puma, Sidekiq, and any other process that
connects to the database before proceeding. For Omnibus
installs, see the following link for more information:
https://docs.gitlab.com/ee/raketasks/backup_restore.html#restore-for-omnibus-gitlab-installationsBefore 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)? yes2025-07-18 07:16:14 UTC -- Restoring repositories ... done
2025-07-18 07:16:14 UTC -- Restoring uploads ...
2025-07-18 07:16:14 UTC -- Restoring uploads ... done
2025-07-18 07:16:14 UTC -- Restoring builds ...
2025-07-18 07:16:14 UTC -- Restoring builds ... done
2025-07-18 07:16:14 UTC -- Restoring artifacts ...
2025-07-18 07:16:14 UTC -- Restoring artifacts ... done
2025-07-18 07:16:14 UTC -- Restoring pages ...
2025-07-18 07:16:14 UTC -- Restoring pages ... done
2025-07-18 07:16:14 UTC -- Restoring lfs objects ...
2025-07-18 07:16:14 UTC -- Restoring lfs objects ... done
2025-07-18 07:16:14 UTC -- Restoring terraform states ...
2025-07-18 07:16:14 UTC -- Restoring terraform states ... done
2025-07-18 07:16:14 UTC -- Restoring packages ...
2025-07-18 07:16:14 UTC -- Restoring packages ... done
2025-07-18 07:16:14 UTC -- Restoring ci secure files ...
2025-07-18 07:16:14 UTC -- Restoring ci secure files ... done
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)? yes2025-07-18 07:39:40 UTC -- Deleting tar staging files ...
2025-07-18 07:39:40 UTC -- Cleaning up /var/opt/gitlab/backups/backup_information.yml
2025-07-18 07:39:40 UTC -- Cleaning up /var/opt/gitlab/backups/db
2025-07-18 07:39:40 UTC -- Cleaning up /var/opt/gitlab/backups/repositories
2025-07-18 07:39:44 UTC -- Cleaning up /var/opt/gitlab/backups/uploads.tar.gz
2025-07-18 07:39:44 UTC -- Cleaning up /var/opt/gitlab/backups/builds.tar.gz
2025-07-18 07:39:44 UTC -- Cleaning up /var/opt/gitlab/backups/artifacts.tar.gz
2025-07-18 07:39:44 UTC -- Cleaning up /var/opt/gitlab/backups/pages.tar.gz
2025-07-18 07:39:44 UTC -- Cleaning up /var/opt/gitlab/backups/lfs.tar.gz
2025-07-18 07:39:44 UTC -- Cleaning up /var/opt/gitlab/backups/terraform_state.tar.gz
2025-07-18 07:39:44 UTC -- Cleaning up /var/opt/gitlab/backups/packages.tar.gz
2025-07-18 07:39:44 UTC -- Cleaning up /var/opt/gitlab/backups/ci_secure_files.tar.gz
2025-07-18 07:39:44 UTC -- Deleting tar staging files ... done
2025-07-18 07:39:44 UTC -- Deleting backups/tmp ...
2025-07-18 07:39:44 UTC -- Deleting backups/tmp ... done
2025-07-18 07:39:44 UTC -- 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.
2025-07-18 07:39:44 UTC -- Restore task is done.
2025-07-18 07:39:44 UTC -- Deleting backup and restore PID file ... done# 复制配置文件到容器挂载目录(宿主机操作)
cp /path/to/backup/gitlab.rb /srv/gitlab/config/
cp /path/to/backup/gitlab-secrets.json /srv/gitlab/config/# 重新配置 GitLab(容器内执行)
gitlab-ctl reconfigure# 重启 GitLab 服务(容器内执行)
gitlab-ctl restart
- 权限异常
root@2504f64e41e6:/var/opt/gitlab/backups# gitlab-backup restore BACKUP=/var/opt/gitlab/backups/20250714100001
2025-07-18 02:52:30 UTC -- Unpacking backup ...
tar: 20250714100001_gitlab_backup.tar: Cannot open: Permission denied
tar: Error is not recoverable: exiting now
2025-07-18 02:52:30 UTC -- Unpacking backup failed
2025-07-18 02:52:30 UTC -- Deleting backup and restore PID file at [/opt/gitlab/embedded/service/gitlab-rails/tmp/backup_restore.pid] ... done
root@2504f64e41e6:/var/opt/gitlab/backups# ll
total 17082276
drwx------ 2 git root 4096 Jul 18 02:26 ./
drwxr-xr-x 20 root root 4096 Jul 18 02:36 ../
-rw------- 1 root root 17492234240 Jul 18 02:29 20250714100001_gitlab_backup.tar
gitlab升级 16.1.1到18.2.0
官方升级路径
- 2 3 4 5 步骤重复执行
# 1. 停止当前容器
docker stop gitlab
docker rm gitlab# 2. 拉取 版本镜像
docker pull gitlab/gitlab-ce:16.3.9-ce.0
docker pull gitlab/gitlab-ce:16.7.10-ce.0
docker pull gitlab/gitlab-ce:16.11.10-ce.0
docker pull gitlab/gitlab-ce:17.1.8-ce.0
docker pull gitlab/gitlab-ce:17.3.7-ce.0
docker pull gitlab/gitlab-ce:17.5.5-ce.0
docker pull gitlab/gitlab-ce:17.8.7-ce.0
docker pull gitlab/gitlab-ce:17.11.6-ce.0
docker pull gitlab/gitlab-ce:18.2.0-ce.0# 3. 使用相同数据卷启动新容器
docker run -d \--hostname your.gitlab.com \--publish 443:443 --publish 80:80 --publish 22:22 \--name gitlab \--restart always \--volume /srv/gitlab/config:/etc/gitlab \--volume /srv/gitlab/logs:/var/log/gitlab \--volume /srv/gitlab/data:/var/opt/gitlab \gitlab/gitlab-ee:16.10.7-ee.0# 4. 等待启动完成后,执行重新配置(触发自动升级)
docker exec -it gitlab gitlab-ctl reconfigure
docker exec -it gitlab gitlab-ctl restart# 5. 验证升级结果
docker exec -it gitlab gitlab-rake gitlab:check SANITIZE=true