Create a git mirror
Posted by mazet on 15 Avr 2021 in Systeme
Create a shell script mirror_git.sh
to mirror a remote git server.
Shell
#!/bin/sh | |
| |
AUTH=myLogin | |
PROTOCOL=https | |
SERVER=remoteHost | |
GITPATH=gitPath | |
REPOS="repos1 repos2 repos3" | |
| |
# global variable | |
PROG=$(basename $0) | |
VERBOSE="yes" | |
| |
# formatting function | |
title() { [ $VERBOSE = no ] || echo "\033[0;1m$*\033[0;0m"; } | |
pass() { [ $VERBOSE = no ] || echo "\033[1;32m$*\033[0;0m"; } | |
warn() { [ $VERBOSE = no ] || echo "\033[1;33m$*\033[0;0m"; } | |
fail() { [ $VERBOSE = no ] || echo "\033[1;31m$*\033[0;0m"; } | |
| |
# help message | |
usage() { | |
echo "$PROG [-h|--help] [-q|--quiet]" | |
exit $1 | |
} | |
| |
# argument check | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
-h|--help) usage 0;; | |
-q|--quiet) VERBOSE="no";; | |
*) usage 1;; | |
esac | |
shift | |
done | |
| |
# get current directory | |
PWD=$(dirname $0) | |
[ "$PWD" ] || PWD=. | |
| |
# action | |
for repos in $REPOS; do | |
if [ -d $PWD/$repos.git ]; then | |
title "Fetch $repos" | |
( cd $PWD/$repos.git && git fetch --tags && pass OK || fail KO ) | |
title "Push $repos" | |
( cd $PWD/$repos.git && git push --mirror && pass OK || fail KO ) | |
title "Prune $repos" | |
( cd $PWD/$repos.git && git fetch --prune && pass OK || fail KO ) | |
else | |
title "Clone $repos" | |
( cd $PWD && echo git clone --mirror "$PROTOCOL://$AUTH@$SERVER/$GITPATH/$repos" && pass OK || fail KO ) | |
fi | |
done |
Init your local git server by running previous script.
Setup cron task to automagically keep your mirror up-todate.
Don't forget to sore your credential and use following command:
Shell
git config --global credential.helper store |
This entry was posted by mazet and filed under Systeme.