added github backup script
This commit is contained in:
parent
125256558b
commit
bcb4148963
123
backup/github/backup-github.py
Normal file
123
backup/github/backup-github.py
Normal file
@ -0,0 +1,123 @@
|
||||
"""
|
||||
@AUTHOR: Gurkengewuerz
|
||||
@REVIEW: 27.02.2017
|
||||
@DESCRIPTION: Backup Private Github Account.
|
||||
Need Private Access Token with Range:
|
||||
- repo
|
||||
- read:org
|
||||
- read:repo_hook
|
||||
- read:user
|
||||
"""
|
||||
|
||||
import requests
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
|
||||
USER = "Gurkengewuerz"
|
||||
OAUTH = "<oauth-key>"
|
||||
PATH = "D:\\Downloads\\git-test"
|
||||
if not os.path.exists(PATH):
|
||||
exit("%s Path does not exists!" % PATH)
|
||||
DOWNLOAD_ORGS = True
|
||||
DOWNLOAD_STARS = True
|
||||
DOWNLOAD_PRIVATE = True
|
||||
DOWNLOAD_FORK = True
|
||||
|
||||
BASE_URL = "https://api.github.com"
|
||||
DEBUG = True
|
||||
|
||||
HEADER = \
|
||||
{
|
||||
"user-agent": "Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0",
|
||||
"Authorization": "token %s" % OAUTH,
|
||||
"Accept": "application/vnd.github.v3.raw"
|
||||
}
|
||||
|
||||
|
||||
def debug(msg):
|
||||
if DEBUG:
|
||||
print(msg)
|
||||
|
||||
|
||||
class Repository:
|
||||
def __init__(self, fullname):
|
||||
self.fullname = fullname
|
||||
splitted = fullname.split("/")
|
||||
self.user = splitted[0]
|
||||
self.name = splitted[1]
|
||||
|
||||
def __str__(self):
|
||||
return "fullname: %s, user: %s, name %s" % (self.fullname, self.user, self.name)
|
||||
|
||||
|
||||
repolist = list()
|
||||
|
||||
|
||||
def repoInList(fullname):
|
||||
for repo in repolist:
|
||||
if repo.fullname == fullname:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def fetchRepos(json_repoList):
|
||||
for repo in json_repoList:
|
||||
if not DOWNLOAD_PRIVATE and repo["private"]:
|
||||
continue
|
||||
if not DOWNLOAD_FORK and repo["fork"]:
|
||||
continue
|
||||
if repoInList(repo["full_name"]):
|
||||
continue
|
||||
repolist.append(Repository(repo["full_name"]))
|
||||
|
||||
|
||||
startedTask = time.time()
|
||||
|
||||
debug(HEADER)
|
||||
|
||||
r = requests.get(BASE_URL + "/users/" + USER, headers=HEADER)
|
||||
user_oject = json.loads(r.text)
|
||||
debug(user_oject)
|
||||
|
||||
r = requests.get(BASE_URL + "/user/repos?per_page=250", headers=HEADER)
|
||||
user_repo_object = json.loads(r.text)
|
||||
debug(user_repo_object)
|
||||
fetchRepos(user_repo_object)
|
||||
|
||||
r = requests.get(user_oject["starred_url"].replace("{/owner}{/repo}", "") + "?per_page=250", headers=HEADER)
|
||||
user_starred_object = json.loads(r.text)
|
||||
debug(user_starred_object)
|
||||
fetchRepos(user_starred_object)
|
||||
|
||||
if DOWNLOAD_ORGS:
|
||||
r = requests.get(BASE_URL + "/user/orgs", headers=HEADER)
|
||||
user_orgs_object = json.loads(r.text)
|
||||
debug(user_orgs_object)
|
||||
|
||||
for orgs in user_orgs_object:
|
||||
orgname = orgs["login"]
|
||||
r = requests.get(orgs["repos_url"] + "?per_page=250", headers=HEADER)
|
||||
org_repo_object = json.loads(r.text)
|
||||
fetchRepos(org_repo_object)
|
||||
|
||||
print("\n\n")
|
||||
for repo in repolist:
|
||||
print(repo)
|
||||
pPath = os.path.join(PATH, repo.user, repo.name)
|
||||
print("Check Project State")
|
||||
if not os.path.exists(pPath):
|
||||
print("%s is a new Project. Creating Folder and init Repo..." % repo.fullname)
|
||||
os.makedirs(pPath)
|
||||
os.chdir(pPath)
|
||||
os.system("git init")
|
||||
else:
|
||||
print("%s project is not new. switching Directory" % repo.fullname)
|
||||
|
||||
os.chdir(pPath)
|
||||
print("Start Cloning %s" % repo.fullname)
|
||||
os.system("git pull https://%s:x-oauth-basic@github.com/%s.git" % (OAUTH, repo.fullname))
|
||||
print("Cloning %s Complete" % repo.fullname)
|
||||
os.chdir(PATH)
|
||||
|
||||
print("Finished. Ran %ss..." % (time.time() - startedTask))
|
Loading…
Reference in New Issue
Block a user