script-sammlung/backup/gitlab/backup-thetown-gitlab.py
2017-02-22 22:06:19 +01:00

51 lines
1.4 KiB
Python

import os
import time
from gitlab import Gitlab
URL = "gitlab.com"
PRIVATE_TOKEN = ""
USERNAME = ""
startedTask = time.time()
gl = Gitlab("https://%s" % URL, PRIVATE_TOKEN)
groupsToCheck = ['TheTown']
gl.auth()
gitPath = "/mnt/sata/BACKUP/Git/"
if not os.path.exists(gitPath):
exit("Git Path does not exists!")
print("Pulling Projects...")
pullList = []
for grp in gl.groups.list():
if [s for s in groupsToCheck if grp.name in s]:
projects = grp.projects.list(all=True)
for p in projects:
path = "%s/%s" % (grp.name, p.name)
pullList.append(path)
print("Pulled %s Projects..." % len(pullList))
print("Starting Clone Task...")
startedClone = time.time()
os.chdir(gitPath)
for singleClone in pullList:
pPath = gitPath + singleClone
print("Check Project State")
if not os.path.exists(pPath):
print("%s is a new Project. Creating Folder and init Repo..." % singleClone)
os.makedirs(pPath)
os.chdir(pPath)
os.system("git init")
else:
print("%s project is not new. switching Directory" % singleClone)
os.chdir(pPath)
print("Start Cloning %s" % singleClone)
os.system("git pull https://%s:%s@%s/%s.git" % (USERNAME, PRIVATE_TOKEN, URL, singleClone))
print("Cloning %s Complete" % singleClone)
os.chdir(gitPath)
print("Clone Complete. Clone ran %ss..." % (time.time() - startedClone))
print("Finished. Ran %ss..." % (time.time() - startedTask))