进入要备份的主机,执行备份操作命令。
[root@xtwj27 ~]# docker exec -t gitlab gitlab-backup create STRATEGY=copy
2019-12-12 02:44:13 +0000 -- Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2019-12-12 02:44:16 +0000 -- done
2019-12-12 02:44:16 +0000 -- Dumping repositories ...
* xtwj/doc/default (@hashed/4e/07/4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce) ... [DONE]
[DONE] Wiki
* xtwj/sample/spring-cloud-alibaba-sample (@hashed/7a/61/7a61b53701befdae0eeeffaecc73f14e20b537bb0f8b91ad7c2936dc63562b25) ... [DONE]
[SKIPPED] Wiki
2019-12-12 02:45:31 +0000 -- done
2019-12-12 02:45:31 +0000 -- Dumping uploads ...
2019-12-12 02:45:32 +0000 -- done
2019-12-12 02:45:32 +0000 -- Dumping builds ...
2019-12-12 02:45:32 +0000 -- done
2019-12-12 02:45:32 +0000 -- Dumping artifacts ...
2019-12-12 02:45:32 +0000 -- done
2019-12-12 02:45:32 +0000 -- Dumping pages ...
2019-12-12 02:45:32 +0000 -- done
2019-12-12 02:45:32 +0000 -- Dumping lfs objects ...
2019-12-12 02:45:32 +0000 -- done
2019-12-12 02:45:32 +0000 -- Dumping container registry images ...
2019-12-12 02:45:32 +0000 -- [DISABLED]
Creating backup archive: 1576118732_2019_12_12_12.4.0_gitlab_backup.tar ... done
Uploading backup archive to remote storage ... skipped
Deleting tmp directories ... done
done
done
done
done
done
done
done
Deleting old backups ... skipping
Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data
and are not included in this backup. You will need these files to restore a backup.
Please back them up manually.
Backup task is done.
[root@xtwj27 ~]#
备份完成以后,我们接下来需要进入gitlab容器中做如下两件事:
(1)将目录/etc/gitlab/里面文件gitlab-secrets.json和gitlab.rb传输到待恢复的目标主机上gitlab容器中相应的位置。
(2)将目录 /var/opt/gitlab/backups/里面的*_gitlab_backup.tar文件传输到待恢复的目标主机上gitlab容器中相应的位置。
这里,我们先进入源主机gitlab容器中,查看相关文件。
[root@xtwj27 ~]# docker exec -it gitlab /bin/bash
root@gitlab:/# ll /etc/gitlab/
total 136
drwxrwxr-x. 4 root root 250 Oct 31 02:27 ./
drwxr-xr-x. 1 root root 22 Oct 24 07:29 ../
-rw-------. 1 root root 15453 Oct 31 02:36 gitlab-secrets.json
-rw-------. 1 root root 96439 Oct 24 08:01 gitlab.rb
-rw-------. 1 root root 227 Oct 24 07:29 ssh_host_ecdsa_key
-rw-r--r--. 1 root root 182 Oct 24 07:29 ssh_host_ecdsa_key.pub
-rw-------. 1 root root 411 Oct 24 07:29 ssh_host_ed25519_key
-rw-r--r--. 1 root root 102 Oct 24 07:29 ssh_host_ed25519_key.pub
-rw-------. 1 root root 1679 Oct 24 07:29 ssh_host_rsa_key
-rw-r--r--. 1 root root 402 Oct 24 07:29 ssh_host_rsa_key.pub
drwxr-xr-x. 2 root root 95 Oct 24 07:59 ssl/
drwxr-xr-x. 2 root root 6 Oct 24 07:30 trusted-certs/
root@gitlab:/# ll -h /var/opt/gitlab/backups/
total 1.2G
drwx------. 2 git root 60 Dec 12 02:46 ./
drwxr-xr-x. 20 root root 4.0K Oct 31 02:36 ../
-rw-------. 1 git git 1.2G Dec 12 02:46 1576118732_2019_12_12_12.4.0_gitlab_backup.tar
root@gitlab:/# exit
exit
[root@xtwj27 ~]#
接下来,我们从容器中将相关文件复制到宿主机上。
[root@xtwj27 ~]# docker cp gitlab:/etc/gitlab/gitlab-secrets.json data/
[root@xtwj27 ~]# docker cp gitlab:/etc/gitlab/gitlab.rb data/
[root@xtwj27 data]# docker cp gitlab:/var/opt/gitlab/backups/1576118732_2019_12_12_12.4.0_gitlab_backup.tar ./
[root@xtwj27 ~]# ll data/
total 16
drwxr-xr-x. 2 root root 33 Dec 12 11:01 .
dr-xr-x---. 6 root root 203 Dec 11 17:03 ..
-rw-------. 1 root root 96439 Oct 24 08:01 gitlab.rb
-rw-------. 1 root root 15453 Oct 31 10:36 gitlab-secrets.json
[root@xtwj27 data]#root@xtwj27 data]#
传输相关备份文件到目录主机
[root@xtwj27 data]# scp gitlab-secrets.json 192.168.3.78:~/
gitlab-secrets.json 100% 15KB 2.1MB/s 00:00
[root@xtwj27 data]# scp gitlab.rb 192.168.3.78:~/
gitlab.rb 100% 94KB 2.1MB/s 00:00
[root@xtwj27 data]# scp 1576118732_2019_12_12_12.4.0_gitlab_backup.tar 192.168.3.78:~/
1576118732_2019_12_12_12.4.0_gitlab_backup.tar 100% 1146MB 10.9MB/s 01:45
[root@xtwj27 data]#
进入目标主机,将相关文件从宿主机复制到gitlab容器中。
[root@xtwj78 ~]# docker cp 1576118732_2019_12_12_12.4.0_gitlab_backup.tar gitlab:/var/opt/gitlab/backups/
[root@xtwj78 ~]#
[root@xtwj78 ~]# docker exec -it gitlab /bin/bash
root@gitlab:/# ll -h /var/opt/gitlab/backups/
total 1.2G
drwx------. 2 git root 60 Dec 12 03:36 ./
drwxr-xr-x. 20 root root 4.0K Dec 12 01:52 ../
-rw-------. 1 root root 1.2G Dec 12 03:35 1576118732_2019_12_12_12.4.0_gitlab_backup.tar
root@gitlab:/# exit
exit
[root@xtwj78 ~]# docker exec -it gitlab gitlab-backup restore
Unpacking backup ... tar: 1576118732_2019_12_12_12.4.0_gitlab_backup.tar: Cannot open: Permission denied
tar: Error is not recoverable: exiting now
unpacking backup failed
[root@xtwj78 ~]# docker exec -it gitlab /bin/bash
root@gitlab:/# ll /var/opt/gitlab/backups/
total 1173984
drwx------. 2 git root 60 Dec 12 03:36 ./
drwxr-xr-x. 20 root root 4096 Dec 12 01:52 ../
-rw-------. 1 root root 1202155520 Dec 12 03:35 1576118732_2019_12_12_12.4.0_gitlab_backup.tar
root@gitlab:/# chown git:git /var/opt/gitlab/backups/1576118732_2019_12_12_12.4.0_gitlab_backup.tar
root@gitlab:/# exit
exit
[root@xtwj78 ~]# docker exec -it gitlab gitlab-backup restore
Unpacking backup ... done
GitLab version mismatch:
Your current GitLab version (12.5.4) differs from the GitLab version in the backup!
Please switch to the following version and try again:
version: 12.4.0
Hint: git checkout v12.4.0
[root@xtwj78 ~]#
这说明,主机xtwj78上的gitlab docker image tag与主机xtwj27上的gitlab docker image tag不一致。究其原因是因为在启动docker容器的时候,我们采用如下的命令:
docker run --detach \
--publish 443:443 --publish 80:80 --publish 2222:22 \
--name gitlab \
--restart always \
--volume /opt/gitlab/config:/etc/gitlab \
--volume /opt/gitlab/logs:/var/log/gitlab \
--volume /opt/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest
而latest
始终指向最新版本的gitlab docker image tag,这就导致过一段时间以后,再次执行上述命令,就会导致安装的版本不一致。
这个时候,为了系统迁移成功,我们必须使目标主机上的gitlab docker image tag与源主机上的的gitlab docker image tag保持一致,于是,我们需要将目标主机上的gitlab docker container实例停机并删除掉,用如下命令重新启动一个实例。
[root@xtwj78 ~]# cd /opt/gitlab/
[root@xtwj78 gitlab]# rm -rf *
[root@xtwj78 gitlab]# mkdir -p config logs data
[root@xtwj78 gitlab]# cd
[root@xtwj78 ~]# tree /opt/gitlab/
/opt/gitlab/
├── config
├── data
└── logs
3 directories, 0 files
[root@xtwj78 ~]#
docker run --detach \
--publish 443:443 \
--publish 80:80 \
--publish 2222:22 \
--name gitlab \
--restart always \
--volume /opt/gitlab/config:/etc/gitlab \
--volume /opt/gitlab/logs:/var/log/gitlab \
--volume /opt/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:12.4.0-ce.0
参考文献