Skip to content

Instantly share code, notes, and snippets.

@CCampJr
Created July 6, 2018 22:06
Show Gist options
  • Save CCampJr/e9c697a00fb5db842466a64c7bc0df33 to your computer and use it in GitHub Desktop.
Save CCampJr/e9c697a00fb5db842466a64c7bc0df33 to your computer and use it in GitHub Desktop.
Bash script to search for git repos in a directory, check last backup date, bundle if necessary, and copy to 2 backup locations
#!/bin/sh
# COMMENT
DAY=`date +"%Y%m%d-%H-%M"`
# Directory to find Repos
DIR=/cygdrive/c/Users/chc/Documents/Python/
DESTINATION=E:/ReposBackup/
NETWORKDEST=X:/ReposBackup/
for d in `find $DIR -maxdepth 2 -name .git`; do
cd $d/..;
PREFIX=${PWD##*/}
# Last backup file, full path
LASTBUNDLE=`find $DESTINATION -name *$PREFIX.bundle | sort -r | head -n 1`
if [ "$LASTBUNDLE" == "" ]
then
# There was no previous bundle, so this bundle just gets all of
# the commits.
echo "First backup for repo $PREFIX"
`git bundle create $DESTINATION$DAY-$PREFIX.bundle --all`
`cp $DESTINATION$DAY-$PREFIX.bundle $NETWORKDEST$DAY-$PREFIX.bundle`
else
# There's a previous bundle, so let's see if there has been a commit
# since then
echo "Repo $PREFIX previously backed up in $LASTBUNDLE"
LASTFILE=${LASTBUNDLE##*/}
LASTDATE=$(echo $LASTFILE | cut -d'-' -f 1)
LASTHOUR=$(echo $LASTFILE | cut -d'-' -f 2)
LASTMIN=$(echo $LASTFILE | cut -d'-' -f 3)
echo "Last backup on $LASTDATE at $LASTHOUR:$LASTMIN"
COMMIT=`git log -1 --format=%cd --date=format:"%Y%m%d-%H-%M"`
LASTCOMMITDATE=$(echo $COMMIT | cut -d'-' -f 1)
LASTCOMMITHOUR=$(echo $COMMIT | cut -d'-' -f 2)
LASTCOMMITMIN=$(echo $COMMIT | cut -d'-' -f 3)
echo "Last commit on $LASTCOMMITDATE at $LASTCOMMITHOUR:$LASTCOMMITMIN"
if (($(($LASTCOMMITDATE$LASTCOMMITHOUR$LASTCOMMITMIN-$LASTDATE$LASTHOUR$LASTMIN)) > 0))
then
echo "New commit(s) since last backup"
`git bundle create $DESTINATION$DAY-$PREFIX.bundle --all`
`cp $DESTINATION$DAY-$PREFIX.bundle $NETWORKDEST$DAY-$PREFIX.bundle`
else
echo "Backed-up since last commit"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment