Last active
July 23, 2020 12:18
-
-
Save somoso/c27fb23930ed8bfb1145fd32aa5eb029 to your computer and use it in GitHub Desktop.
Clone a Git repository from source to destination.
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 | |
| set -ea | |
| # Function to generate a random string for making a temp folder with our repo contents | |
| function randomstring { | |
| echo $(openssl rand -base64 12) | |
| } | |
| # Check parameters | |
| if [ "$#" -lt "2" ]; then | |
| echo "not enough arguments, specify the original repo and the repo to clone into" | |
| exit -1 | |
| fi | |
| # Generate random dummy path locations to do our cloning in | |
| folder1=/tmp/$(randomstring) | |
| # Generate another random dummy path location to do our verification in | |
| folder2=/tmp/$(randomstring) | |
| # Do the actual backup operation | |
| # The --mirror param helps us in the cloning | |
| git clone --mirror $1 $folder1 | |
| cd $folder1 | |
| git remote add new-origin $2 | |
| git push new-origin --mirror | |
| # Verify everything works | |
| cd .. | |
| git clone --mirror $2 $folder2 | |
| cd $folder2 | |
| success=0 | |
| if [ $(find $folder2 -maxdepth 0 -type d -empty 2>/dev/null) ]; then | |
| echo "git cloning was unsuccessful" | |
| success=-1 | |
| fi | |
| # Cleanup after our mess | |
| cd .. | |
| rm -rf $folder1 | |
| rm -rf $folder2 | |
| exit $success |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment