#!/bin/sh

# Copyright C 2002-2008 by Isilon Systems, Inc. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.


#
# isi_backup
#
# This file accepts a single argument: the full path to a file to be
# backed up.  This script will keep up to 9 backups of the file.
#

# The file we're watching
watchfile="$1"
sendalert="$2"

if [ ! -f $watchfile ]; then
	logger -p ifs.warn "Warning: file $watchfile missing"
	if [ "$sendalert" = "alert" ]; then
		logger -p istat.warn \
			"OneFS: Node `hostname` is missing file $watchfile"
	fi
  exit 1
fi

# no backup or most recent backup doesn't match current
if [ ! -f $watchfile.0 ] || ! cmp -s $watchfile $watchfile.0 ; then

	# keep 9 backups (0-8, inclusive)
	i=8

	# shuffle through, decrementing backup files
	while [ $i != "0" ]
	do
		j=`/bin/expr $i - 1`
		/bin/mv $watchfile.$j $watchfile.$i > /dev/null 2>&1
		i=$j
	done

	# store current as first backup
	/bin/cp $watchfile $watchfile.0 > /dev/null 2>&1
fi
