前提:迁移机器和被迁移机器的gitlab版本必须一致。
浏览器输入:http://localhost/help即可查看
或者点击这个小扳手
1、Gitlab 创建备份
1.1 创建备份文件
首先需要把老服务器上的Gitlab整体备份,使用Gitlab一键安装包安装Gitlab非常简单, 同样的备份恢复与迁移也比较简单,使用一条命令即可创建完整的Gitlab备份。
gitlab-rake gitlab:backup:create
使用以上命令会在/var/opt/gitlab/backups目录下创建一个名称类似为1624434930_2021_06_23_13.12.3_gitlab_backup.tar的压缩包, 这个压缩包就是Gitlab整个的完整部分, 其中开头的1624434930_2021_06_23_13.12.3是备份创建的日期和gitlab的版本号
/etc/gitlab/gitlab.rb 配置文件须备份
/var/opt/gitlab/nginx/conf nginx.conf配置文件
/etc/postfix/main.cf 邮件配置备份
# 更改Gitlab备份目录
通过修改/etc/gitlab/gitlab.rb配置文件来修改默认存放备份文件的目录
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
默认是带有#的,表示默认的位置为:/var/opt/gitlab/backups修改为你想存放备份的目录即可,去掉#后修改为想要的位置即可,如下:
gitlab_rails['backup_path'] = '/mnt/backups'
# 修改完成后重载配置文件即可:
gitlab-ctl reconfigure
2、 Gitlab迁移
将gitlab老服务器上生成的备份文件想办法拷贝到gitlab新服务器上,这里我是用的scp命令,第一次执行会提示是否连接,输入yes即可。
[root@localhost ~]# scp /var/opt/gitlab/backups/1624436184_2021_06_23_13.12.3_gitlab_backup.tar root@192.168.6.12:/var/opt/gitlab/backups/
The authenticity of host '192.168.6.12 (192.168.6.12)' can't be established.
ECDSA key fingerprint is SHA256:Q3fO68CJgJqgqDRcqp2Hxm/T0eUMa06PxbBBhf7ZK94.
ECDSA key fingerprint is MD5:24:5f:f9:9c:f6:23:4b:12:43:24:d1:fc:7f:53:07:23.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.6.12' (ECDSA) to the list of known hosts.
root@192.168.6.12's password:
1624436184_2021_06_23_13.12.3_gitlab_backup.tar 100% 8951MB 63.0MB/s 02:22
复制完成后到新机器上查看:
[root@localhost ~]# clear
[root@localhost ~]# cd /var/opt/gitlab/backups/
[root@localhost backups]# ls
1624436184_2021_06_23_13.12.3_gitlab_backup.tar
这时候再授予该压缩文件可执行权:
chmod 755 1624436184_2021_06_23_13.12.3_gitlab_backup.tar
3、执行命令停止相关数据连接服务
# 停止相关数据连接服务
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
4、执行命令从备份文件中恢复Gitlab
执行命令从备份文件中恢复Gitlab;格式如下:gitlab-rake gitlab:backup:restore BACKUP=备份文件编号
[root@localhost backups]# gitlab-rake gitlab:backup:restore BACKUP=1624436184_2021_06_23_13.12.3
敲完命令后,出现第一个交互页面,
[root@localhost backups]# gitlab-rake gitlab:backup:restore BACKUP=1624436184_2021_06_23_13.12.3
Unpacking backup ... done
Before restoring the database we recommend removing 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”继续。
出现第二个交互页面,
Put GitLab hooks in repositories dirs [DONE]
done
Restoring uploads ...
done
Restoring builds ...
done
Restoring artifacts ...
done
Restoring pages ...
done
Restoring lfs objects ...
done
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”继续。
启动Gitlab
sudo gitlab-ctl start
打开迁移后的Gitlab,进行对比
老Gitlab服务器截图
新Gitlab服务器截图
对比,可以发现,两台服务器除了IP地址不一样之外,其他的内容完全一模一样,迁移成功!