Skip to content

Instantly share code, notes, and snippets.

@david-bergstrom
Last active August 29, 2015 14:23
Show Gist options
  • Save david-bergstrom/2c1c90b7d90295cf5663 to your computer and use it in GitHub Desktop.
Save david-bergstrom/2c1c90b7d90295cf5663 to your computer and use it in GitHub Desktop.
#!/bin/python2
# -*- coding: utf-8 -*-
# Author: David Bergström
#
# This script contacts the given gitlab servers and retreives a list
# of all git repositories the user has access to. It then proceeds and
# runs "git clone --mirror" on all the repositories, creating a backup
# of all the repositories.
#
# Requirements:
# - git
# - python2
from urllib2 import urlopen
from json import JSONDecoder
from pprint import pprint
from subprocess import call
from os.path import isdir
# The repositories are defined with a 3-tuple
gitlab = [("http://gitlab.example.com", "private-token", "directory-name"),
("http://gitlab2.example.com", "private-token", "directory-name")]
for server, token, directory in gitlab:
url = urlopen(server + "/api/v3/projects?private_token=" + token)
data = JSONDecoder().decode(url.read())
ssh_addresses = [(project[u"ssh_url_to_repo"], project[u"path"])
for project in data]
for address, project_path in ssh_addresses:
path = directory + "/" + project_path
if (isdir(path)):
call(["git", "--git-dir=" + path, "fetch", "--all"])
else:
call(["git", "clone", "--mirror", address, directory + "/" + project_path])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment