A guide and script to back up and update all your GitHub repositories, including those from your organizations, into a local directory on your Mac.
Plus: Exclude one or more of your organizations and include repositories from all over GitHub!
Note: The script uses the active Git user configured in your environment and relies on the GitHub CLI (gh) for efficient updates. Authentication is managed separately via gh auth login. The backup retrieves the full Git history with git clone.
Follow these steps to set up your macOS environment and run the backup script.
Homebrew is a package manager for macOS that simplifies software installation.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Follow any on-screen instructions to add Homebrew to your PATH.
Use Homebrew to install the necessary tools:
brew install git git-lfs gh jqNote: jq is a lightweight and flexible command-line JSON processor. The script uses it to process GitHub metadata and the config.json file.
Initialize Git LFS:
git lfs installSet up your Git user information with your GitHub credentials:
git config --global user.name "Your GitHub username"git config --global user.email "your.github.email@example.com"Generate an SSH key with a custom name and add it to your GitHub account. The keys will be stored in the .ssh directory in your home folder.
ssh-keygen -t ed25519 -f ~/.ssh/github_ed25519 -C "your.github.email@example.com"eval "$(ssh-agent -s)"ssh-add -K ~/.ssh/github_ed25519pbcopy < ~/.ssh/github_ed25519.pub- Go to https://github.com/settings/keys
- Click New SSH key, give it a title, and paste the key.
gh auth login- Choose GitHub.com.
- Select SSH.
- Use the key you created.
- Log in with your browser when prompted.
git clone https://github.com/sebfried/Backup_GitHub.git && cd Backup_GitHubThe backup script allows you to exclude specific organizations and include additional repositories from GitHub in the backup process, using a config.json file.
Rename example.config.json to config.json and update it with your preferences:
{
"exclude_orgs": ["your_organization_1", "your_org_name_2"],
"include_repos": ["other_user2/repository1", "other_org3/repository2"]
}You can exclude any organizations you don't want to back up and include any additional repositories from other users or organizations.
Make the script executable and run it:
chmod +x backup.sh./backup.shThe script will:
- Create a
backupdirectory in the same location as the script. - Set up directories for your account and each organization.
- Clone repositories if they don’t already exist locally.
- Fetch and pull updates for existing repositories.
To simplify running the backup script, you can set up an alias for it.
Add the alias using your current directory path:
echo 'alias backup-github="'"$(pwd)/backup.sh"'"' >> ~/.zshrc && source ~/.zshrcNow you can run the backup script from anywhere by simply typing backup-github in your terminal.