1.配置docker环境1.1安装docker[rootgitlab ~]# rpm -qa | grep podman[rootgitlab ~]# rpm -q podmanpackage podman is not installed[rootgitlab ~]# dnf install -y yum-utils device-mapper-persistent-data lvm2Upgraded:device-mapper-8:1.02.181-15.el8_10.x86_64 device-mapper-event-8:1.02.181-15.el8_10.x86_64 device-mapper-event-libs-8:1.02.181-15.el8_10.x86_64device-mapper-libs-8:1.02.181-15.el8_10.x86_64 dnf-plugins-core-4.0.21-25.el8.noarch lvm2-8:2.03.14-15.el8_10.x86_64lvm2-libs-8:2.03.14-15.el8_10.x86_64 python3-dnf-plugins-core-4.0.21-25.el8.noarchInstalled:python3-systemd-234-8.el8.x86_64 yum-utils-4.0.21-25.el8.noarchComplete![rootgitlab ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repoAdding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo[rootgitlab ~]# yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginInstalled:container-selinux-2:2.229.0-2.moduleel8.10.019484b5cd4a9.noarch containerd.io-1.6.32-3.1.el8.x86_64docker-buildx-plugin-0.14.0-1.el8.x86_64 docker-ce-3:26.1.3-1.el8.x86_64docker-ce-cli-1:26.1.3-1.el8.x86_64 docker-ce-rootless-extras-26.1.3-1.el8.x86_64docker-compose-plugin-2.27.0-1.el8.x86_64 fuse-overlayfs-1.13-1.moduleel8.10.019484b5cd4a9.x86_64fuse3-3.3.0-19.el8.x86_64 fuse3-libs-3.3.0-19.el8.x86_64libcgroup-0.41-19.el8.x86_64 libslirp-4.4.0-2.moduleel8.10.019484b5cd4a9.x86_64slirp4netns-1.2.3-1.moduleel8.10.019484b5cd4a9.x86_64Complete![rootgitlab ~]# systemctl enable dockerCreated symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.[rootgitlab ~]# systemctl start docker[rootgitlab ~]# docker -vDocker version 26.1.3, build b72abbb[rootgitlab ~]# vim /etc/docker/daemon.json{builder: {gc: {defaultKeepStorage: 20GB,enabled: true}},experimental: false,features: {buildkit: true},live-restore: true,registry-mirrors: [https://docker.211678.top,https://docker.1panel.live,https://hub.rat.dev,https://docker.m.daocloud.io,https://do.nark.eu.org,https://dockerpull.com,https://dockerproxy.cn,https://docker.awsl9527.cn/]}[rootgitlab ~]# systemctl daemon-reload systemctl restart docker1.2安装docker-compose[rootgitlab ~]# curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose[rootgitlab ~]# chmod x /usr/local/bin/docker-compose[rootgitlab ~]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose[rootgitlab ~]# docker-compose --versionDocker Compose version v2.33.12.docker安装gitlab2.1下载gitlab镜像[rootgitlab ~]# docker pull gitlab/gitlab-ce:12.5.2-ce.012.5.2-ce.0: Pulling from gitlab/gitlab-ce976a760c94fc: Pull completec58992f3c37b: Pull complete0ca0e5e7f12e: Pull completef2a274cc00ca: Pull complete8e1658b69e6f: Pull complete9fd149f4f0a7: Pull completeedaf9ce4564c: Pull completed24a22750244: Pull completea7a54e11cce8: Pull complete34ffe09b108b: Pull completeDigest: sha256:40826e1f9b3c1ce3c83a90b64ea9dc27d33a08f6687ca35081d11c6ed3b57ce6Status: Downloaded newer image for gitlab/gitlab-ce:12.5.2-ce.0docker.io/gitlab/gitlab-ce:12.5.2-ce.0[rootgitlab ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEgitlab/gitlab-ce 12.5.2-ce.0 a968f7f66f71 5 years ago 1.82GB2.2安装gitlab[rootgitlab ~]# mkdir -p /data/gitlab/config[rootgitlab ~]# mkdir -p /data/gitlab/logs[rootgitlab ~]# mkdir -p /data/gitlab/data[rootgitlab ~]# mkdir -p /data/gitlab/backups[rootgitlab ~]# vim docker-compose.ymlversion: 3.4services:gitlab:image: gitlab/gitlab-ce:12.5.2-ce.0restart: alwayshostname: gitlabenvironment:GITLAB_OMNIBUS_CONFIG: |external_url http://10.9.254.40gitlab_rails[gitlab_shell_ssh_port] 43215 # 可选如果你需要更改SSH端口ports:- 80:80 # HTTP端口映射- 443:443 # HTTPS端口映射可选- 43215:22 # SSH端口映射如果你更改了默认SSH端口volumes:- /data/gitlab/config:/etc/gitlab- /data/gitlab/logs:/var/log/gitlab- /data/gitlab/data:/var/opt/gitlab- /data/gitlab/backups:/var/opt/gitlab/backups[rootgitlab ~]# docker-compose up -d[] Running 2/2✔ Network root_default Created 0.3s✔ Container root-gitlab-1 Started[rootgitlab ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1afdd2bc72bb gitlab/gitlab-ce:12.5.2-ce.0 /assets/wrapper 28 seconds ago Up 26 seconds (health: starting) 0.0.0.0:80-80/tcp, :::80-80/tcp, 0.0.0.0:443-443/tcp, :::443-443/tcp, 0.0.0.0:43215-22/tcp, :::43215-22/tcp root-gitlab-12.3登录gitlabhttp://10.9.254.40/3.备份gitlab数据3.1停止gitlab容器在进行备份之前最好先停止 GitLab 容器以确保数据一致性[rootgitlab ~]# docker stop root-gitlab-1root-gitlab-1[rootgitlab ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES673bf507dbf6 gitlab/gitlab-ce:12.5.2-ce.0 /assets/wrapper 10 minutes ago Exited (0) 16 seconds ago root-gitlab-13.2创建备份启动 GitLab 容器并执行备份命令[rootgitlab ~]# docker start root-gitlab-1root-gitlab-1[rootgitlab ~]# docker exec -t root-gitlab-1 gitlab-backup create2025-04-17 03:40:34 0000 -- Dumping database ...Dumping PostgreSQL database gitlabhq_production ... [DONE]2025-04-17 03:40:36 0000 -- done2025-04-17 03:40:36 0000 -- Dumping repositories ...* root/etc- (hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b) ... [DONE][SKIPPED] Wiki2025-04-17 03:40:36 0000 -- done2025-04-17 03:40:36 0000 -- Dumping uploads ...2025-04-17 03:40:37 0000 -- done2025-04-17 03:40:37 0000 -- Dumping builds ...2025-04-17 03:40:37 0000 -- done2025-04-17 03:40:37 0000 -- Dumping artifacts ...2025-04-17 03:40:37 0000 -- done2025-04-17 03:40:37 0000 -- Dumping pages ...2025-04-17 03:40:37 0000 -- done2025-04-17 03:40:37 0000 -- Dumping lfs objects ...2025-04-17 03:40:37 0000 -- done2025-04-17 03:40:37 0000 -- Dumping container registry images ...2025-04-17 03:40:37 0000 -- [DISABLED]Creating backup archive: 1744861237_2025_04_17_12.5.2_gitlab_backup.tar ... doneUploading backup archive to remote storage ... skippedDeleting tmp directories ... donedonedonedonedonedonedonedoneDeleting old backups ... skippingWarning: Your gitlab.rb and gitlab-secrets.json files contain sensitive dataand are not included in this backup. You will need these files to restore a backup.Please back them up manually.Backup task is done.[rootgitlab ~]# ll /data/gitlab/backups/-rw------- 1 polkitd render 153600 Apr 17 11:40 1744861237_2025_04_17_12.5.2_gitlab_backup.tar[rootgitlab ~]# mkdir -p /backup/gitlab/data[rootgitlab ~]# mkdir -p /backup/gitlab/file[rootgitlab ~]# mkdir -p /backup/gitlab/config3.3将备份文件复制到主机使用 docker cp 命令将备份文件从容器拷贝到主机[rootgitlab ~]# docker cp root-gitlab-1:/var/opt/gitlab/backups/1744861237_2025_04_17_12.5.2_gitlab_backup.tar /backup/gitlab/data/Successfully copied 155kB to /backup/gitlab/data/[rootgitlab ~]# ll /backup/gitlab/data/-rw------- 1 root root 153600 Apr 17 11:40 1744861237_2025_04_17_12.5.2_gitlab_backup.tar3.4备份外部文件[rootgitlab ~]# docker cp root-gitlab-1:/var/opt/gitlab/gitlab-rails/shared /backup/gitlab/file/Successfully copied 5.63kB to /backup/gitlab/file/[rootgitlab ~]# ll /backup/gitlab/file/drwxr-x--x 10 root root 141 Apr 17 10:54 shared3.5备份配置文件[rootgitlab ~]# docker cp root-gitlab-1:/etc/gitlab /backup/gitlab/config/Successfully copied 125kB to /backup/gitlab/config/[rootgitlab ~]# ll /backup/gitlab/config/drwxrwxr-x 3 root root 4096 Apr 17 11:21 gitlab3.6启动gitlab备份完成后重新启动 GitLab 容器[rootgitlab ~]# docker start root-gitlab-1root-gitlab-14.恢复gitlab数据删除原有项目进行恢复测试4.1将备份文件拷贝到容器[rootgitlab ~]# docker cp /backup/gitlab/data/1744861237_2025_04_17_12.5.2_gitlab_backup.tar root-gitlab-1:/var/opt/gitlab/backups/Successfully copied 155kB to root-gitlab-1:/var/opt/gitlab/backups/[rootgitlab ~]# ll /data/gitlab/backups/-rw------- 1 root root 153600 Apr 17 11:40 1744861237_2025_04_17_12.5.2_gitlab_backup.tar4.2恢复备份启动 GitLab 容器并执行恢复命令[rootgitlab ~]# ll /data/gitlab/backups/-rw------- 1 root root 153600 Apr 17 11:40 1744861237_2025_04_17_12.5.2_gitlab_backup.tar[rootgitlab ~]# chmod 755 /data/gitlab/backups/1744861237_2025_04_17_12.5.2_gitlab_backup.tar[rootgitlab ~]# docker exec -it root-gitlab-1 gitlab-backup restore BACKUP1744861237_2025_04_17_12.5.2This 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)? yesDeleting tmp directories ... donedonedonedonedonedonedonedoneWarning: Your gitlab.rb and gitlab-secrets.json files contain sensitive dataand are not included in this backup. You will need to restore these files manually.Restore task is done.4.3恢复外部文件使用 docker cp 命令恢复外部文件[rootgitlab ~]# docker cp /backup/gitlab/file/shared root-gitlab-1:/var/opt/gitlab/gitlab-rails/Successfully copied 5.63kB to root-gitlab-1:/var/opt/gitlab/gitlab-rails/4.4恢复配置文件同样使用 docker cp 命令恢复配置文件[rootgitlab ~]# docker cp /backup/gitlab/config/gitlab root-gitlab-1:/etc/Successfully copied 125kB to root-gitlab-1:/etc/4.5重新配置并启动gitlab在恢复完成后重新配置 GitLab 并启动服务[rootgitlab ~]# docker exec -it root-gitlab-1 gitlab-ctl reconfigureRunning handlers:Running handlers completeChef Client finished, 23/683 resources updated in 15 secondsgitlab Reconfigured![rootgitlab ~]# docker restart root-gitlab-1root-gitlab-15.定时备份为了实现定时备份可以创建一个定时任务cron job定期执行上述备份命令。以下是一个示例脚本你可以根据需要进行修改并将其添加到 crontab 中。[rootgitlab ~]# mkdir script[rootgitlab ~]# vim script/gitlab_backup.sh#!/bin/bash# 停止 GitLab 容器#docker stop root-gitlab-1# 创建备份docker exec -t root-gitlab-1 gitlab-backup create# 将备份文件拷贝到主机backup_file$(docker exec root-gitlab-1 ls /var/opt/gitlab/backups | grep $(date %Y_%m_%d) | head -n 1)docker cp root-gitlab-1:/var/opt/gitlab/backups/$backup_file /backup/gitlab/data/# 备份外部文件docker cp root-gitlab-1:/var/opt/gitlab/gitlab-rails/shared /backup/gitlab/file/# 备份配置文件docker cp root-gitlab-1:/etc/gitlab /backup/gitlab/config/# 启动 GitLab 容器#docker start root-gitlab-1[rootgitlab ~]# chmod x /root/script/gitlab_backup.sh[rootgitlab ~]# crontab -e0 2 * * * /root/script/gitlab_backup.sh[rootgitlab ~]# systemctl restart crond[rootgitlab ~]# systemctl enable crond
gitlab-在rockylinux8上docker安装-备份-恢复
1.配置docker环境1.1安装docker[rootgitlab ~]# rpm -qa | grep podman[rootgitlab ~]# rpm -q podmanpackage podman is not installed[rootgitlab ~]# dnf install -y yum-utils device-mapper-persistent-data lvm2Upgraded:device-mapper-8:1.02.181-15.el8_10.x86_64 device-mapper-event-8:1.02.181-15.el8_10.x86_64 device-mapper-event-libs-8:1.02.181-15.el8_10.x86_64device-mapper-libs-8:1.02.181-15.el8_10.x86_64 dnf-plugins-core-4.0.21-25.el8.noarch lvm2-8:2.03.14-15.el8_10.x86_64lvm2-libs-8:2.03.14-15.el8_10.x86_64 python3-dnf-plugins-core-4.0.21-25.el8.noarchInstalled:python3-systemd-234-8.el8.x86_64 yum-utils-4.0.21-25.el8.noarchComplete![rootgitlab ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repoAdding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo[rootgitlab ~]# yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginInstalled:container-selinux-2:2.229.0-2.moduleel8.10.019484b5cd4a9.noarch containerd.io-1.6.32-3.1.el8.x86_64docker-buildx-plugin-0.14.0-1.el8.x86_64 docker-ce-3:26.1.3-1.el8.x86_64docker-ce-cli-1:26.1.3-1.el8.x86_64 docker-ce-rootless-extras-26.1.3-1.el8.x86_64docker-compose-plugin-2.27.0-1.el8.x86_64 fuse-overlayfs-1.13-1.moduleel8.10.019484b5cd4a9.x86_64fuse3-3.3.0-19.el8.x86_64 fuse3-libs-3.3.0-19.el8.x86_64libcgroup-0.41-19.el8.x86_64 libslirp-4.4.0-2.moduleel8.10.019484b5cd4a9.x86_64slirp4netns-1.2.3-1.moduleel8.10.019484b5cd4a9.x86_64Complete![rootgitlab ~]# systemctl enable dockerCreated symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.[rootgitlab ~]# systemctl start docker[rootgitlab ~]# docker -vDocker version 26.1.3, build b72abbb[rootgitlab ~]# vim /etc/docker/daemon.json{builder: {gc: {defaultKeepStorage: 20GB,enabled: true}},experimental: false,features: {buildkit: true},live-restore: true,registry-mirrors: [https://docker.211678.top,https://docker.1panel.live,https://hub.rat.dev,https://docker.m.daocloud.io,https://do.nark.eu.org,https://dockerpull.com,https://dockerproxy.cn,https://docker.awsl9527.cn/]}[rootgitlab ~]# systemctl daemon-reload systemctl restart docker1.2安装docker-compose[rootgitlab ~]# curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose[rootgitlab ~]# chmod x /usr/local/bin/docker-compose[rootgitlab ~]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose[rootgitlab ~]# docker-compose --versionDocker Compose version v2.33.12.docker安装gitlab2.1下载gitlab镜像[rootgitlab ~]# docker pull gitlab/gitlab-ce:12.5.2-ce.012.5.2-ce.0: Pulling from gitlab/gitlab-ce976a760c94fc: Pull completec58992f3c37b: Pull complete0ca0e5e7f12e: Pull completef2a274cc00ca: Pull complete8e1658b69e6f: Pull complete9fd149f4f0a7: Pull completeedaf9ce4564c: Pull completed24a22750244: Pull completea7a54e11cce8: Pull complete34ffe09b108b: Pull completeDigest: sha256:40826e1f9b3c1ce3c83a90b64ea9dc27d33a08f6687ca35081d11c6ed3b57ce6Status: Downloaded newer image for gitlab/gitlab-ce:12.5.2-ce.0docker.io/gitlab/gitlab-ce:12.5.2-ce.0[rootgitlab ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEgitlab/gitlab-ce 12.5.2-ce.0 a968f7f66f71 5 years ago 1.82GB2.2安装gitlab[rootgitlab ~]# mkdir -p /data/gitlab/config[rootgitlab ~]# mkdir -p /data/gitlab/logs[rootgitlab ~]# mkdir -p /data/gitlab/data[rootgitlab ~]# mkdir -p /data/gitlab/backups[rootgitlab ~]# vim docker-compose.ymlversion: 3.4services:gitlab:image: gitlab/gitlab-ce:12.5.2-ce.0restart: alwayshostname: gitlabenvironment:GITLAB_OMNIBUS_CONFIG: |external_url http://10.9.254.40gitlab_rails[gitlab_shell_ssh_port] 43215 # 可选如果你需要更改SSH端口ports:- 80:80 # HTTP端口映射- 443:443 # HTTPS端口映射可选- 43215:22 # SSH端口映射如果你更改了默认SSH端口volumes:- /data/gitlab/config:/etc/gitlab- /data/gitlab/logs:/var/log/gitlab- /data/gitlab/data:/var/opt/gitlab- /data/gitlab/backups:/var/opt/gitlab/backups[rootgitlab ~]# docker-compose up -d[] Running 2/2✔ Network root_default Created 0.3s✔ Container root-gitlab-1 Started[rootgitlab ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1afdd2bc72bb gitlab/gitlab-ce:12.5.2-ce.0 /assets/wrapper 28 seconds ago Up 26 seconds (health: starting) 0.0.0.0:80-80/tcp, :::80-80/tcp, 0.0.0.0:443-443/tcp, :::443-443/tcp, 0.0.0.0:43215-22/tcp, :::43215-22/tcp root-gitlab-12.3登录gitlabhttp://10.9.254.40/3.备份gitlab数据3.1停止gitlab容器在进行备份之前最好先停止 GitLab 容器以确保数据一致性[rootgitlab ~]# docker stop root-gitlab-1root-gitlab-1[rootgitlab ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES673bf507dbf6 gitlab/gitlab-ce:12.5.2-ce.0 /assets/wrapper 10 minutes ago Exited (0) 16 seconds ago root-gitlab-13.2创建备份启动 GitLab 容器并执行备份命令[rootgitlab ~]# docker start root-gitlab-1root-gitlab-1[rootgitlab ~]# docker exec -t root-gitlab-1 gitlab-backup create2025-04-17 03:40:34 0000 -- Dumping database ...Dumping PostgreSQL database gitlabhq_production ... [DONE]2025-04-17 03:40:36 0000 -- done2025-04-17 03:40:36 0000 -- Dumping repositories ...* root/etc- (hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b) ... [DONE][SKIPPED] Wiki2025-04-17 03:40:36 0000 -- done2025-04-17 03:40:36 0000 -- Dumping uploads ...2025-04-17 03:40:37 0000 -- done2025-04-17 03:40:37 0000 -- Dumping builds ...2025-04-17 03:40:37 0000 -- done2025-04-17 03:40:37 0000 -- Dumping artifacts ...2025-04-17 03:40:37 0000 -- done2025-04-17 03:40:37 0000 -- Dumping pages ...2025-04-17 03:40:37 0000 -- done2025-04-17 03:40:37 0000 -- Dumping lfs objects ...2025-04-17 03:40:37 0000 -- done2025-04-17 03:40:37 0000 -- Dumping container registry images ...2025-04-17 03:40:37 0000 -- [DISABLED]Creating backup archive: 1744861237_2025_04_17_12.5.2_gitlab_backup.tar ... doneUploading backup archive to remote storage ... skippedDeleting tmp directories ... donedonedonedonedonedonedonedoneDeleting old backups ... skippingWarning: Your gitlab.rb and gitlab-secrets.json files contain sensitive dataand are not included in this backup. You will need these files to restore a backup.Please back them up manually.Backup task is done.[rootgitlab ~]# ll /data/gitlab/backups/-rw------- 1 polkitd render 153600 Apr 17 11:40 1744861237_2025_04_17_12.5.2_gitlab_backup.tar[rootgitlab ~]# mkdir -p /backup/gitlab/data[rootgitlab ~]# mkdir -p /backup/gitlab/file[rootgitlab ~]# mkdir -p /backup/gitlab/config3.3将备份文件复制到主机使用 docker cp 命令将备份文件从容器拷贝到主机[rootgitlab ~]# docker cp root-gitlab-1:/var/opt/gitlab/backups/1744861237_2025_04_17_12.5.2_gitlab_backup.tar /backup/gitlab/data/Successfully copied 155kB to /backup/gitlab/data/[rootgitlab ~]# ll /backup/gitlab/data/-rw------- 1 root root 153600 Apr 17 11:40 1744861237_2025_04_17_12.5.2_gitlab_backup.tar3.4备份外部文件[rootgitlab ~]# docker cp root-gitlab-1:/var/opt/gitlab/gitlab-rails/shared /backup/gitlab/file/Successfully copied 5.63kB to /backup/gitlab/file/[rootgitlab ~]# ll /backup/gitlab/file/drwxr-x--x 10 root root 141 Apr 17 10:54 shared3.5备份配置文件[rootgitlab ~]# docker cp root-gitlab-1:/etc/gitlab /backup/gitlab/config/Successfully copied 125kB to /backup/gitlab/config/[rootgitlab ~]# ll /backup/gitlab/config/drwxrwxr-x 3 root root 4096 Apr 17 11:21 gitlab3.6启动gitlab备份完成后重新启动 GitLab 容器[rootgitlab ~]# docker start root-gitlab-1root-gitlab-14.恢复gitlab数据删除原有项目进行恢复测试4.1将备份文件拷贝到容器[rootgitlab ~]# docker cp /backup/gitlab/data/1744861237_2025_04_17_12.5.2_gitlab_backup.tar root-gitlab-1:/var/opt/gitlab/backups/Successfully copied 155kB to root-gitlab-1:/var/opt/gitlab/backups/[rootgitlab ~]# ll /data/gitlab/backups/-rw------- 1 root root 153600 Apr 17 11:40 1744861237_2025_04_17_12.5.2_gitlab_backup.tar4.2恢复备份启动 GitLab 容器并执行恢复命令[rootgitlab ~]# ll /data/gitlab/backups/-rw------- 1 root root 153600 Apr 17 11:40 1744861237_2025_04_17_12.5.2_gitlab_backup.tar[rootgitlab ~]# chmod 755 /data/gitlab/backups/1744861237_2025_04_17_12.5.2_gitlab_backup.tar[rootgitlab ~]# docker exec -it root-gitlab-1 gitlab-backup restore BACKUP1744861237_2025_04_17_12.5.2This 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)? yesDeleting tmp directories ... donedonedonedonedonedonedonedoneWarning: Your gitlab.rb and gitlab-secrets.json files contain sensitive dataand are not included in this backup. You will need to restore these files manually.Restore task is done.4.3恢复外部文件使用 docker cp 命令恢复外部文件[rootgitlab ~]# docker cp /backup/gitlab/file/shared root-gitlab-1:/var/opt/gitlab/gitlab-rails/Successfully copied 5.63kB to root-gitlab-1:/var/opt/gitlab/gitlab-rails/4.4恢复配置文件同样使用 docker cp 命令恢复配置文件[rootgitlab ~]# docker cp /backup/gitlab/config/gitlab root-gitlab-1:/etc/Successfully copied 125kB to root-gitlab-1:/etc/4.5重新配置并启动gitlab在恢复完成后重新配置 GitLab 并启动服务[rootgitlab ~]# docker exec -it root-gitlab-1 gitlab-ctl reconfigureRunning handlers:Running handlers completeChef Client finished, 23/683 resources updated in 15 secondsgitlab Reconfigured![rootgitlab ~]# docker restart root-gitlab-1root-gitlab-15.定时备份为了实现定时备份可以创建一个定时任务cron job定期执行上述备份命令。以下是一个示例脚本你可以根据需要进行修改并将其添加到 crontab 中。[rootgitlab ~]# mkdir script[rootgitlab ~]# vim script/gitlab_backup.sh#!/bin/bash# 停止 GitLab 容器#docker stop root-gitlab-1# 创建备份docker exec -t root-gitlab-1 gitlab-backup create# 将备份文件拷贝到主机backup_file$(docker exec root-gitlab-1 ls /var/opt/gitlab/backups | grep $(date %Y_%m_%d) | head -n 1)docker cp root-gitlab-1:/var/opt/gitlab/backups/$backup_file /backup/gitlab/data/# 备份外部文件docker cp root-gitlab-1:/var/opt/gitlab/gitlab-rails/shared /backup/gitlab/file/# 备份配置文件docker cp root-gitlab-1:/etc/gitlab /backup/gitlab/config/# 启动 GitLab 容器#docker start root-gitlab-1[rootgitlab ~]# chmod x /root/script/gitlab_backup.sh[rootgitlab ~]# crontab -e0 2 * * * /root/script/gitlab_backup.sh[rootgitlab ~]# systemctl restart crond[rootgitlab ~]# systemctl enable crond