This Bash script automates cloning or updating all repositories owned by a the authenticated user. It uses the GitHub API and SSH to securely back up repositories to a local directory.
- Fetches all repositories owned by the authenticated user.
- Supports
--dry-runmode to preview actions without cloning. - Automatically handles pagination for large accounts.
- Uses
git clone --recurse-submodulesfor full repository backups.
- bash
- git
- curl
- jq
- A valid GitHub personal access token with Read access to metadata.
Set your target directory in the script:
CLONE_DIR="/home/username/my-github-projects"Export your GitHub token before running:
export GITHUB_TOKEN="your_token_here"Start the SSH agent before running:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# Afterwards kill the agent with:
ssh-agent -kMake the script executable:
chmod +x backup_github_repos.sh./backup_github_repos.sh -h
Usage: backup_github_repos.sh [options]
Options:
--dry-run Print actions without cloning
-h, --help Show this help and exit
Environment:
GITHUB_TOKEN GitHub token (required)Perform a dry run (no cloning):
# No cloning, just print actions
./backup_github_repos.sh --dry-run
# Clone or update repositories
./backup_github_repos.sh- Cloned repositories are stored in the directory specified by
CLONE_DIR. - Existing repositories are updated.
- Progress and errors are printed to the console.
This script is provided as-is under the MIT License.