Created
April 12, 2018 09:01
-
-
Save pingec/2d97f320b292a0a0f6fc79b683ea16e4 to your computer and use it in GitHub Desktop.
Create gitolite repos snapshots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DATE=$(date +%Y-%m-%d) | |
DEST_BACKUP_PREFIX="repositories" | |
BACKUP_NAME="${DEST_BACKUP_PREFIX}_${DATE}.tar" | |
#echo $DATE $DEST_BACKUP_PREFIX | |
#echo $BACKUP_NAME | |
SOURCE="/home/git/repositories" | |
BACKUP_PATH="/mnt/ainas.iscsi-storage.GIT-Backup-01/backups" | |
BACKUP_PATH_DAILY="$BACKUP_PATH/daily" | |
BACKUP_PATH_WEEKLY="$BACKUP_PATH/weekly" | |
BACKUP_PATH_MONTHLY="$BACKUP_PATH/monthly" | |
#echo $BACKUP_PATH_DAILY $BACKUP_PATH_WEEKLY $BACKUP_PATH_MONTHLY | |
MAX_DAILY_BACKUPS=5 | |
MAX_MONTHLY_BACKUPS=12 | |
MAX_WEEKLY_BACKUPS=4 | |
#echo $MAX_DAILY_BACKUPS $MAX_MONTHLY_BACKUPS $MAX_WEEKLY_BACKUPS | |
GITOLITE="/home/git/gitolite/src/gitolite" | |
mkdir -p "$BACKUP_PATH_DAILY" | |
mkdir -p "$BACKUP_PATH_WEEKLY" | |
mkdir -p "$BACKUP_PATH_MONTHLY" | |
function cleanup_daily { | |
# 2>/dev/null will supress stderr on no matches | |
DAILY_COUNT=$(ls ${BACKUP_PATH_DAILY}/${DEST_BACKUP_PREFIX}_* 2>/dev/null | wc -l) | |
let "TO_DELETE = $DAILY_COUNT - $MAX_DAILY_BACKUPS" | |
#echo $TO_DELETE | |
if [ "$TO_DELETE" -gt 0 ] | |
then | |
#ls ${BACKUP_PATH_DAILY}/${DEST_BACKUP_PREFIX}_* 2>/dev/null | sort -r | tail -n ${TO_DELETE} | |
TO_DELETE_FILES=$(ls ${BACKUP_PATH_DAILY}/${DEST_BACKUP_PREFIX}_* 2>/dev/null | sort -r | tail -n ${TO_DELETE}) | |
echo deleting files... $TO_DELETE_FILES | |
echo $TO_DELETE_FILES | xargs rm | |
fi | |
} | |
function backup_daily { | |
$GITOLITE writable @all off "Git backup in progress, try later" | |
echo creating "$BACKUP_PATH_DAILY/$BACKUP_NAME" from "$SOURCE" | |
tar -cf "$BACKUP_PATH_DAILY/$BACKUP_NAME" "$SOURCE" | |
$GITOLITE writable @all on | |
cleanup_daily | |
} | |
function cleanup_weekly { | |
# 2>/dev/null will supress stderr on no matches | |
WEEKLY_COUNT=$(ls ${BACKUP_PATH_WEEKLY}/${DEST_BACKUP_PREFIX}_* 2>/dev/null | wc -l) | |
let "TO_DELETE = $WEEKLY_COUNT - $MAX_WEEKLY_BACKUPS" | |
#echo $TO_DELETE | |
if [ "$TO_DELETE" -gt 0 ] | |
then | |
TO_DELETE_FILES=$(ls ${BACKUP_PATH_WEEKLY}/${DEST_BACKUP_PREFIX}_* 2>/dev/null | sort -r | tail -n ${TO_DELETE}) | |
echo deleting files... $TO_DELETE_FILES | |
echo $TO_DELETE_FILES | xargs rm | |
fi | |
} | |
function backup_weekly { | |
if ! [[ $(date +%d) = "01" || $(date +%d) = "08" || $(date +%d) = "15" || $(date +%d) = "22" ]] | |
then | |
return | |
fi | |
$GITOLITE writable @all off "Git backup in progress, try later" | |
echo creating "$BACKUP_PATH_WEEKLY/$BACKUP_NAME" from "$SOURCE" | |
tar -cf "$BACKUP_PATH_WEEKLY/$BACKUP_NAME" "$SOURCE" | |
$GITOLITE writable @all on | |
cleanup_weekly | |
} | |
function cleanup_monthly { | |
# 2>/dev/null will supress stderr on no matches | |
MONTHLY_COUNT=$(ls ${BACKUP_PATH_MONTHLY}/${DEST_BACKUP_PREFIX}_* 2>/dev/null | wc -l) | |
let "TO_DELETE = $MONTHLY_COUNT - $MAX_MONTHLY_BACKUPS" | |
#echo $TO_DELETE | |
if [ "$TO_DELETE" -gt 0 ] | |
then | |
TO_DELETE_FILES=$(ls ${BACKUP_PATH_MONTHLY}/${DEST_BACKUP_PREFIX}_* 2>/dev/null | sort -r | tail -n ${TO_DELETE}) | |
echo deleting files... $TO_DELETE_FILES | |
#echo $TO_DELETE_FILES | xargs rm | |
fi | |
} | |
function backup_monthly { | |
if ! [[ $(date +%d) = "05" ]] | |
then | |
return | |
fi | |
$GITOLITE writable @all off "Git backup in progress, try later" | |
echo creating "$BACKUP_PATH_MONTHLY/$BACKUP_NAME" from "$SOURCE" | |
tar -cf "$BACKUP_PATH_MONTHLY/$BACKUP_NAME" "$SOURCE" | |
$GITOLITE writable @all on | |
cleanup_monthly | |
} | |
backup_daily | |
backup_weekly | |
backup_monthly | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment