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