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=owner
from thecurl
request, 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.example
file to.env
:cp .env.example .env
-
Set the values in the
.env
file:- 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-script
directory:id_ed25519
andid_ed25519.pub
should be present in the directory of theDockerfile
directory.- If you use a different SSH key, modify the Dockerfile to copy the correct key files.
.gitconfig
should be present in the directory of theDockerfile
To start the script using Docker, run:
docker-compose up -d --build
bash: ./github_backup_script.sh: /usr/bin/bash^M: bad interpreter: No such file or directory
Solution
- Install
dos2unix
sudo apt install dos2unix
- Convert the script
dos2unix -k -o github_backup_script.sh
- Try to run it again