Skip to content

Instantly share code, notes, and snippets.

@lattmann
Created May 2, 2015 05:04
Show Gist options
  • Save lattmann/692622e6e3aa51cf2c7a to your computer and use it in GitHub Desktop.
Save lattmann/692622e6e3aa51cf2c7a to your computer and use it in GitHub Desktop.
Pull git repository from one source and mirrors it to another remote. Saves a full bundle (--all) of the repository.
#!/bin/bash
# FIXME: add show_help function
# FIXME: add example how to use it
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Initialize our own variables:
mirror_dir=mirror
archive_dir=archive
identity_file="~/.ssh/id_rsa"
while getopts "h?a:i:m:" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
a) archive_dir=$OPTARG
;;
i) identity_file=$OPTARG
;;
m) mirror_dir=$OPTARG
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
mirror_dir=$(readlink -f ${mirror_dir})
archive_dir=$(readlink -f ${archive_dir})
echo "mirror_dir='$mirror_dir', archive_dir='$archive_dir', identity_file=$identity_file Leftovers: $@"
if [ ! -d "$mirror_dir" ]; then
# Control will enter here if $DIRECTORY doesn't exist.
echo $mirror_dir does not exist.
exit 1;
fi
mkdir -p $archive_dir
pushd $mirror_dir
pwd | xargs echo Entering
for d in */*/ ; do
pushd $d
pwd | xargs echo Entering
archive_name=$(echo $d | tr '/' '_')
len=${#archive_name}-1
archive_name=${archive_name:0:len}
ssh-agent bash -c "git fetch -p origin; ssh-add $identity_file; git push --mirror"
git bundle create $archive_dir/$archive_name.bundle --all
pwd | xargs echo Leaving
popd
done
pwd | xargs echo Leaving
popd
# End of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment