背景: 将docker部署的gitlab迁移到另一台服务器
查看gitlab版本
[root@gitlab ~]# docker exec gitlab gitlab-rake gitlab:env:info
System information
System:
Current User: git
Using RVM: no
Ruby Version: 2.7.2p137
Gem Version: 3.1.4
Bundler Version:2.1.4
Rake Version: 13.0.6
Redis Version: 6.0.14
Git Version: 2.32.0
Sidekiq Version:5.2.9
Go Version: unknown
GitLab information
Version: 14.2.3
Revision: 2324101daeb
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 12.7
URL: http://172.16.140.174
HTTP Clone URL: http://172.16.140.174/some-group/some-project.git
SSH Clone URL: git@172.16.140.174:some-group/some-project.git
Using LDAP: yes
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 13.19.1
Repository storage paths:
- default: /var/opt/gitlab/git-data/repositories
GitLab Shell path: /opt/gitlab/embedded/service/gitlab-shell
Git: /opt/gitlab/embedded/bin/git
查看映射关系
[root@gitlab ~]# docker inspect gitlab | grep Mount -A 20
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/opt/gitlab/config:/etc/gitlab",
"/opt/gitlab/logs:/var/log/gitlab",
"/opt/gitlab/data:/var/opt/gitlab/"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"22/tcp": [
{
"HostIp": "",
"HostPort": "10022"
--
"Mounts": [
{
"Type": "bind",
"Source": "/opt/gitlab/config",
"Destination": "/etc/gitlab",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/opt/gitlab/logs",
"Destination": "/var/log/gitlab",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/opt/gitlab/data",
"Destination": "/var/opt/gitlab",
1. 将旧服务器的gitlab 镜像打包导出
[root@gitlab ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
assaflavie/runlike latest e178428d4a3c 2 years ago 283MB
gitlab-backup latest 4066f53ab230 2 years ago 2.26GB
gitlab/gitlab-ce latest fe83bde77572 2 years ago 2.25GB
elleflorio/svn-server latest b8cc2d411214 2 years ago 49.7MB
[root@gitlab ~]# docker save -o gitlab.tar gitlab/gitlab-ce:latest
导入到新服务器
[root@localhost ~]# docker load -i gitlab.tar
Loaded image: gitlab/gitlab-ce:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gitlab/gitlab-ce 14.2.3-ce.0 fe83bde77572 2 years ago 2.25GB
gitlab/gitlab-ce latest fe83bde77572 2 years ago 2.25GB
2. 查看旧服务器gitlab的运行信息
[root@gitlab ~]# docker pull assaflavie/runlike:latest
latest: Pulling from assaflavie/runlike
a0d0a0d46f8b: Pull complete
14df950e5a79: Pull complete
86a31cc9e913: Pull complete
c6e88c3a8cca: Pull complete
843e00801cfc: Pull complete
8c7a43c10170: Pull complete
1b52f4e484f7: Pull complete
d2414af5b173: Pull complete
e784d6354f86: Pull complete
90bffe34dc9b: Pull complete
616b184e3902: Pull complete
Digest: sha256:a213a7d8e2a26ef0167b5d9a8d71c799cbb1fd52e350dbb69558ba3853a61889
Status: Downloaded newer image for assaflavie/runlike:latest
docker.io/assaflavie/runlike:latest
[root@gitlab ~]# alias runlike="docker run --rm -v /var/run/docker.sock:/var/run/docker.sock assaflavie/runlike"
[root@gitlab ~]# runlike gitlab
docker run --name=gitlab --hostname=a88968cb1dcc --mac-address=02:42:ac:11:00:02 --env=PATH=/opt/gitlab/embedded/bin:/opt/gitlab/bin:/assets:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin --env=LANG=C.UTF-8 --env=EDITOR=/bin/vi --env=TERM=xterm --volume=/opt/gitlab/config:/etc/gitlab --volume=/opt/gitlab/logs:/var/log/gitlab --volume=/opt/gitlab/data:/var/opt/gitlab/ --volume=/etc/gitlab --volume=/var/log/gitlab --volume=/var/opt/gitlab -p 10022:22 --expose=443 -p 80:80 --restart=always --runtime=runc --detach=true gitlab/gitlab-ce /assets/wrapper
[root@gitlab ~]# runlike -p gitlab
docker run \
--name=gitlab \
--hostname=a88968cb1dcc \
--mac-address=02:42:ac:11:00:02 \
--env=PATH=/opt/gitlab/embedded/bin:/opt/gitlab/bin:/assets:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
--env=LANG=C.UTF-8 \
--env=EDITOR=/bin/vi \
--env=TERM=xterm \
--volume=/opt/gitlab/config:/etc/gitlab \
--volume=/opt/gitlab/logs:/var/log/gitlab \
--volume=/opt/gitlab/data:/var/opt/gitlab/ \
--volume=/etc/gitlab \
--volume=/var/log/gitlab \
--volume=/var/opt/gitlab \
-p 10022:22 \
--expose=443 \
-p 80:80 \
--restart=always \
--runtime=runc \
--detach=true \
gitlab/gitlab-ce \
/assets/wrapper
在新服务器创建相同版本的gitlab容器
创建映射目录
[root@localhost opt]# mkdir gitlab
[root@localhost opt]# cd gitlab/
[root@localhost gitlab]# mkdir logs data config
新服务器运行容器
docker run \
--name=gitlab \
--hostname=172.16.140.11 \
--env=PATH=/opt/gitlab/embedded/bin:/opt/gitlab/bin:/assets:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
--env=LANG=C.UTF-8 \
--env=EDITOR=/bin/vi \
--env=TERM=xterm \
--volume=/opt/gitlab/config:/etc/gitlab \
--volume=/opt/gitlab/logs:/var/log/gitlab \
--volume=/opt/gitlab/data:/var/opt/gitlab/ \
--volume=/etc/gitlab \
--volume=/var/log/gitlab \
--volume=/var/opt/gitlab \
-p 10022:22 \
--expose=443 \
-p 80:80 \
--restart=always \
--runtime=runc \
--detach=true \
gitlab/gitlab-ce
已启动成功
[root@localhost gitlab]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
209580d3aa08 gitlab/gitlab-ce "/assets/wrapper" 9 minutes ago Up 9 minutes (healthy) 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp, 0.0.0.0:10022->22/tcp, :::10022->22/tcp gitlab
迁移备份和恢复
将旧服务器下的gitlab备份文件复制到新服务器的gitlab备份目录下
[root@localhost backups]# ll
总用量 24877100
-rw-r--r--. 1 root root 25474150400 3月 27 11:30 1711476565_2024_03_26_14.2.3_gitlab_backup.tar
[root@localhost backups]# chmod +777 1711476565_2024_03_26_14.2.3_gitlab_backup.tar
[root@localhost backups]# ll
总用量 24877100
-rwxrwxrwx. 1 root root 25474150400 3月 27 11:30 1711476565_2024_03_26_14.2.3_gitlab_backup.tar
恢复备份
进入容器
[root@localhost backups]# docker exec -it 2095 /bin/bash
停止相关数据连接服务
root@a88968cb1dcc:/var/opt/gitlab/backups# gitlab-ctl stop unicorn
root@a88968cb1dcc:/var/opt/gitlab/backups# gitlab-ctl stop sidekiq
ok: down: sidekiq: 1s, normally up
进入目录/var/opt/gitlab/backups
cd /var/opt/gitlab/backups
加载备份文件
root@a88968cb1dcc:/var/opt/gitlab/backups# gitlab-rake gitlab:backup:restore BACKUP=1711476565_2024_03_26_14.2.3
等待备份文件加载完成,中间会出现几次交互,大概意思就是加载备份会删除现有数据,是否确定,都输入yes就可以
如果遇到错误:must be owner of extension plpgsql
参考:https://www.jianshu.com/p/09a2b0c25ecd
提示:
Do you want to continue (yes/no)? yes
Deleting backups/tmp ... done
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.
Restore task is done.
需要把原来的gitlab.rb和gitlab-secrets.json 拷贝到现配置文件目录下
并修改url地址
[root@localhost config]# vim gitlab.rb
external_url 'http://172.16.140.11'
[root@localhost gitlab]# vim data/gitlab-rails/etc/gitlab.yml
gitlab:
## Web server settings (note: host is the FQDN, do not include http://)
host: 172.16.140.11
port: 80
https: false
重新加载配置
root@172:/var/opt/gitlab/backups# gitlab-ctl reconfigure
Running handlers:
Running handlers complete
Chef Infra Client finished, 30/728 resources updated in 01 minutes 35 seconds
gitlab Reconfigured!
启动服务
root@172:/var/opt/gitlab/backups# gitlab-ctl start
ok: run: alertmanager: (pid 14103) 107s
ok: run: gitaly: (pid 1198) 8221s
ok: run: gitlab-exporter: (pid 1233) 8220s
ok: run: gitlab-workhorse: (pid 14063) 108s
ok: run: grafana: (pid 14124) 106s
ok: run: logrotate: (pid 5903) 1019s
ok: run: nginx: (pid 14083) 107s
ok: run: postgres-exporter: (pid 1306) 8218s
ok: run: postgresql: (pid 1315) 8217s
ok: run: prometheus: (pid 1317) 8217s
ok: run: puma: (pid 14025) 166s
ok: run: redis: (pid 1442) 8216s
ok: run: redis-exporter: (pid 1448) 8216s
ok: run: sidekiq: (pid 14452) 0s
ok: run: sshd: (pid 1469) 8211s
测试是否成功