批量将gitlab仓库转移到gitea中

前言

单位有一个机器,机器里是个vm esxi里面跑着一个虚拟机里面跑着一个gitlab,经历了岁月变迁,网改了一遍,办公室改了一遍,机器折腾来折腾去,可能闪到腰了吧,gitlab总是访问不到,寻思吧库都导出来迁移到另一个机器里,另一个机器里面是gitea,现在这个虚拟机已经访问不大了,一拷贝数据磁盘就找不到。。只好换了个机器,插着这块硬盘,但是网络问题连不上虚拟机。干脆手动迁移吧(好几百个库呢。。。)

注意:只能导出来Git仓库,不能导出其他东西

将数据拷贝出来

使用vm esx控i制台登录虚拟机(本来想直连的,结果网不行,转不过去)跳转到/var/opt/gitlab/git-data将其拷出来cp -d /var/opt/gitlab/git-data ~/gitBack(必须加-d 否则有软连接后续无法使用)

此时发生了一件事,磁盘有500G。。而我没地方存。。于是关机,给虚拟机加了一个20g的硬盘,根据网上的教程挂载上去,把数据拷贝过去,然后打开vm的ssh,登录,找到当时加的那快硬盘,拷贝到本地电脑里

整理文件

使用7zip打开vmdk(这玩意真牛B。DG都打不开这个居然可以)解压到某处

此时的结构是xxxxx下有好几个子库,,如android_app.git其下是这些

这里跟gitea是一样的,但是外层目录结构不一样,所以得改一下,gitlab名称是可以加好多/的但是gitea不行,拿脚本创建新目录,把之前导出来的数据压扁成这样
xxxxx_android_app\.git

以下脚本在xxxxx的上一层使用

powershell 复制代码
$d = Resolve-Path ../
$outPath = "$d" + "\outGitRpos\"
New-Item -ItemType Directory -Path $outPath
Get-ChildItem | foreach {
	# Set-Location $_.Name
	$base_path_name = $_.Name
	Get-ChildItem -Path $_.Name | foreach {
		$path_name = $base_path_name + "\" + $_.Name
		$path_name2 = $base_path_name + "_" + $_.Name
		# 名字中去除.git
		$new_path_name = $path_name2 -replace ".git",""
		Write-Output $path_name\*		
		Write-Output $outPath$new_path_name\.git
		# 按新规则创建文件夹和其下.git文件夹
		New-Item -ItemType Directory -Path $outPath$new_path_name\.git
		# 复制文件到里面
		Copy-Item $path_name\* $outPath$new_path_name\.git -Recurse
	}
}

执行脚本后找上一层outGitRpos其下都是整理好的仓库每个文件夹里都有一个.git文件夹,此时是无法还原文件的,因为.git里的config文件不对,需要更改

创建一个config文件,写下以下内容

txt 复制代码
[core]
	bare = false
	repositoryformatversion = 0
	filemode = false
	symlinks = false
	ignorecase = true
	logallrefupdates = true

outGitRpos文件夹下执行第二个脚本,修改每个文件夹下的config

powershell 复制代码
Get-ChildItem -Directory | foreach {
	$path = "$_" + "\.git"
	Write-Output $path
	Copy-Item -Force config $path
}

新问题

此时又出现了一个问题。仓库没有名字。不知道是什么项目,只好进去拿命令跑出备份,
gitlab-rake gitlab:backup:create

像以前一样拷贝出来,解压,找到db,找到,根据不同数据库类型自行寻找导入方法,执行一下sql查出一个json,复制出后再前后加上[]去除最后一个逗号,留下备用

sql 复制代码
select replace(b.path, '/', '_'), concat('{ "name": "',replace(b.path, '/', '_'),'", "description": "', a.description,'" },') from projects a left join routes b on b.source_id = a.id and source_type = 'Project'

gitea接口

http://192.168.1.220:3000/api/swagger 接口文档
http://192.168.1.220:3000/swagger.v1.json 导入接口文档

找个apifox用json那个导入进去

或者按需拿脚本调用
auth改成basic auth输入用户名密码,
post /user/repos 创建仓库

body json

json 复制代码
{
    "name": "项目名",
    "description": "描述",
    "private": true
}

默认将仓库创建到登录用户名下
post /repos/{owner}/{repo}/transfer转入仓库(我要将所有项目转移到组织里)

复制代码
owner: 仓库转让的所有者
repo: 要转移的仓库名称

body json

json 复制代码
{
    "new_owner": "创建的组织名(英文命名)"
}

想办法拼接上面jsonlist循环调用这两个接口,gitea的事就完了

我用的apifox调用的,写的自动化测试,可以用自己熟悉的脚本语言跑

最后一哆嗦

outGitRpos执行,将仓库上传到gitea

powershell 复制代码
Get-ChildItem -Directory | foreach {
	$path = "$_"
	#Write-Output $path
	# git checkout
	# 恢复文件
	#Start-Process cmd.exe -ArgumentList "/k cd /d $path & git checkout"
	# 恢复文件并更改仓库地址并推送
	Start-Process cmd.exe -ArgumentList "/k cd /d $path & git remote add origin http://192.168.1.220:3000/oldProjects/$path.git & git push -u origin master"
}

脚本跑完手动检查一下是否有错误,有的仓库是空的,没有文件,直接就失败了。忽略就行

完结

相关推荐
不念霉运16 小时前
2025 Gitee vs. GitLab:全面对比与选择指南
gitee·gitlab
水瓶_bxt16 小时前
创建 GitLab Runner 使用CICD自动化部署容器
eureka·自动化·gitlab
黑心的奥利奥2 天前
Docker配置Gitlab-runner实现自动化容器化部署前端项目
docker·自动化·gitlab
wuzuyu3653 天前
在腾讯云上安装gitlab
云计算·gitlab·腾讯云
xiaodaiwang3 天前
OpenEuler 22.03 系统上安装配置gitlab runner
gitlab
TimberWill3 天前
gitlab私服搭建
gitlab
中东大鹅3 天前
访问 gitlab 跳转 0.0.0.0
gitlab
guygg884 天前
配置本地git到gitlab并推送
git·gitlab
大A崛起4 天前
Gitlab-CI实现组件自动推送
ci/cd·gitlab·github
越来越无动于衷5 天前
GitLab 社区版 10.8.4 安装、汉化与使用教程
gitlab