A shell script for Linux that automatically backs up your GitHub repositories to your external hard drive.
This script backs up all OWNED repositories, including private ones.
If you also want to back up repositories that are part of an organization, follow these steps:
- Modify the API query: Either:
- Remove
&affiliation=ownerfrom thecurlrequest, or - Update the request URL to:
This change ensures that the script backs up repositories where you are an owner, collaborator, or organization member (the default affiliation).
'https://api.github.com/user/repos?per_page=100&page=${page}&visibility=all&affiliation=owner,collaborator,organization_member'
- Remove
For more details about the GitHub API endpoint, visit: GitHub Repositories API Documentation
-
Copy the
.env.examplefile to.env:cp .env.example .env
-
Set the values in the
.envfile:- GITHUB_USERNAME: Your GitHub username (e.g., TomTruyen)
- GITHUB_TOKEN: Your GitHub access token (e.g., abcdefghij123456798) - Generate it at GitHub Token Settings
- OUTPUT_PATH: The location where you want your backups to be saved (e.g., ~/Desktop/Backups)
-
Ensure you have your SSH key and Git Config stored in the
github-backup-scriptdirectory:id_ed25519andid_ed25519.pubshould be present in the directory of theDockerfiledirectory.- If you use a different SSH key, modify the Dockerfile to copy the correct key files.
.gitconfigshould be present in the directory of theDockerfile
To start the script using Docker, run:
docker-compose up -d --buildbash: ./github_backup_script.sh: /usr/bin/bash^M: bad interpreter: No such file or directorySolution
- Install
dos2unix
sudo apt install dos2unix- Convert the script
dos2unix -k -o github_backup_script.sh- Try to run it again