一、迁移前准备
openssh-server、postfix、policycoreutils-python),确保GitLab运行所需的服务(SSH、邮件)可用。二、备份原GitLab数据
rsync或tar备份/var/opt/gitlab目录(包含仓库、配置、附件等所有数据),例如:sudo rsync -aAXv /var/opt/gitlab/ /path/to/backup/gitlab_backup/
# 或
sudo tar -cvzf gitlab_backup.tar.gz -C /var/opt gitlab
此目录包含用户仓库、配置文件(/var/opt/gitlab/config)、附件(/var/opt/gitlab/shared)等重要数据。gitlab-rake命令导出:sudo gitlab-rake gitlab:backup:create
备份文件默认保存在/var/opt/gitlab/backups目录,文件名为时间戳_gitlab_backup.tar(如1715200000_2025-05-08_16.0.0_gitlab_backup.tar)。/etc/gitlab/gitlab.rb和SSL证书目录/etc/gitlab/ssl,确保新服务器配置一致:sudo cp /etc/gitlab/gitlab.rb /path/to/backup/
sudo cp -r /etc/gitlab/ssl /path/to/backup/
```。
三、在新服务器上安装GitLab
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo yum install -y gitlab-ce
http://<新服务器IP>。/etc/gitlab/gitlab.rb,设置external_url(如http://your-new-server-ip),并保存。四、恢复数据到新服务器
/var/opt/gitlab/backups目录(含备份文件)、/var/opt/gitlab目录(数据目录)、/etc/gitlab/gitlab.rb(配置文件)复制到新服务器对应位置,例如使用scp:scp -r /path/to/backup/gitlab_backup/backups/ root@new-server:/var/opt/gitlab/backups/
scp -r /path/to/backup/gitlab_backup/config/ root@new-server:/var/opt/gitlab/config/
scp -r /path/to/backup/gitlab_backup/shared/ root@new-server:/var/opt/gitlab/shared/
scp /path/to/backup/gitlab_backup/gitlab.rb root@new-server:/etc/gitlab/
```。
sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq
/var/opt/gitlab/backups目录,文件名需保留时间戳,如1715200000_gitlab_backup.tar):sudo cp /path/to/backup/backups/1715200000_gitlab_backup.tar /var/opt/gitlab/backups/
BACKUP参数为备份文件的时间戳部分):sudo gitlab-rake gitlab:backup:restore BACKUP=1715200000
sudo gitlab-ctl start
```。
/var/opt/gitlab目录覆盖新服务器的对应目录(需停止GitLab服务):sudo rsync -aAXv /path/to/backup/gitlab_backup/ /var/opt/gitlab/
sudo cp /path/to/backup/gitlab.rb /etc/gitlab/gitlab.rb
sudo cp -r /path/to/backup/ssl /etc/gitlab/
sudo gitlab-ctl reconfigure
```。
五、验证迁移结果
sudo gitlab-ctl status,确认所有服务(unicorn、sidekiq、postgresql)均处于run状态。http://<新服务器IP>,使用原GitLab的管理员账号登录,验证是否能正常访问。