Last active
August 20, 2024 20:47
-
-
Save jwhb/17b7f5b61e7735a8410951afc1e83cdf to your computer and use it in GitHub Desktop.
Create export of all GitLab user project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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