gitlab批量迁移到gogs python脚本 本文共有6370个字,关键词: 原来使用的是gitlab服务器。现需要迁移至新的服务器,如果是仍然安装gitlab进行迁移,操作起来很麻烦。而且容易出问题。并且gitlab本身比较臃肿服务器开销也大不少。所以考虑之后选择gogs这种轻量的git服务。gogs是golang编写的程序,本身配置比较简单而且跨平台。相应的配置请百度。这里分享一下此次迁移中自行编写的一个python脚本 具体使用说明请看注释 ``` #coding=UTF-8 #######coding="utf-8" import requests import json from urllib import parse #本地gogs平台的api地址,目前的api版本是v1 purl = "http://192.168.1.100:3000/api/v1" ps = parse.urlparse(purl) phost = ps.netloc #gogs 后台设置的root用户token token = "c2327973b0af45da3d8c52dce70b7e3ae7f7be53" header = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36", "Content-Type": "application/x-www-form-urlencoded", "Host": str(phost), "Accept": "*/*", "Referer": str(purl)} url = str(purl)+"/user/repos?token="+str(token) #gogs 添加用户 def addUser(full_name,username,password,email): header = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36", "Content-Type": "application/x-www-form-urlencoded", "Host": str(phost), "Accept": "*/*", "Referer": str(purl)} url=str(purl)+"/admin/users?token="+str(token) data1={"full_name":str(full_name),"username":str(username),"password":str(password),"email":str(email)} res = requests.post(url,proxies=None, data=data1, headers=header, verify=False) respond = json.loads(res.text) #返回的数据格式 #{'id': 9, 'username': 'test', 'login': 'test', 'full_name': '', 'email': 'test@baidu.com', 'avatar_url': 'https://secure.gravatar.com/avatar/92343f3384a3d235d695c827ccbf8fec?d=identicon'} return respond #获取gogs 用户信息 def getUser(username): header = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36", "Content-Type": "application/x-www-form-urlencoded", "Host": str(phost), "Accept": "*/*", "Referer": str(purl)} url=str(purl)+"/users/"+str(username) data1="" res = requests.get(url,proxies=None, data=data1, headers=header, verify=False) respond = json.loads(res.text) #返回的数据格式 #{'id': 9, 'username': 'test', 'login': 'test', 'full_name': '', 'email': 'test@baidu.com', 'avatar_url': 'https://secure.gravatar.com/avatar/92343f3384a3d235d695c827ccbf8fec?d=identicon'} return respond #gogs 添加仓库 def Migrate(clone_addr,uid,repo_name): header = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36", "Content-Type": "application/x-www-form-urlencoded", "Host": str(phost), "Accept": "*/*", "Referer": str(purl)} url=str(purl)+"/repos/migrate?token="+str(token) data1={"clone_addr":str(clone_addr),"uid":str(uid),"repo_name":str(repo_name)} res = requests.post(url,proxies=None, data=data1, headers=header, verify=False) respond = json.loads(res.text) return respond #通过接口获取gitlab的所有用户,并添加到gogs中。 def getOldRepo(): header = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36", "Content-Type": "application/x-www-form-urlencoded", "Host": str(phost), "Accept": "*/*", "Referer": str(purl)} #旧的gitlab仓库api地址 url="http://88.88.88.88:8099/api/v3/users?private_token=rLzYi52Di6unT9Z6zj9H&page="+str(1)+"&per_page="+str(100) res = requests.get(url,proxies=None, data="", headers=header, verify=False) all_users = json.loads(res.text) for u in all_users: #跳过某个用户 if(u['username'] == 'test'): continue new_user = addUser(str(u['name']),str(u['username']),"123456",str(u['email'])) if(new_user['message']!=None): #用户存在 # print(new_user['message']) try: new_user = getUser(u['username']) except Exception as e: print("出错B",e) # print(u['id'],u['name'],u['username'],u['email']) #这里是获取某个用户的所有仓库。找了一遍gitlab的api接口没有发现哪个接口可以获取到一个用户的所有仓库列表 #所以这里我们采用一个简单的办法就是在gitlab服务器端新建一个站点,然后里边放一个git.php文件,直接通过 #这个文件去读取服务器上仓库所在的目录文件夹。列表出对应用户的所有仓库并用json返回 #【当然你也可以用python在服务器开一个api,结果是一样的。】 url="http://test.com/git.php?dir="+str(u['username']) ps = parse.urlparse(url) host = ps.netloc header = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36", "Content-Type": "application/x-www-form-urlencoded", "Host": str(host), "Accept": "*/*", "Referer": str(purl)} res = requests.get(url,proxies=None, data="", headers=header, verify=False) if(res.text != ""): repos = json.loads(res.text) if(len(repos) > 0): # print(repos) for r in repos: # print(r) giturl = "http://88.88.88.88:8099/"+str(u['username'])+"/"+str(r) cur_repo_name = str(r)[0:len(str(r))-4] if(cur_repo_name.find(".wiki") > 0): continue; # cur_repo_name = cur_repo_name.replace(".wiki","_wiki") # print(r,"=>",cur_repo_name) try: r2 = Migrate(giturl,new_user['id'],str(cur_repo_name)) if(len(r2)<=0): print("迁移出错") elif(r2['message'] != None): if(r2['message'].find('exists') > 0): continue else: print(u['name']+str(giturl)+" => "+str(r2['message'])) # print("迁移仓库出错:"+u['name']+"=>"+str(cur_repo_name)+"=>"+str(r2['message'])+"=>"+str(giturl)) elif(r2['id'] != None): print(u['name']+"=>"+str(cur_repo_name)+"=>添加成功") else: print("迁移未知错误",r2) except Exception as e: print("出错C",r2,e) try: getOldRepo() except Exception as e: print("出错A",e) getOldRepo() ``` 这里需要注意如果仓库比较大的话,会迁移失败。这时需要修改一下gogs的仓库迁移时间限制和大小限制。 × yihong (๑>ڡ<)☆谢谢老板~ 2元 5元 10元 50元 100元 任意金额 2元 使用微信扫描二维码完成支付 版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。 码农心得 2019-11-12 评论 2931 次浏览