#!/bin/bash
# ----------------------------------------------------------------------
# mikes handy rotating-filesystem-snapshot utility: daily snapshots
# ----------------------------------------------------------------------
# intended to be run daily as a cron job when hourly.3 contains the
# midnight (or whenever you want) snapshot; say, 13:00 for 4-hour snapshots.
# ----------------------------------------------------------------------

# ------ I suggest you put this on a daily cron.

unset PATH

# ------------- system commands used by this script --------------------
ID=/usr/bin/id;
ECHO=/bin/echo;

MOUNT=/bin/mount;
RM=/bin/rm;
MV=/bin/mv;
CP=/bin/cp;

# ------------- file locations -----------------------------------------

# ----- CHANGE THESE TO MATCH YOUR SETUP, should be the same as the make_backup script
MOUNT_DEVICE=/dev/hdb1;		# Device the backup is stored on
REMOUNT=0;			# Remount backup device Read-Only?
SOURCE=/;			# Source of the files to back up
SNAPSHOT_RW=/storage/snapshot;	# Directory to output the files (within $MOUNT_DEVICE of course)
EXCLUDES=/etc/backup_exclude;	# Files/Subdirs to exclude from backup

# ------------- the script itself --------------------------------------

# make sure we're running as root
if (( `$ID -u` != 0 )); then { $ECHO "Sorry, must be root.  Exiting..."; exit; } fi

if [ -d $SNAPSHOT_RW ]; then
        $ECHO "$SNAPSHOT_RW folder exists..";
else
        $ECHO "$SNAPSHOT_RW does not exist..  Wrong drive?";
        exit;
fi  

# attempt to remount the RW mount point as RW; else abort
if (( $REMOUNT == 1 )); then
	$MOUNT -o remount,rw $MOUNT_DEVICE $SNAPSHOT_RW ;
	if (( $? )); then
	{
		$ECHO "snapshot: could not remount $SNAPSHOT_RW readwrite";
		exit;
	}
	fi;
fi;

# step 1: delete the oldest snapshot, if it exists:
if [ -d $SNAPSHOT_RW/daily.5 ] ; then			\
$RM -rf $SNAPSHOT_RW/daily.5 ;				\
fi ;

# step 2: shift the middle snapshots(s) back by one, if they exist
if [ -d $SNAPSHOT_RW/daily.4 ] ; then			\
$MV $SNAPSHOT_RW/daily.4 $SNAPSHOT_RW/daily.5 ;	\
fi;
if [ -d $SNAPSHOT_RW/daily.3 ] ; then			\
$MV $SNAPSHOT_RW/daily.3 $SNAPSHOT_RW/daily.4 ;	\
fi;
if [ -d $SNAPSHOT_RW/daily.2 ] ; then			\
$MV $SNAPSHOT_RW/daily.2 $SNAPSHOT_RW/daily.3 ;	\
fi;
if [ -d $SNAPSHOT_RW/daily.1 ] ; then			\
$MV $SNAPSHOT_RW/daily.1 $SNAPSHOT_RW/daily.2 ;	\
fi;
if [ -d $SNAPSHOT_RW/daily.0 ] ; then			\
$MV $SNAPSHOT_RW/daily.0 $SNAPSHOT_RW/daily.1;	\
fi;

# step 3: make a hard-link-only (except for dirs) copy of
# hourly.3, assuming that exists, into daily.0
if [ -d $SNAPSHOT_RW/hourly.3 ] ; then			\
$CP -al $SNAPSHOT_RW/hourly.3 $SNAPSHOT_RW/daily.0 ;	\
fi;

# note: do *not* update the mtime of daily.0; it will reflect
# when hourly.3 was made, which should be correct.

# now remount the RW snapshot mountpoint as readonly

if (( $REMOUNT == 1 )); then
	$MOUNT -o remount,ro $MOUNT_DEVICE $SNAPSHOT_RW ;
	if (( $? )); then
	{
		$ECHO "snapshot: could not remount $SNAPSHOT_RW readonly";
		exit;
	} fi;
fi;

