Skip to content

Instantly share code, notes, and snippets.

@LoesterFranco
Forked from akishin/gitsync.sh
Created September 23, 2021 14:26
Show Gist options
  • Save LoesterFranco/a9114d162d4a7bf66f47359577b2a3fb to your computer and use it in GitHub Desktop.
Save LoesterFranco/a9114d162d4a7bf66f47359577b2a3fb to your computer and use it in GitHub Desktop.
gitsync.sh
#!/bin/sh
ORIGIN="serverA"
MIRROR="serverB"
SYNC_USER="git"
ORIGIN_REPO_ROOT="/home/$SYNC_USER/repositories"
MIRROR_REPO_ROOT="/home/$SYNC_USER/repositories"
LOGFILE=$ORIGIN_REPO_ROOT/gitsync.log
reponame=$(basename $(pwd))
echo "[`date +"%Y/%m/%d %H:%M:%S"`] ${reponame} sync start." >> ${LOGFILE}
exists=`ssh $SYNC_USER@$MIRROR ls -l $MIRROR_REPO_ROOT/$reponame 2>>$LOGFILE | wc -l`
if [ $exists != 0 ]; then
echo "[`date +"%Y/%m/%d %H:%M:%S"`] mirror repository ${reponame} already exists. \"git push --mirror\" execute." >> ${LOGFILE}
`git push --mirror $SYNC_USER@$MIRROR:$MIRROR_REPO_ROOT/$reponame >> $LOGFILE 2>&1`
else
echo "[`date +"%Y/%m/%d %H:%M:%S"`] mirror repository ${reponame} does not exist. \"git clone --mirror\" execute." >> ${LOGFILE}
`ssh $SYNC_USER@$MIRROR "(cd $MIRROR_REPO_ROOT ; git clone --mirror $SYNC_USER@$ORIGIN:$ORIGIN_REPO_ROOT/$reponame >> $LOGFILE 2>&1)"`
fi
echo "[`date +"%Y/%m/%d %H:%M:%S"`] ${reponame} sync end." >> ${LOGFILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment