批量将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"
}

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

完结

相关推荐
不倒翁玩偶5 小时前
Gitlab拉取代码token换成账号密码登录
gitlab
xixingzhe26 小时前
ubuntu安装gitlab
linux·ubuntu·gitlab
切糕师学AI1 天前
GitLab 是什么?
gitlab
路由侠内网穿透.3 天前
本地部署代码托管解决方案 Gitea 并实现外部访问( Windows 版本)
运维·服务器·网络协议·gitea
明月心9523 天前
git remote add 用法
gitlab
吹牛不交税3 天前
gitea安装windows并实现CICD持续集成部署
ci/cd·gitea
only_Klein3 天前
jenkins流水线报错:Connection reset by peer
ci/cd·kubernetes·gitlab·jenkins·ssl
梁萌4 天前
docker部署gitlab和gitlab runner
docker·eureka·gitlab
johnnyAndCode4 天前
Idea 设置GitLab时使用账密,而不是token的配置方法
gitlab·idea
天外飞雨4 天前
Gitlab使用
gitlab