How to backup your Github & Gitlab projects

Github Backup

Create an Access Token

Run the backup script

#!/bin/sh

dirname=github-backup-$(date "+%Y-%m-%d-%H-%M-%S")
mkdir "$dirname"
cd $dirname

#change these vars:
privateToken=YOUR_ACCESS_TOKEN
userName=YOUR_GITHUB_USERNAME

curl -H "Accept: application/vnd.github.nebula-preview+json" \
-H "Authorization: token $privateToken" \
"https://api.github.com/user/repos?visibility=all&affiliation=owner&per_page=200" \
| jq -r '.[] | .name' \
| while IFS= read projectName; do
curl -H "Authorization: token $privateToken" -H "Accept: application/vnd.github.v3.raw" -L \
"https://api.github.com/repos/$userName/$projectName/zipball" --output $projectName.zip
done

echo Done! All files downloaded here: $(pwd)

Gitlab Backup

Create an Access Token

Run the backup script

#!/bin/sh

dirname=gitlab-backup-$(date "+%Y-%m-%d-%H-%M-%S")
mkdir "$dirname"
cd $dirname

privateToken=YOUR_ACCESS_TOKEN
userName=YOUR_GITLAB_USERNAME

curl --header "Private-Token: $privateToken" "https://gitlab.com/api/v4/users/$userName/projects" \
| jq -r '.[] | .id, .name' \
| while IFS= read projectId; read projectName; do
curl --header "Private-Token: $privateToken" "https://gitlab.com/api/v4/projects/$projectId/repository/archive.zip" --output $projectName.zip
done

echo Done! All files downloaded here: $(pwd)

--

--

--

https://alexadam.dev/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why Graph data bases are the future

How Low-Code Platforms are Making Digitalization more Accessible

Everything I learned about authentication in web API’s

YAML Fundamentals

Date — The Command

Easy Error Resolution

Leetcode: Q121 — Best Time to Buy and Sell Stock [Easy]

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
alex adam

alex adam

https://alexadam.dev/

More from Medium

How to create a chroot environment

How to get alerts of GitHub events on your Microsoft Teams channel?

Improve your project workflow with these bitbucket pipelines/github actions automations — ClickPesa…

Comparing Shopware apps and plugins — BitBag