Skip to content

Instantly share code, notes, and snippets.

@rokiden
Created August 7, 2024 10:59
Show Gist options
  • Save rokiden/cbe0be92b458f6d6e8501033721fb4f3 to your computer and use it in GitHub Desktop.
Save rokiden/cbe0be92b458f6d6e8501033721fb4f3 to your computer and use it in GitHub Desktop.
Git mirror from one remote server to another through local or readonly mirror using Nginx
sudo docker run --name gitmir -v $PWD:/usr/share/nginx/html:ro nginx
# from remote server to local for dumb http server
if [ "$#" -ne 0 ] && [ "$#" -ne 1 ]; then
echo "Invalid number of arguments."
echo "Usage: $0 [source_repo_url mirror_repo_url]"
exit 1
fi
if [ "$#" -eq 1 ]; then
git clone --mirror $1 && \
cd "$(basename "$_" .git).git" && \
for ref in $(git for-each-ref --format='%(refname)' refs/pull/); do \
git update-ref -d $ref; \
done \
else
git fetch origin '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*'; \
fi && \
git update-server-info
# from one remote server to another through local
if [ "$#" -ne 0 ] && [ "$#" -ne 2 ]; then
echo "Invalid number of arguments."
echo "Usage: $0 [source_repo_url mirror_repo_url]"
exit 1
fi
if [ "$#" -eq 2 ]; then
git clone --mirror $1 && \
cd "$(basename "$_" .git).git" && \
git remote add mirror $2 && \
for ref in $(git for-each-ref --format='%(refname)' refs/pull/); do \
git update-ref -d $ref; \
done \
else
git fetch origin '+refs/heads/*:refs/heads/*' '+refs/tags/*:refs/tags/*'; \
fi && \
git push mirror 'refs/heads/*' 'refs/tags/*'
# to update all mirrors in working dir
for d in *.git; do
echo $d && \
cd $d && \
../mirror_repo.sh && \
cd ..
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment