Skip to content

Instantly share code, notes, and snippets.

@vepetkov
Created September 4, 2018 11:29
Show Gist options
  • Save vepetkov/3ba11c9fb39f15faf6ca4687faa2f521 to your computer and use it in GitHub Desktop.
Save vepetkov/3ba11c9fb39f15faf6ca4687faa2f521 to your computer and use it in GitHub Desktop.
Backup All GitLab Projects
## pip install python-gitlab gitpython
import gitlab # python-gitlab
from git import Repo # gitpython
import os, time
##########################
### Python Gitlab Config: ~/.python-gitlab.cfg
# [global]
# default = GitLab
# ssl_verify = true
# timeout = 5
# api_version = 4
#
# [GitLab]
# url = http://GitLab.localdomain
# private_token = afsdfsdfsdfrewet43cef123
# api_version = 4
#####
## Backup all accessible repos
backupDir = './repos_backup'
# Create the GitLab client
gl = gitlab.Gitlab.from_config('GitLab')
# Get all groups accessible with the given token
all_groups = [(g.id, g.name, g.description, g.full_path) for g in gl.groups.list(all=True)]
# or select a specific group from them
# all_groups = [g for g in all_groups if g[3] == 'devtool/special-projects']
# For all accessible groups, get all projects and either clone or update them
for g in all_groups:
projects = [(p.name, p.path_with_namespace, p.ssh_url_to_repo) for p in gl.groups.get(g[0]).projects.list(all=True)]
for p in projects:
path = os.path.join(backupDir, p[1])
if (os.path.exists(path)):
print('Updating %s/%s' % (g[1], p[0]))
repo = Repo(path)
origin = repo.remote()
try:
for fetch_info in origin.pull():
print("Updated %s to %s" % (fetch_info.ref, fetch_info.commit))
except:
print('Failed updating %s/%s' % (g[1], p[0]))
pass
else:
print('Cloning: ' + g[1] + ' : ' + p[0])
os.makedirs(path, exist_ok = True)
# Clone a remote repo
try:
Repo.clone_from(p[2], path)
except:
print('Failed updating %s/%s' % (g[1], p[0]))
pass
# Slow down the process as Gitlab will block the account :-)
print(' ... waiting ...')
time.sleep(15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment