Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Last active August 12, 2025 09:09
Show Gist options
  • Save jcoglan/511c770b8709ec8e211683310243f01a to your computer and use it in GitHub Desktop.
Save jcoglan/511c770b8709ec8e211683310243f01a to your computer and use it in GitHub Desktop.
# Usage:
#
# 1. Get a personal access token with repo scope and place it in GITHUB_TOKEN
# 2. Run `backup-github /abs/path/to/dir` to backup repos to the given dir
backup-github () {
local target="$1"
local name=''
local url=''
local dir=''
github-repos | while read -r name url ; do
dir="$target/$name.git"
if [[ -d "$dir" ]] ; then
cd "$dir"
git fetch --prune --verbose origin
cd -
else
mkdir -p "$(dirname "$dir")"
git clone --mirror "$url" "$dir"
fi
done
}
GITHUB_REPO_URL='https://api.github.com/user/repos'
github-repos () {
local page=1
local response=''
while :; do
response="$(curl -s "$GITHUB_REPO_URL?page=$page" -H "Authorization: Bearer $GITHUB_TOKEN")"
if [[ $(jq 'length' <<<"$response") -eq 0 ]] ; then
return
else
jq -r '.[] | .full_name + " " + .ssh_url' <<<"$response"
let page=page+1
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment