CentOS环境下GitLab数据迁移详细步骤
yum install -y policycoreutils openssh-server openssh-clients postfix
systemctl enable sshd && systemctl start sshd # 启用SSH服务(用于传输文件)
systemctl enable postfix && systemctl start postfix # 启用邮件服务(用于GitLab通知)
GitLab数据主要分为三类,需分别备份:
gitlab-rake工具创建包含数据库、仓库、配置等的完整备份,备份文件默认存储在/var/opt/gitlab/backups目录:# 停止GitLab相关服务(确保数据一致性)
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
# 创建备份(备份文件名为时间戳格式,如171017_120000_gitlab_backup.tar)
gitlab-rake gitlab:backup:create
# 启动服务(备份完成后恢复)
gitlab-ctl start
/var/opt/gitlab/git-data/repositories/var/opt/gitlab/postgresql(PostgreSQL数据目录)/var/opt/gitlab/users/etc/gitlab/gitlab.rb、/etc/gitlab/gitlab-secrets.json(密钥文件,务必备份)rsync命令(保留权限):rsync -aHAX /var/opt/gitlab/git-data/repositories/ /tmp/git_repos_backup/
rsync -aHAX /etc/gitlab/gitlab.rb /tmp/git_config_backup/
# 添加GitLab官方YUM仓库
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
# 安装GitLab CE(社区版)
yum install -y gitlab-ce
/etc/gitlab/gitlab.rb,设置外部访问URL(需与旧服务器一致,避免用户访问中断):external_url 'http://新服务器IP' # 若用HTTPS,需配置SSL证书路径
/var/opt/gitlab/backups下的.tar文件)复制到新服务器的相同目录:scp root@旧服务器IP:/var/opt/gitlab/backups/171017_120000_gitlab_backup.tar root@新服务器IP:/var/opt/gitlab/backups/
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-rake gitlab:backup:restore BACKUP=171017_120000 # 171017_120000为备份文件名(不含.tar后缀)
gitlab-ctl start
rsync -aHAX -e ssh /tmp/git_repos_backup/ root@新服务器IP:/var/opt/gitlab/git-data/repositories/
rsync -aHAX -e ssh /tmp/git_config_backup/gitlab.rb root@新服务器IP:/etc/gitlab/
gitlab-ctl stop postgresql
rm -rf /var/opt/gitlab/postgresql/*
rsync -aHAX -e ssh /tmp/git_db_backup/ root@新服务器IP:/var/opt/gitlab/postgresql/
gitlab-ctl start postgresql
/etc/gitlab/gitlab.rb中的以下参数:external_url 'http://新服务器IP:新端口' # 如80→8080,需同步修改SSH端口(若用SSH克隆)
gitlab_rails['gitlab_shell_ssh_port'] = 新端口 # 若SSH端口变更
gitlab-ctl reconfigure # 重新生成配置文件
gitlab-ctl restart # 重启所有服务
http://新服务器IP,使用旧服务器的用户名/密码登录,检查项目、用户、权限是否正常。git clone http://新服务器IP/项目路径.git,测试提交(git push)和拉取(git pull)功能。gitlab-ctl tail # 实时查看所有服务日志
unicorn、sidekiq),避免数据写入导致不一致。rsync时添加-aHAX参数,保留文件权限和属主(如git用户对仓库的读写权限)。chcon -R -t var_opt_t /var/opt/gitlab),避免权限拒绝。