Listing 1 SortingHat
#!/bin/ksh
# Listing 1
# SortingHat
USAGE="Usage: $(basename $0) [-h host] [-bflr] pathname"
SORTINGHAT_DIR=/opt/SortingHat
SERVERS_DIR=$SORTINGHAT_DIR/servers
DEFAULT_DIR=$SORTINGHAT_DIR/default
BACKUP_DIR=$SORTINGHAT_DIR/backup
CURRENT_DIR=$SORTINGHAT_DIR/current
HOST=""
LIST=""
FIND=""
BACKUP=""
current_file=""
REPLACE="FALSE"
while getopts bfh:lr option
do
case "$option"
in
b)
BACKUP="TRUE";;
f)
FIND="TRUE" ;;
h)
HOST=$OPTARG;;
l)
LIST="TRUE";;
r)
REPLACE="TRUE";;
\?)
exit 1;;
esac
done
if [ "$OPTIND" -gt "$#" ]
then
echo $USAGE
exit 1
else
shift $(($OPTIND-1))
file=$1
fi
echo "\n$0..."
if [ -z "$HOST" ]
then
HOST=$(cat /etc/nodename)
else
echo "Host: $HOST"
fi
echo "File: $file"
HOST_DIR=$SERVERS_DIR/$HOST
if [ "$FIND" == "TRUE" ]
then
echo "Searching for $HOST_DIR$file"
if [ -f $HOST_DIR$file ]
then
echo "Found $HOST_DIR$file"
current_file=$HOST_DIR$file
else
echo "Searching for $DEFAULT_DIR$file"
if [ -f $DEFAULT_DIR$file ]
then
echo "Found $DEFAULT_DIR$file"
current_file=$DEFAULT_DIR$file
else
echo "Searching for $file "
if [ -f $file ]
then
echo "Found $file"
current_file=$file
else
echo "Searching for $BACKUP_DIR$file"
if [ -f $BACKUP_DIR$file ]
then
echo "Found $BACKUP_DIR$file"
current_file=$BACKUP_DIR$file
fi
fi
fi
fi
if [ -z "$current_file" ]
then
echo "Did not find a current copy of $file"
rm -f $CURRENT_DIR$file
else
if [ ! -d $CURRENT_DIR$(dirname $file) ]
then
mkdir -p $CURRENT_DIR$(dirname $file)
fi
cp -p $current_file $CURRENT_DIR$file
echo "Current copy: $CURRENT_DIR$file"
fi
fi
if [ "$BACKUP" == "TRUE" ]
then
if [ -f $file ]
then
if [ ! -d $BACKUP_DIR$(dirname $file) ]
then
mkdir -p $BACKUP_DIR$(dirname $file)
fi
backup_file=$BACKUP_DIR$file.$(date +%y%m%d.%H%M%S).$$
cp -p $file $backup_file
rm -f $BACKUP_DIR$file 2> /dev/null
ln -s $backup_file $BACKUP_DIR$file
echo "Backup copy: $BACKUP_DIR$file"
else
echo "No working file to back up."
fi
fi
if [ "$REPLACE" == "TRUE" ]
then
if [ -f "$CURRENT_DIR$file" ]
then
cp -p $CURRENT_DIR$file $file
echo "Replaced $file"
else
echo "Current copy of $file does not exist."
exit 1
fi
fi
if [ "$LIST" == "TRUE" ]
then
if [ -f "$CURRENT_DIR$file" ]
then
cat $CURRENT_DIR$file
else
echo "Current copy of $file does not exist."
exit 1
fi
fi
exit 0
|