Skip to content

Instantly share code, notes, and snippets.

@iandark
Created August 8, 2023 20:15
Show Gist options
  • Select an option

  • Save iandark/fed2ca036d6069eebc90cab7c9d78970 to your computer and use it in GitHub Desktop.

Select an option

Save iandark/fed2ca036d6069eebc90cab7c9d78970 to your computer and use it in GitHub Desktop.
This is a git mirror bare script
#!/bin/bash
# Hey, this is a mirroring bare script, but if you just want a simple backup try this: https://gist.github.com/iandark/d79d08a75f444647afa82744af8fea60
# This shell script reads a CSV file, where you can pass an origin repository and makes a whole backup into another repository.
#
# The expected format of the CSV file is below.
#
# +--------------------------------------------+-----------------------------------------------+
# | Origin | Backup |
# +--------------------------------------------+-----------------------------------------------+
# | git@github.com:username-x/repository-x.git | git@bitbucket.org:username-y/repository-y.git |
# +--------------------------------------------+-----------------------------------------------+
# | git@github.com:username-a/repository-a.git | git@gitlab.com:username-b/repository-b.git |
# +--------------------------------------------+-----------------------------------------------+
#
# To use this script, pass your CSV file as a parameter;
# $ ./smart-mirroring-bare.sh file_name.csv
#
# PS: You should be able to use SSH and HTTPS (but I only tested ssh).
#
# PS-2: Probably you have to give this script permission to execute; grant +x.
# $ sudo chmod +x smart-mirroring-bare.sh
path=$EPOCHSECONDS
pathCsv=$1
if [ "$pathCsv" == "" ]
then
echo "Please, provide a CSV file."
echo "Example: ./script.sh file_name.csv"
exit 0
fi
mkdir -p /tmp/$path
line=1
missing=false
while IFS=, read -r originRepo backupRepo
do
originRepo="$(echo -e "${originRepo}" | tr -d '[:space:]')"
backupRepo="$(echo -e "${backupRepo}" | tr -d '[:space:]')"
folder="${originRepo##*/}"
if [ "$originRepo" == "Origin" -a "$backupRepo" == "Backup" ]
then
continue
fi
if [ "$originRepo" == "" -o "$backupRepo" == "" ]
then
echo "WARNING: Origin or backup repository is empty or no value set on line" $line
missing=true
break
elif [[ $originRepo != *git ]] || [[ $backupRepo != *git ]]
then
echo "WARNING: You provided an invalid repository URL on your CSV on line" $line
missing=true
break
else
mkdir -p /tmp/$path
cd /tmp/$path
git clone --bare $originRepo
cd $folder
git push --mirror $backupRepo
fi
((line=line+1))
done < $pathCsv
if [ "$missing" == true ]
then
echo "WARNING: Missing valid values in your CSV file. Operation failed. Please use the proper format."
exit 1
else
echo "Backup git repositories successfully."
fi
rm -rf /tmp/$path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment