本地git仓库文件夹迁移到gogs服务器 python脚本 本文共有1912个字,关键词: 在gogs git服务器当机或系统损坏的情况下。直接将服务器gogs-repositories目录拷贝到本地,然后通过脚本枚举导入到新搭建的gogs服务器中。 脚本如下: ``` #coding=UTF-8 #######coding="utf-8" import requests import json from urllib import parse import os #本地gogs平台的api地址,目前的api版本是v1 purl = "http://192.168.1.100:3000/api/v1" ps = parse.urlparse(purl) phost = ps.netloc #gogs 后台设置的root用户token token = "c38fa4754a628f71d89ba26505fbfe758a1b2c2a" 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)} #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) print(res) respond = json.loads(res.text) return respond #通过接口获取gitlab的所有用户,并添加到gogs中。 def getLocalgit(): file_dir = "./" for files in os.listdir(file_dir): if(str(files) != "gogs.py"): print(files) user = "test" uid = 1 clone_addr = "/www/nas/git_part/"+ user +"/"+str(files) repo_name = str(files).replace(".git","") print(clone_addr,uid,repo_name) add_result = Migrate(clone_addr,uid,repo_name) print(add_result) try: getLocalgit() except Exception as e: print("出错A",e) getLocalgit() ``` × yihong (๑>ڡ<)☆谢谢老板~ 2元 5元 10元 50元 100元 任意金额 2元 使用微信扫描二维码完成支付 版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。 码农心得,Python 2019-11-25 评论 6403 次浏览