Skip to content

Instantly share code, notes, and snippets.

@Aleksandr-ru
Created January 24, 2017 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aleksandr-ru/515e85409fe0ae0ce7eaa13061927b23 to your computer and use it in GitHub Desktop.
Save Aleksandr-ru/515e85409fe0ae0ce7eaa13061927b23 to your computer and use it in GitHub Desktop.
Git repo backup script
#!/bin/sh
# Usage:
# ./backup-repo.sh /path/to/reponame
#
# Result:
# /backup/reponame/reponame.YYYY-MM-DD.HHMMSS.tar
if [ ! -d "$1" ]; then
echo "'$1' is not a directory!"
echo "Usage:"
echo "./backup-repo.sh /path/to/reponame"
exit 1
fi
REPO_DIR=${1%/}
REPO_NAME=${REPO_DIR##*/}
LOG=/var/log/gitbot/$REPO_NAME.log
DATE=`date +%Y-%m-%d.%H%M%S`
BACKUP_DIR=/backup/$REPO_NAME
BACKUP=$BACKUP_DIR/$REPO_NAME.$DATE.tar
MIN_DAYS=1 # min days to renew backup
MAX_DAYS=30 # max days to erase old backup
if [ `find $BACKUP_DIR -ctime -$MIN_DAYS -name '*.tar' | wc -l` -gt 0 ]; then
echo "Backup already done within $MIN_DAYS days."
exit 0;
fi;
# use --exclude-vcs instead if your tar supports it
tar -cf $BACKUP $REPO_DIR --exclude $REPO_DIR/.git
if [ -s $BACKUP ] && [ $MAX_DAYS -gt 0 ]; then
find $BACKUP_DIR -ctime +$MAX_DAYS -name '*.tar' -exec rm -f {} \;
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment