#! /bin/sh
#
# Program: fbr
# Current Revision: 1.10
# Last Modification Date: 03/08/05
# Last Modified by: %N%
#
# Copyright 2002,2003 Sun Microsystems, Inc.  All Rights Reserved
# Use is subject to license terms.
#
# FBR Script
#
# This script provides the ability to archive (backup) or
# recover (restore) critical personality files for the
# Storage Service Processor from the flash USB disk device
# attached to the Service Processor Core USB port.
#
# The user interface is simple, requiring a single argument
# describing the direction of flow, backup or restore.
#
# fbr -b
# fbr -r
#
# This script locates the list of files, looking first
# to use the list maintained by SSRR or if not found it
# uses a local list of files.
#
# It then looks for the USB flash disk device to be located
# on the Service Processor using devfsadm and cfgadm commands.
#
# If it is found, it will be mounted and the requested operation
# performed.  It will then be unmounted.
#

PKGNAME=SUNWsefbru
PKGBASE=$INSTALL_BASE/opt
PGMNAME=`basename $0`
LOGGER=$INSTALL_BASE/opt/se6x20/bin/log

FBR_FILE_LIST=$PKGBASE/$PKGNAME/files/backup_list

WORK_DIR=/tmp
TARGET_FILENAME=SP_Personality

OP=

FLASH_DIR=/flash
FLASHDRIVE_FOUND=FALSE

COMPRESSED=FALSE

USAGE="\nUSAGE: $PGMNAME -[brc?]\n       $PGMNAME -b backs up the Personality to the flash disk\n       $PGMNAME -r restores the Personality from the flash disk\n       $PGMNAME -c clears the log file - typically used by cron jobs only\n       $PGMNAME -? shows usage"

#
# Verify that an argument list has been specified
#
ARGCNT=$#
	if [ $ARGCNT = "0" ]
	then
		DATE=`date '+%m/%d/%y %H:%M:%S'`
		echo "$DATE: $PGMNAME: OPER_ERROR: Missing Argument[s]"
		echo $USAGE
		exit 1
	fi
#
# Parse arg list and validate request
#
	while getopts bcdr? c
	do
		case $c in
		b)   OP=backup;;
		r)   OP=restore;;
		/?)  echo $USAGE
		     exit 2;;
		*)   echo $USAGE
		     exit 2;;
		esac
	done

#
# If doing a backup - set-up file list
#
if [ $OP = "backup" ]
then
	FILE_LIST=$FBR_FILE_LIST
fi

for i in `ls /dev/rdsk`
do
	DEV=`echo $i`
	if [ "$DEV" != "" ]
	then
		if [ `echo $i | grep -c "s2"` -gt 0 ]
		then
			RDEV=/dev/rdsk/${i}	# Raw Drive for Inquiry
			FDEV=/dev/dsk/${i}	# Formatted Drive for mount
			PHYS_PATH_LINK=`ls -l $RDEV | grep usb` 
			if [ "$PHYS_PATH_LINK" !=  "" ]
			then
				FLASHDRIVE_FOUND=TRUE
				break
			fi
		fi
	fi
done

if [ $FLASHDRIVE_FOUND = "FALSE" ]
then
	$LOGGER PERSISTENCE_IO_ERROR_ON_STORAGE
	exit 7
fi

#
# Mount identified USB flash disk device as /flash
#
STAT=`fsck -m $RDEV >/dev/null 2>&1;echo $?`
case $STAT in
	0|32|33)  ;;
	*)   
	     STAT=`mkfs -F ufs $RDEV 30720 32 64 8192 1024 16 10 3 2048 t 0 -1 8 16 >/dev/null 2>&1;echo $?`
	     if [ $STAT -ne 0 ]
	     then
		$LOGGER PERSISTENCE_IO_ERROR
		umount $FLASH_DIR
		exit 6
	     fi;;
esac

if [ ! -d /flash ]
then
	mkdir /flash
	chmod 0755 /flash
fi

STAT=`mount $FDEV /flash >/dev/null 2>&1; echo $?`
if [ $STAT -ne 0 ]
then 
	$LOGGER PERSISTENCE_IO_ERROR
	exit 8
fi

#
# Perform the requested service
#

if [ $OP = backup ]
then

#	Log timestamp when backup begins
	DATE=`date '+%m/%d/%y %H:%M:%S'`

	LIST=`cat $FILE_LIST`

#	Create Personality TAR file of backup file list contents

	tar -cpf $WORK_DIR/$TARGET_FILENAME.tar $LIST 2>&1 >/dev/null
		
#	Compress Personality file to reduce storage space

	compress $WORK_DIR/$TARGET_FILENAME.tar

#	Save off backup tar in $WORK_DIR
#	Could be useful for service

	cp $WORK_DIR/$TARGET_FILENAME.tar.Z $WORK_DIR/$TARGET_FILENAME.tar.Z.backup

#	Copy Personality file to flash disk if any files have been modified
#	since last update

	CMPSTAT=`cmp $WORK_DIR/$TARGET_FILENAME.tar.Z $FLASH_DIR/$TARGET_FILENAME.tar.Z >/dev/null 2>&1; echo $?`
	if [ $CMPSTAT -ne 0 ]
	then 
		cp $WORK_DIR/$TARGET_FILENAME.tar.Z $FLASH_DIR/$TARGET_FILENAME.tar.Z
	fi

#	Cleanup work area
	rm $WORK_DIR/$TARGET_FILENAME.tar.Z

#	Done with flash disk - unmount it

	umount $FLASH_DIR

fi

if [ $OP = "restore" ]
then
#	Copy Personality file from flash disk to working directory

	if [ -f $FLASH_DIR/$TARGET_FILENAME.tar.Z ]
	then
		cp $FLASH_DIR/$TARGET_FILENAME.tar.Z $WORK_DIR/$TARGET_FILENAME.tar.Z
		uncompress $WORK_DIR/$TARGET_FILENAME.tar.Z

	else
		$LOGGER PERSISTENCE_IO_ERROR_ON_FILE $FLASH_DIR/$TARGET_FILENAME.tar.Z
		umount $FLASH_DIR
		exit 3
	fi

#	Restoring Personality files to Service Processor
	tar -xpf $WORK_DIR/$TARGET_FILENAME.tar 2>&1 >/dev/null


#	Clean up work area

	rm $WORK_DIR/$TARGET_FILENAME.tar

#	Unmounting flash disk device

	umount $FLASH_DIR

fi
exit 0
#
# End of FBR Script
#
