Skip to content

Instantly share code, notes, and snippets.

@luttje
Last active January 14, 2022 15:24
Show Gist options
  • Save luttje/5e2886952fe80a47fa78ae8021f1181c to your computer and use it in GitHub Desktop.
Save luttje/5e2886952fe80a47fa78ae8021f1181c to your computer and use it in GitHub Desktop.
Backup script for my public & private repo's
#!/bin/sh
# Aa script to backup Github repositories to local
# Based on a script by Rong Zhuang:
# https://github.com/jojozhuang
# https://jojozhuang.github.io/tutorial/backup-github-repositories-to-synology-nas/
# The local path where the download repo's should be saved
BACKUP_PATH="/volume1/github"
# GitHub API URL
URL_REPOS="https://api.github.com/user/repos?type=owner&per_page=100&page="
# (Required) GitHub Personal Access Token
# Ensure full `repo` scope access @ https://github.com/settings/tokens
OAUTH_TOKEN=""
# Total repo count
COUNT=0
# Create backup directory
DIR_NAME="repos"
mkdir "${BACKUP_PATH}/${DIR_NAME}" -p
echo "Create backup directory: ${BACKUP_PATH}/${DIR_NAME}"
fetch_fromUrl() {
PAGE=1
while true
do
URL_REPO_PAGE="$URL_REPOS$PAGE"
echo "Fetching all repositories from ${URL_REPO_PAGE}"
REPOS=`curl -H "Authorization: token ${OAUTH_TOKEN}" -s "${URL_REPO_PAGE}" | jq -r '.[] | "\(.name),\(.full_name),\(.private),\(.html_url),\(.default_branch)"'`
if [ ${#REPOS} -le 0 ]; then
break
fi
((PAGE++))
for REPO in $REPOS
do
let COUNT++
REPONAME=`echo ${REPO} | cut -d ',' -f1`
REPOFULLNAME=`echo ${REPO} | cut -d ',' -f2`
PRIVATEFLAG=`echo ${REPO} | cut -d ',' -f3`
ARCHIVEURL=`echo ${REPO} | cut -d ',' -f4`
DEFAULTBRANCH=`echo ${REPO} | cut -d ',' -f5`
ARCHIVEURL="${ARCHIVEURL}/archive/${DEFAULTBRANCH}.zip"
FILEPATH="${BACKUP_PATH}/${DIR_NAME}/${REPONAME}.zip"
echo ${ARCHIVEURL}
curl -H "Authorization: token ${OAUTH_TOKEN}" -L ${ARCHIVEURL} > ${FILEPATH}
echo "Saved to ${FILEPATH}"
done
done
echo "Page count: $((PAGE-1))"
}
fetch_fromUrl
echo "$((COUNT)) repositories updated"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment