How to Backup all your GitHub repositories to your NAS

How to Backup all your GitHub repositories to your NAS

Created
Jul 27, 2022
Tags
Backup
Tech
Many developers do not realize this, but we might lose access to all our code base if, for some reason, GitHub is down (This would be a worst case scenario, better be safe than sorry).
As part of our conservative backup strategy, we run a daily backup of all our repositories into our Synology NAS.
 
Save this script, edit the variables and run it daily to be able to backup your GitHub repositories.
This supports private and public repositories (and paginate all available repos).
#!/bin/bash # Edit this values BACKUP_PATH="/volume1/homes/YOUR_USERNAME/github" # Backup destination OAUTH_TOKEN="" # Token from https://github.com/settings/tokens GITHUB_USERNAME="" # You github usernmame # Fetch all repositories ALL_REPOSITORIES="" fetch_repositories() { PAGE=1 while : do echo "Getting page $PAGE" if [ -z "$OAUTH_TOKEN" ] then PAGE_REPOSITORIES=`curl "https://api.github.com/users/${GITHUB_USERNAME}/repos?per_page=100&page=${PAGE}" | jq -r '.[] | "\(.name),\(.full_name),\(.private),\(.html_url)"'` else PAGE_REPOSITORIES=`curl -H "Authorization: token ${OAUTH_TOKEN}" -s "https://api.github.com/user/repos?per_page=100&page=${PAGE}" | jq -r '.[] | "\(.name),\(.full_name),\(.private),\(.html_url)"'` fi TOTAL_PAGE_REPOSITORIES=`echo $PAGE_REPOSITORIES | tr -cd " " | wc -c` ALL_REPOSITORIES="${ALL_REPOSITORIES} ${PAGE_REPOSITORIES}" if [ "$TOTAL_PAGE_REPOSITORIES" = "99" ]; then let PAGE++ else break; fi done } fetch_repositories TOTAL_REPOSITORIES=`echo $ALL_REPOSITORIES | tr -cd " " | wc -c` echo "Backing up $TOTAL_REPOSITORIES repositories" COUNT=1 for REPO in $ALL_REPOSITORIES do REPO_NAME=`echo ${REPO} | cut -d ',' -f1` REPO_FULLNAME=`echo ${REPO} | cut -d ',' -f2` REPO_OWNER=`echo ${REPO_FULLNAME} | cut -d '/' -f1` PRIVATE_FLAG=`echo ${REPO} | cut -d ',' -f3` ARCHIVE_URL=`echo ${REPO} | cut -d ',' -f4` ARCHIVE_URL="${ARCHIVE_URL}/archive/master.zip" mkdir -p "${BACKUP_PATH}/${REPO_OWNER}" FILE_PATH="${BACKUP_PATH}/${REPO_OWNER}/${REPO_NAME}.zip" curl -H "Authorization: token ${OAUTH_TOKEN}" -L ${ARCHIVE_URL} -o ${FILE_PATH} -s echo "${COUNT}/${TOTAL_REPOSITORIES}: Saved ${FILE_PATH}" let COUNT++ done echo "Done!"
 
To run this task daily:
  • On Synology’s DSM: Go to Control Panel > Task Scheduler > Create User defined script, and on the task settings set the script to bash /volume1/homes/YOUR_USERNAME/backup-scripts/github.sh
  • On any server: Add the following crontab 0 0 * * * bash /volume1/homes/YOUR_USERNAME/backup-scripts/github.sh
 

This article is brought you by tonoïd – we are a micro-startup studio building small businesses that are profitable and solve a specific problem without any external funding nor billion-dollar market-size. Most notably, RefurbMe, a comparison site for refurbished products – and Notion Automations.
If you have any feedback, do not hesitate to reach out at
✉️
Contact