Skip to content

Instantly share code, notes, and snippets.

@derekmurawsky
Created September 6, 2024 17:09
Show Gist options
  • Select an option

  • Save derekmurawsky/29c43ec91d9b8f2e92085df019249aff to your computer and use it in GitHub Desktop.

Select an option

Save derekmurawsky/29c43ec91d9b8f2e92085df019249aff to your computer and use it in GitHub Desktop.
A simple git repo backup script. It can back up a list of git repos with the -m option, or update a list of already mirrored directories with the -u option.
#!/bin/bash
# This simple script backs up all the repositories in a list
# To generate the list, you can do something like the following:
# gh repo list [org_name] --limit [limit] --json nameWithOwner --jq '.[] | "https://github.com/"+.nameWithOwner+".git"' >> git_urls.txt
# Then you can use this script to run a `git clone --mirror` for each repository in the list
# To get a worktree from the repobackup, run `git clone mirrored_repository normal_repository`
while getopts "hum" option; do
case $option in
h) # display Help
echo "Usage: backup-git.sh [option...]"
echo "Options:"
echo "h Display help"
echo "u Update mirrors"
echo "m Mirror repositories from a git_urls.txt file"
exit
;;
u) # update mirrors
for d in *.git/; do
echo "Updating $d"
(cd "$d" && git remote update && git lfs fetch --all)
done
exit
;;
m) # mirror repositories
while read p; do
git clone --mirror "$p"
done <git_urls.txt
exit
;;
*) # Any other input
echo "Invalid option"
exit
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment