Skip to content

Instantly share code, notes, and snippets.

@icoloma
Last active March 2, 2018 04:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save icoloma/2af0aaa087d918f15fe9 to your computer and use it in GitHub Desktop.
Save icoloma/2af0aaa087d918f15fe9 to your computer and use it in GitHub Desktop.
Backup a Github repository into a local file. Useful when retiring a Github repository, but keeping a backup copy for a rainy day in Dropbox / Drive / Box.
#!/bin/bash
# Backup the repositories indicated in the command line
# Example:
# bin/backup user1/repo1 user1/repo2
set -e
for i in $@; do
FILENAME=$(echo $i | sed 's/\//-/g')
echo "== Backing up $i to $FILENAME.bak"
git clone git@github.com:$i $FILENAME.git --mirror
cd "$FILENAME.git"
git bundle create ../$FILENAME.bak --all
cd ..
rm -rf $i.git
echo "== Repository saved as $FILENAME.bak"
done
#!/bin/bash
# Restore the repository indicated in the command line
# Example:
# bin/restore filename.bak
set -e
FOLDER_NAME=$(echo $1 | sed 's/.bak//')
git clone --bare $1 $FOLDER_NAME.git
#!/bin/bash
# Verify the backup file. Both the .bak file and the corresponding .git folder must exist
# Example:
# bin/verify filename1.bak filename2.bak
set -e
for i in $@; do
FOLDER_NAME=$(echo $i | sed 's/.bak/.git/')
echo "== Contents of $i"
cd $FOLDER_NAME
echo "Last commit: $(git log --name-status HEAD^..HEAD -n1 | egrep '( )|(Date)' )"
cd ..
echo
git bundle list-heads $i
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment