环境:
旧服务器:192.168.0.231
新服务器:192.168.0.31
一、旧服务器数据备份 192.168.0.231
1、备份镜像
[root@host231 otoyix]# cd /data/soft/
[root@host231 otoyix]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3eecbb6b6e14 gitlab:v2.0 "/assets/wrapper" 6 months ago Up 17 hours (healthy) 443/tcp, 0.0.0.0:64222->22/tcp, 0.0.0.0:64280->80/tcp gitlab_new
[root@host231 soft]# docker images|grep v2.0
gitlab v2.0 43f8b1f97c2e 6 months ago 1.34 GB
[root@host231 soft]# docker save gitlab:v2.0 >gitlab.tar
[root@host231 soft]# ls
gitlab.tar
2、gitlab-rake 备份gitlab_backup.tar文件
[root@host231 soft]# docker exec -it 3eecbb6b6e14 /bin/bash
root@3eecbb6b6e14:/# cd /var/opt/gitlab/backups/
root@3eecbb6b6e14:/var/opt/gitlab/backups# gitlab-rake gitlab:backup:create
root@3eecbb6b6e14:/var/opt/gitlab/backups# ls /usr/local/backup/
1605237472_2020_11_13_10.8.1_gitlab_backup.tar
传输到新服务器
root@3eecbb6b6e14:/usr/local/backup# scp 1605237472_2020_11_13_10.8.1_gitlab_backup.tar root@192.168.0.31:/data/soft/
3、备份配置文件
[root@host231 soft]# docker exec -it 3eecbb6b6e14 /bin/bash
root@3eecbb6b6e14:/# cd /etc/gitlab
root@3eecbb6b6e14:/etc/gitlab# ls gitlab*
gitlab-secrets.json gitlab.rbr 备份这两个文件
root@3eecbb6b6e14:/etc/gitlab# scp gitlab* root@192.168.0.31:/data/soft
root@192.168.0.31's password:
gitlab-secrets.json 100% 15KB 14.6KB/s 00:00
gitlab.rb 100% 79KB 79.3KB/s 00:00
二、新服务器安装配置docker及创建Gitlab
1、安装docker
基础环境
[root@localhost ~]# yum install vim wget lrzsz bash-completion -y 新服务器习惯性安装软件
换源
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache
安装docker
[root@localhost ~]# yum install docker* -y
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# ifconfig docker0
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
ether 02:42:3f:3f:b7:ad txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
添加国内加速源
[root@localhost soft]# vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
[root@localhost soft]# systemctl restart docker
2、导入Gitlab镜像
从旧服务器中得到的镜像导入到本服务器仓库中
[root@localhost soft]# docker load < gitlab.tar
c8aa3ff3c3d3: Loading layer [==================================================>] 117 MB/117 MB
82718dbf791d: Loading layer [==================================================>] 15.87 kB/15.87 kB
3a0404adc8bd: Loading layer [==================================================>] 14.85 kB/14.85 kB
cd7b4cc1c2dd: Loading layer [==================================================>] 5.632 kB/5.632 kB
bf3d982208f5: Loading layer [==================================================>] 3.072 kB/3.072 kB
91d31d71195d: Loading layer [==================================================>] 75.85 MB/75.85 MB
74b365ee3929: Loading layer [==================================================>] 2.048 kB/2.048 kB
5a4c280cb0a8: Loading layer [==================================================>] 2.048 kB/2.048 kB
278c9ddc7b34: Loading layer [==================================================>] 2.048 kB/2.048 kB
076f3b9b42f4: Loading layer [==================================================>] 15.87 kB/15.87 kB
49d5def88499: Loading layer [==================================================>] 1.214 GB/1.214 GB
cb82f5d3d3cf: Loading layer [==================================================>] 2.376 MB/2.376 MB
Loaded image: gitlab:v2.0
[root@localhost soft]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gitlab v2.0 43f8b1f97c2e 6 months ago 1.34 GB
3、创建Gitlab容器
宿主机创建Gitlab挂载目录
[root@localhost ~]# mkdir -p /data/gitlab/etc
[root@localhost ~]# mkdir -p /data/gitlab/log
[root@localhost ~]# mkdir -p /data/gitlab/opt
创建Gitlab容器
[root@localhost ~]# docker run -itd \
--privileged=true \
--publish 64222:22 \
--publish 8443:443 \
--publish 64280:80 \
--name gitlab_new \
--restart unless-stopped \
-v /data/gitlab/etc:/etc/gitlab \
-v /data/gitlab/log:/var/log/gitlab \
-v /data/gitlab/opt:/var/opt/gitlab \
-v /data/gitlab/backup:/usr/local/backup \
gitlab:v2.0
291821c7c49cef283e258db30f013103fa21f6a7c705b137aa83105065c4e676
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
291821c7c49c gitlab:v2.0 "/assets/wrapper" 6 seconds ago Up 5 seconds (health: starting) 0.0.0.0:64222->22/tcp, 0.0.0.0:64280->80/tcp, 0.0.0.0:8443->443/tcp gitlab_new
[root@localhost ~]#
4、还原相关数据,配置数据及备份gitlab_backup.tar文件
[root@localhost soft]# \cp gitlab* /data/gitlab/etc/
gitlab.rb ssh_host_ecdsa_key.pub ssh_host_rsa_key
gitlab-secrets.json ssh_host_ed25519_key ssh_host_rsa_key.pub
ssh_host_ecdsa_key ssh_host_ed25519_key.pub trusted-certs/
[root@localhost soft]# \cp gitlab* /data/gitlab/etc/
[root@localhost soft]# cp 1605237472_2020_11_13_10.8.1_gitlab_backup.tar /data/gitlab/opt/backups/
改容器配置文件,在宿主机上 直接改,改成
vim /data/gitlab/etc/gitlab.rb
[root@localhost ~]# vim /data/gitlab/etc/gitlab.rb
external_url 'http://192.168.0.31'
vim /data/gitlab/opt/gitlab-rails/etc/gitlab.yml
[root@localhost ~]# vim /data/gitlab/opt/gitlab-rails/etc/gitlab.yml
gitlab:
## Web server settings (note: host is the FQDN, do not include http://)
host: 192.168.0.31
port: 80
https: false
5、删除及重建容器
删除容器
[root@localhost soft]# docker stop gitlab_new
gitlab_new
[root@localhost soft]# docker ps -qa
146feb57c39b
[root@localhost soft]# docker rm 146feb57c39b
146feb57c39b
重新创建Gitlab容器
[root@localhost soft]# docker run -itd --privileged=true --publish 64222:22 --publish 8443:443 --publish 64280:80 --name gitlab_new --restart unless-stopped -v /data/gitlab/etc:/etc/gitlab -v /data/gitlab/log:/var/log/gitlab -v /data/gitlab/opt:/var/opt/gitlab gitlab:v2.0
ec9d3c9f25bcf1597dca23ca22b97c6fab22495c1712c24fa8d033824662ec2a
[root@localhost soft]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ec9d3c9f25bc gitlab:v2.0 "/assets/wrapper" 4 seconds ago Up 4 seconds (health: starting) 0.0.0.0:64222->22/tcp, 0.0.0.0:64280->80/tcp, 0.0.0.0:8443->443/tcp gitlab_new
[root@localhost soft]#
6、进入容器,导入gitlab_backup.tar数据
[root@localhost soft]# docker exec -it ec9d3c9f25bc /bin/bash
root@ec9d3c9f25bc:/#
停止相关服务连接
root@ec9d3c9f25bc:/# gitlab-ctl stop unicorn
root@ec9d3c9f25bc:/# gitlab-ctl stop sidekiq
导入
root@ec9d3c9f25bc:/# cd /var/opt/gitlab/backups/
root@ec9d3c9f25bc:/var/opt/gitlab/backups# ls
1605237472_2020_11_13_10.8.1_gitlab_backup.tar
root@ec9d3c9f25bc:/var/opt/gitlab/backups# gitlab-rake gitlab:backup:restore BACKUP=1605237472_2020_11_13_10.8.1 这里不用写gitlab_backup.tar,到数字为止
No backups found in /usr/local/backup
Please make sure that file name ends with _gitlab_backup.tar
提示目录不对,将文件放到/usr/local/backup中,再次导入
root@ec9d3c9f25bc:/var/opt/gitlab/backups# cp 1605237472_2020_11_13_10.8.1_gitlab_backup.tar /usr/local/backup/
添加权限
root@ec9d3c9f25bc:/var/opt/gitlab/backups# chmod 777 /usr/local/backup/1605237472_2020_11_13_10.8.1_gitlab_backup.tar
导入
root@ec9d3c9f25bc:/var/opt/gitlab/backups# gitlab-rake gitlab:backup:restore BACKUP=1605237472_2020_11_13_10.8.1
Unpacking backup ...
Unpacking backup ... done
Before 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)? yes
Removing all tables. Press `Ctrl-C` within 5 seconds to abort
......
This will rebuild an authorized_keys file.
You will lose any data stored in authorized_keys file.
Do you want to continue (yes/no)? yes
......................................................
Deleting tmp directories ... done
done
done
done
done
done
done
done
root@ec9d3c9f25bc:/var/opt/gitlab/backups#
重新加载配置
root@ec9d3c9f25bc:/var/opt/gitlab/backups# gitlab-ctl reconfigure
......
Running handlers:
Running handlers complete
Chef Client finished, 2/532 resources updated in 09 seconds
gitlab Reconfigured!
启动服务
root@ec9d3c9f25bc:/var/opt/gitlab/backups# gitlab-ctl start
ok: run: alertmanager: (pid 494) 1421s
ok: run: gitaly: (pid 536) 1421s
ok: run: gitlab-monitor: (pid 532) 1421s
ok: run: gitlab-workhorse: (pid 518) 1421s
ok: run: logrotate: (pid 534) 1421s
ok: run: nginx: (pid 537) 1421s
ok: run: node-exporter: (pid 499) 1421s
ok: run: postgres-exporter: (pid 513) 1421s
ok: run: postgresql: (pid 505) 1421s
ok: run: prometheus: (pid 497) 1421s
ok: run: redis: (pid 510) 1421s
ok: run: redis-exporter: (pid 511) 1421s
ok: run: sidekiq: (pid 5787) 0s
ok: run: sshd: (pid 39) 1437s
ok: run: unicorn: (pid 5791) 1s
root@ec9d3c9f25bc:/var/opt/gitlab/backups#
浏览器测试
查看项目是否存在
注:
1、如浏览器报502错误,先关闭容器,重启系统即可
2、crontab定时备份时,可以使用
0 0 * * * docker exec -i gitlab_new sh /data/sh/bak_gitlab_code.sh
这里的exec 不能加t
-----------------end