Skip to content

Instantly share code, notes, and snippets.

@iammoen
Created January 27, 2020 16:47
Show Gist options
  • Save iammoen/12d5ad95fb3acc474e8e480bb90b2a67 to your computer and use it in GitHub Desktop.
Save iammoen/12d5ad95fb3acc474e8e480bb90b2a67 to your computer and use it in GitHub Desktop.
Function for zshrc file that will backup the current git repo by finding the git root, zipping it all up, and putting it in ~/backups/
backuprepo() {
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
declare -i LOOP=0
declare -i LOOPMAX=10
while [ ! -d "${DIR}/.git/" ] && [ "${LOOP}" -ne "${LOOPMAX}" ]; do
DIR="$( echo $DIR | sed 's/\(.*\)\/.*/\1/' )"
LOOP=$((LOOP+1))
done
if [ "${LOOP}" -ne "${LOOPMAX}" ]
then
echo "${DIR}/.git/ does exist"
echo "${DIR} should get zipped up"
FOLDER="$( echo ${DIR} | sed 's/.*\/\(.*\)/\1/' )"
BRANCH="$(git branch | grep \* | cut -d ' ' -f2)"
DATETIME=`date +%Y-%m-%d_%H-%M-%S`
FILENAME="${FOLDER}__ON__${BRANCH}__@__${DATETIME}"
tar --exclude='node_modules' -cf ~/backup/"${FILENAME}".tar -C "${DIR}" .
tput setaf 3
tput setab 0
echo "Done zipping ${FILENAME}"
tput sgr0
else
tput setaf 1
tput setab 7
echo "No .git directory found in folder hierarchy"
tput sgr0
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment