GitLab在Linux上的迁移流程
GitLab提供了gitlab-rake
命令,可一键备份仓库、数据库、用户/组信息、配置文件、附件等所有数据。执行以下命令:
sudo gitlab-rake gitlab:backup:create
备份文件默认存储在/var/opt/gitlab/backups/
目录下,文件名格式为时间戳_gitlab_backup.tar
(如1716590523_2025_09_23_gitlab_backup.tar
)。
若需额外备份GitLab数据目录(包含仓库裸文件、配置等),可使用rsync
或tar
命令:
sudo rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*"} /var/opt/gitlab/ /path/to/linux_backup/
sudo tar -czvf gitlab_full_backup.tar.gz -C /var/opt/gitlab/ .
以上命令会将/var/opt/gitlab/
目录下的所有数据(除排除项外)备份到指定路径。
根据Linux发行版选择安装方式(以CentOS 7为例,使用Omnibus包):
# 添加GitLab官方YUM仓库
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
# 安装GitLab Community Edition(CE)
sudo yum install -y gitlab-ce
安装完成后,GitLab会自动配置默认服务。
编辑GitLab主配置文件/etc/gitlab/gitlab.rb
,设置外部访问URL(替换为新服务器IP或域名):
sudo vim /etc/gitlab/gitlab.rb
找到external_url
配置项,修改为:
external_url 'http://新服务器IP'
保存后,重新加载配置使更改生效:
sudo gitlab-ctl reconfigure
启动GitLab服务:
sudo gitlab-ctl start
访问http://新服务器IP
,确认GitLab首页可正常访问。
使用scp
或rsync
将原服务器的备份文件(如1716590523_gitlab_backup.tar
)复制到新服务器的/var/opt/gitlab/backups/
目录:
scp /path/to/linux_backup/1716590523_gitlab_backup.tar root@新服务器IP:/var/opt/gitlab/backups/
确保备份文件权限正确(属主为git
用户):
sudo chown git:git /var/opt/gitlab/backups/1716590523_gitlab_backup.tar
```。
#### **4.2 恢复备份数据**
执行以下命令恢复备份(将`1716590523`替换为备份文件的时间戳):
```bash
sudo gitlab-rake gitlab:backup:restore BACKUP=1716590523
恢复过程会覆盖新服务器上的现有数据(若新服务器已有数据,需提前备份),恢复完成后会提示“Restore complete”。
若备份时未包含/var/opt/gitlab/config
(配置文件)或/var/opt/gitlab/shared
(附件、上传文件)目录,需手动复制:
# 从原服务器复制目录到新服务器
sudo rsync -avz /var/opt/gitlab/config/ root@新服务器IP:/var/opt/gitlab/config/
sudo rsync -avz /var/opt/gitlab/shared/ root@新服务器IP:/var/opt/gitlab/shared/
# 确保目录权限正确
sudo chown -R git:git /var/opt/gitlab/config/
sudo chown -R git:git /var/opt/gitlab/shared/
```。
### **5. 验证迁移结果**
- **访问GitLab**:通过浏览器访问新服务器的`external_url`(如`http://新服务器IP`),登录GitLab管理员账号。
- **检查项目数据**:进入“项目”页面,确认所有项目(包括仓库、分支、标签、提交记录)均已恢复。
- **检查用户/组**:进入“Admin Area”→“Users”和“Groups”,确认用户、组信息及权限设置正确。
- **测试功能**:创建一个新项目,推送代码到仓库,确认Git操作(clone、push、pull)正常。
### **注意事项**
- **版本一致性**:迁移前后GitLab版本必须一致,否则会提示“版本不匹配”错误。可通过`cat /opt/gitlab/embedded/service/gitlab-rails/VERSION`查看原服务器版本,新服务器安装时选择相同版本。
- **备份完整性**:迁移前务必验证备份文件的完整性(如检查备份文件大小、解压后是否能正常读取数据)。
- **权限问题**:恢复数据后,需确保`/var/opt/gitlab/`目录及其子目录的属主为`git`用户(`sudo chown -R git:git /var/opt/gitlab/`),否则GitLab服务可能无法正常访问数据。
- **停机时间**:备份和恢复过程中,需停止GitLab相关服务(如`unicorn`、`sidekiq`),以避免数据不一致。停止服务的命令:
```bash
sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq
恢复完成后,再启动服务:
sudo gitlab-ctl start
```。