#!/bin/sh
#
# Snapshot retention
#
# Create tar-ball
#

# RETENTION_PERIOD
RETENTION_PERIOD=7

# SNAPSHOT_LOCATION
SNAPSHOT_LOCATION=/usr/data/backup/snapshots/

# RETENTION_LOCATION
RETENTION_LOCATION=/usr/data/backup/archive-snapshots

DATESTAMP=`date "+%Y%m%d"`

TARBALL="$RETENTION_LOCATION/snapshot-$DATESTAMP.tar.bz2"

tar -cjf $TARBALL $SNAPSHOT_LOCATION
echo "Created $TARBALL"
echo "Free space:"
df -h /usr/data

COUNT_SNAPSHOTS=`ls $RETENTION_LOCATION/snapshot-*.tar.bz2 | sort | wc -l`

OVERFLOW=`echo $COUNT_SNAPSHOTS - $RETENTION_PERIOD | bc`

if [ $OVERFLOW -gt 0 ];
then
	for files in `ls $RETENTION_LOCATION/snapshot-*.tar.bz2 | sort | head -n$OVERFLOW`;
	do
		 rm $files
		 echo "Removed $files"
	done
fi

