Skip to content

Instantly share code, notes, and snippets.

@jwhb
Last active October 31, 2023 20:27
Show Gist options
  • Save jwhb/17b7f5b61e7735a8410951afc1e83cdf to your computer and use it in GitHub Desktop.
Save jwhb/17b7f5b61e7735a8410951afc1e83cdf to your computer and use it in GitHub Desktop.
Create export of all GitLab user project.
# GitLab user project export
#
# Install gitlab module: python3 -m pip install python-gitlab==3.15.0
#
# Set environment variables GITLAB_URL and GITLAB_OAUTH_TOKEN (Personal Access Token):
# GITLAB_URL=https://gitlab.com GITLAB_OAUTH_TOKEN=changeme python3 gitlab-export.py
import gitlab
import time
import tarfile
import os
# Initialize GitLab connection
gl = gitlab.Gitlab.merge_config({
'url': os.getenv('GITLAB_URL'),
'oauth_token': os.getenv('GITLAB_OAUTH_TOKEN')
})
# Get the projects owned by the authenticated user
projects = gl.projects.list(owned=True)
print(f"Projects: {', '.join([p.path_with_namespace for p in projects])}\n")
for project in projects:
print(f"Project: {project.path_with_namespace} ({project.id})")
output = f"gitlab-export-{str(project.path_with_namespace).replace('/', '_')}.tar.gz"
# Check if file exists and is a valid tar file
try:
with tarfile.open(output, 'r') as tar:
print(f"{output} exists, continuing...\n")
continue
except Exception:
pass
# Start the export
export = project.exports.create({"description": "python-gitlab-export"})
time.sleep(1)
# Wait for the export to finish
while True:
export.refresh()
status = export.export_status
if status == "finished":
break
print(f"Waiting for export to finish... (got status '{status}')")
time.sleep(5)
# Download the export
with open(output, 'wb') as f:
export.download(streamed=True, action=f.write)
print(f"Created export {output}.\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment