mirrorgit

Simple Script for Mirroring git Repos
git clone git://git.ckyln.com/mirrorgit.git
Log | Files | Refs | README | LICENSE

mirrorgit.sh (4351B)


      1 #!/bin/sh
      2 
      3 ## mirrorgit.sh, a simple shell script to mirror repositories
      4 ## Copyright (C) 2019 Cem Keylan <cem@ckyln.com>
      5 
      6 ##    This program is free software: you can redistribute it and/or modify
      7 ##    it under the terms of the GNU General Public License as published by
      8 ##    the Free Software Foundation, either version 3 of the License, or
      9 ##    (at your option) any later version.
     10 ##
     11 ##    This program is distributed in the hope that it will be useful,
     12 ##    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 ##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 ##    GNU General Public License for more details.
     15 ##
     16 ##    You should have received a copy of the GNU General Public License
     17 ##    along with this program.  If not, see <https://www.gnu.org/licenses/>.
     18 
     19 initialize() {\
     20 	clear
     21 	printf "███╗   ███╗██╗██████╗ ██████╗  ██████╗ ██████╗  ██████╗ ██╗████████╗ \n████╗ ████║██║██╔══██╗██╔══██╗██╔═══██╗██╔══██╗██╔════╝ ██║╚══██╔══╝ \n██╔████╔██║██║██████╔╝██████╔╝██║   ██║██████╔╝██║  ███╗██║   ██║ \n██║╚██╔╝██║██║██╔══██╗██╔══██╗██║   ██║██╔══██╗██║   ██║██║   ██║ \n██║ ╚═╝ ██║██║██║  ██║██║  ██║╚██████╔╝██║  ██║╚██████╔╝██║   ██║ \n╚═╝     ╚═╝╚═╝╚═╝  ╚═╝╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═╝ ╚═════╝ ╚═╝   ╚═╝ \n"
     22 	printf "\nHello, I see you don't have a configuration file on $HOME/.mirrorgitrc\n\nLet's set it up!\n\nYou need to enter your git username first (leave blank if your server does not require a username)\nUsername: "
     23 	read username
     24 	printf "Now, add the domain you will be mirroring from\nFOR SSH: type 'git@example.com' or 'user@example.com'\nFOR HTTP(S): type 'http://git.host.com'\nURL: "
     25 	read fromhost
     26 	printf "You should now add the domain you will be mirroring to.\nIMPORTANT: You must have your ssh keys deployed on this git repository\nDo you understand? (y/n) "
     27 	read answer
     28 	case $answer in
     29 		y*)
     30 			printf "You should type your mirror's host name like 'git@host.com' or 'user@host.com'\nEnter host name: "
     31 			read tohost
     32 			printf "Enter the mirror username if you have one\nUsername: "
     33 			read touser
     34 			printf "\
     35 FROMUSER=\"$username\"\n\n\
     36 ## Add only the host name\n\
     37 FROMHOST=\"$fromhost\"\n\
     38 ## Mirrorhost should not contain https or http (eg. \"github.com\")\n\
     39 MIRRORHOST=\"$tohost\"\n\
     40 MIRRORUSER=\"$touser\"\n" > $HOME/.mirrorgitrc
     41 			;;
     42 		*)
     43 				printf "Did you read the prompt carefully?\n" && exit 1
     44 			;;
     45 		esac			
     46 	printf "Repos you will be mirroring (leave spaces between repos)\nRepos: "
     47 	read repos
     48 	printf "REPONAMES=\"${repos}\"\n" >> $HOME/.mirrorgitrc
     49 }
     50 
     51 [ -f $HOME/.mirrorgitrc ] || initialize
     52 . $HOME/.mirrorgitrc
     53 
     54 [ -z $FROMHOST ] && printf "You must add your own git repo address before you can use the script\n" && exit 1
     55 [ -z $MIRRORHOST ] && printf "You must add the git repo address you will be mirroring to before you can use this script\n" && exit 1
     56 [ -z "$REPONAMES" ] && printf "You must add repositories before you can use this script\n" && exit 1
     57 
     58 case $MIRRORHOST in
     59 	https://*) MIRRORHOST=$(echo $MIRRORHOST | cut -c 9-) ;;
     60 	http://*) MIRRORHOST=$(echo $MIRRORHOST | cut -c 8-) ;;
     61 esac
     62 
     63 case $FROMHOST in
     64 	http*)
     65 		if [ -z $FROMUSER ]
     66 		then
     67 			FROMURL="$FROMHOST/"
     68 		else
     69 			FROMURL="$FROMHOST/$FROMUSER/"
     70 		fi
     71 		;;
     72 	*@*)
     73 		if [ -z $FROMUSER ]
     74 		then
     75 			FROMURL="$FROMHOST:"
     76 		else
     77 			FROMURL="$FROMHOST:$FROMUSER/"
     78 		fi
     79 		;;
     80 esac
     81 
     82 if [ -z $MIRRORUSER ]
     83 then
     84 	MIRRORURL="$MIRRORHOST:"
     85 else
     86 	MIRRORURL="$MIRRORHOST:$MIRRORUSER/"
     87 fi
     88 
     89 ORIGFOLD=$PWD
     90 TEMPFOLD=$(mktemp -d /tmp/gitmirror-XXXXXX)
     91 
     92 cd $TEMPFOLD
     93 for i in $REPONAMES
     94 do
     95 	git clone --mirror $FROMURL$i || exit 1
     96 	cd "$i".git
     97 	git push --mirror $MIRRORURL$i || exit 1
     98 	cd $TEMPFOLD 
     99 done
    100 
    101 printf "\nDone\n"
    102 cd $ORIGFOLD