You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitLab备份恢复失败:Git数据未恢复且PostgreSQL连接异常

解决方案:GitLab EE备份恢复PG连接错误及仓库数据缺失问题

一、解决PQsocket() can't get socket descriptor连接错误

这个错误多是恢复过程中数据库连接被意外中断,结合Docker Swarm环境,按以下步骤排查修复:

  • 重启GitLab内部数据库相关服务:恢复时即便停了puma和sidekiq,外部PG的连接可能被Swarm网络或容器连接池回收。执行:

    docker exec -t $CONTAINER_ID gitlab-ctl restart postgresql
    docker exec -t $CONTAINER_ID gitlab-ctl restart redis
    

    重启后重新执行恢复,先确保核心服务处于停止状态:

    docker exec -t $CONTAINER_ID gitlab-ctl stop puma
    docker exec -t $CONTAINER_ID gitlab-ctl stop sidekiq
    docker exec -t $CONTAINER_ID gitlab-backup restore BACKUP=<backup_name>
    
  • 调整Swarm网络超时设置:Swarm overlay网络默认超时可能导致长时恢复断连,更新服务网络参数:

    docker service update --network-opt "com.docker.network.connect-timeout=600" <gitlab_service_name>
    

    把超时设为10分钟,适配大备份的恢复时长。

  • 验证外部PG的连接权限:确认GitLab容器所在Swarm节点能访问外部PG,检查PG的pg_hba.conf是否允许容器IP段连接,同时把max_connections参数调至100以上。

二、修复仓库数据未恢复到指定目录的问题

备份含仓库数据但恢复后缺失,大概率是恢复因连接错误提前终止,或存储配置有误:

  • 手动解压备份恢复仓库:找到备份文件(路径一般是/var/opt/gitlab/backups/<backup_name>.tar),手动提取并复制数据:

    # 进入GitLab容器
    docker exec -it $CONTAINER_ID bash
    # 创建临时解压目录
    mkdir /tmp/gitlab_backup_extract
    # 解压备份包
    tar -xf /var/opt/gitlab/backups/<backup_name>.tar -C /tmp/gitlab_backup_extract
    # 复制仓库数据到目标路径
    cp -r /tmp/gitlab_backup_extract/repositories/* /var/opt/gitlab/git-data/repositories/
    # 修复权限
    chown -R git:git /var/opt/gitlab/git-data/repositories/
    chmod -R 700 /var/opt/gitlab/git-data/repositories/
    

    完成后重启GitLab服务:

    gitlab-ctl restart
    
  • 检查gitlab.rb存储配置:确认git_data_dirs指向正确路径,避免被Swarm挂载卷覆盖错误:

    git_data_dirs({
      "default" => {
        "path" => "/var/opt/gitlab/git-data"
      }
    })
    

    修改后执行gitlab-ctl reconfigure生效。

三、恢复后验证

  1. 执行gitlab-rake gitlab:check检查组件状态
  2. 查看日志确认无报错:tail -f /var/log/gitlab/gitlab-rails/production.log
  3. 登录GitLab界面验证仓库是否正常显示

内容的提问来源于stack exchange,提问作者Gutt0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 14:46:05