温馨提示×

linux gitlab怎样恢复仓库

小樊
45
2026-10-01 13:32:18
栏目: 智能运维

在 Linux 上的 GitLab 中“恢复仓库”要分几种场景,不同情况做法完全不同。下面按常见情况说明:


一、误删 GitLab 仓库(项目被删除)

1. GitLab 自带软删除(30 天内可恢复)

如果是通过 Web 页面删除项目:

  • 登录 GitLab
  • 进入 Admin Area → Abusive Content → Blocked Projects(老版本) 或
  • Admin → Overview → Projects → Deleted Projects
  • 找到项目 → Restore

⚠️ 默认保留 7 天或 30 天(取决于配置)


二、GitLab 数据丢失(磁盘/数据库损坏)

1. 使用备份恢复(推荐)

如果你有 GitLab 备份:

# 停止相关服务
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq

# 恢复备份
gitlab-rake gitlab:backup:restore BACKUP=备份时间戳

# 启动
gitlab-ctl start

备份文件一般在:

/var/opt/gitlab/backups/

三、只是仓库的 Git 数据损坏

1. 从本地克隆恢复

如果你或其他人还有本地仓库:

cd /var/opt/gitlab/git-data/repositories/<group>/<project>.git
rm -rf *
git clone --bare /本地仓库路径 .

或:

git push --mirror git@gitlab:group/project.git

四、误删分支 / 提交

1. 本地有记录

git reflog
git checkout <commit>
git branch recovery

2. 远程分支被删

如果有人本地还有:

git push origin recovery

五、GitLab 容器或磁盘损坏(无备份)

⚠️ 很难恢复 可尝试:

  • 使用 git fsck 修复裸仓库
  • 从 CI/CD 缓存、镜像、本地 clone 恢复

六、建议你补充的信息

为了给你精确命令,请告诉我:

  1. GitLab 版本(gitlab-rake gitlab:env:info)
  2. 是怎么“丢”的(删除 / 崩溃 / 迁移失败)
  3. 是否有备份
  4. 是 Omnibus 还是 Docker 安装

我可以一步步带你操作。

0 踩