GitLab在Linux上的数据迁移流程
备份现有GitLab数据
为避免数据丢失,迁移前需完整备份所有关键数据(数据库、仓库、配置文件等)。使用GitLab内置的gitlab-rake命令创建备份:
sudo gitlab-rake gitlab:backup:create
备份文件默认存储在/var/opt/gitlab/backups目录,文件名格式为时间戳_gitlab_backup.tar(如1716400000_2025_05_20_15.0.0_gitlab_backup.tar)。
安装新服务器GitLab
在目标Linux服务器上安装与原服务器相同版本的GitLab(版本不一致可能导致数据结构不兼容)。以CentOS为例,安装步骤如下:
# 添加GitLab官方仓库
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
# 安装GitLab Community Edition
sudo yum install -y gitlab-ce
# 设置外部访问URL(根据实际情况修改)
sudo vim /etc/gitlab/gitlab.rb
# 添加:external_url 'http://your-new-server-ip'
安装完成后,通过sudo gitlab-ctl reconfigure应用配置。
使用scp或rsync将原服务器的备份文件复制到新服务器的/var/opt/gitlab/backups目录(需确保新服务器有足够存储空间):
scp /var/opt/gitlab/backups/1716400000_2025_05_20_15.0.0_gitlab_backup.tar root@new-server:/var/opt/gitlab/backups/
若备份文件较大,可使用rsync进行增量传输。
git用户对备份文件有读取权限:sudo chown git:git /var/opt/gitlab/backups/1716400000_2025_05_20_15.0.0_gitlab_backup.tar
gitlab-rake恢复备份(BACKUP参数为备份文件的时间戳部分):sudo gitlab-rake gitlab:backup:restore BACKUP=1716400000
恢复过程会覆盖新服务器上的现有数据,需确认无误后输入yes继续。除备份文件外,还需迁移原服务器的配置文件和附件目录,确保新服务器保留原设置:
/etc/gitlab/gitlab.rb(GitLab主配置文件)和/etc/gitlab/gitlab-secrets.json(密钥文件,含数据库密码、OAuth令牌等敏感信息):scp /etc/gitlab/gitlab.rb root@new-server:/etc/gitlab/
scp /etc/gitlab/gitlab-secrets.json root@new-server:/etc/gitlab/
/var/opt/gitlab/config(配置文件目录)、/var/opt/gitlab/shared(上传文件、CI/CD artifacts等共享目录):rsync -avz /var/opt/gitlab/config/ root@new-server:/var/opt/gitlab/
rsync -avz /var/opt/gitlab/shared/ root@new-server:/var/opt/gitlab/
这些目录包含用户上传的文件、CI/CD生成 artifacts 等关键数据。编辑新服务器的/etc/gitlab/gitlab.rb文件,确保数据库连接信息和外部访问URL正确:
# 数据库配置(若未修改默认值,可跳过)
gitlab_rails['db_adapter'] = 'postgresql'
gitlab_rails['db_encoding'] = 'unicode'
gitlab_rails['db_host'] = 'localhost'
gitlab_rails['db_port'] = 5432
gitlab_rails['db_username'] = 'gitlab'
gitlab_rails['db_password'] = 'your-password'
# 外部访问URL(根据实际情况修改)
external_url 'http://your-new-server-ip'
修改完成后,重新加载配置:
sudo gitlab-ctl reconfigure
启动GitLab及相关服务,确保所有组件正常运行:
sudo gitlab-ctl start
# 检查服务状态(所有服务应为“run”状态)
sudo gitlab-ctl status
http://your-new-server-ip,确认能正常登录。/var/opt/gitlab/repositories)、用户权限、CI/CD配置、上传文件等是否与原服务器一致。sudo gitlab-ctl tail查看实时日志,定位问题。sudo gitlab-ctl stop unicorn),避免数据在迁移过程中修改。scp -i private_key(SSH密钥认证)或rsync -e "ssh -i private_key",确保数据加密。