Last active
October 16, 2025 03:40
-
-
Save michaelteter/0556752d38ac5038e80066d909059cae to your computer and use it in GitHub Desktop.
Keep a safe backup of your github repos!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # script name: repobak.sh | |
| # | |
| # Keep a separate backup of all your repos, and keep them synced periodically. | |
| # DO NOT make changes to any of these backed up repos locally; you want them pristine. | |
| # Do your development in a different folder with your repo, such as (~/work/<my_repo>). | |
| # | |
| # Consider adding this to your crontab: | |
| # 0 3 * * * /path/to/your/repobak.sh # run repobak at 3am daily. | |
| # --- Configuration --- | |
| # Your GitHub username. | |
| GITHUB_USER="<your github username>" | |
| # The local directory where you want to store the backups. | |
| # Use an absolute path to ensure the script works correctly with cron. | |
| BACKUP_DIR="/path/to/your/github_backups" | |
| # A list of your repository names (the part after your username). | |
| # Your repo name is the last part of https://github.com/<yourusername>/<the_repo_name> | |
| # Add all your repository names inside the parentheses, separated by spaces. | |
| REPOS=( | |
| "my_repo_1" | |
| "my_second_repo" | |
| ... | |
| ) | |
| # --- End of Configuration --- | |
| # Ensure the parent backup directory exists. | |
| mkdir -p "$BACKUP_DIR" | |
| echo "Starting GitHub backup process..." | |
| echo "---------------------------------" | |
| # Loop through each repository name in the list. | |
| for repo_name in "${REPOS[@]}"; do | |
| local_repo_path="$BACKUP_DIR/$repo_name" | |
| remote_repo_url="https://github.com/$GITHUB_USER/$repo_name.git" | |
| # Check if the local repository directory already exists. | |
| if [ -d "$local_repo_path" ]; then | |
| # The directory exists, so we sync it. | |
| echo "Syncing '$repo_name'... -----------------------------" | |
| cd "$local_repo_path" || continue # Go into the repo dir, or skip if cd fails | |
| git pull --prune | |
| else | |
| # The directory does not exist, so we clone it. | |
| echo "Cloning '$repo_name' for the first time..." | |
| git clone "$remote_repo_url" "$local_repo_path" | |
| fi | |
| done | |
| echo "---------------------------------" | |
| echo "GitHub backup process finished." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment