#!/bin/bash
#
# Execute Backup Critical Console Data and Remote Restore of Critical Console
# Data commands requiring root privilege 
#
# Description: Issue to run privileged Backup/Restore commands 
#
# Change Activity:
#   08/11/2006 brocious    565084 - initial version
#   08/16/2006 brocious    566127 - Make -d optional; always write a file on -c writeDesc
#
#STARTUSAGE
#
# Usage:
#   backupRestoreUtil -c writeDesc [-d <description_text>]
#   backupRestoreUtil -c removeDesc
#   backupRestoreUtil -c unmount
#   backupRestoreUtil -c removeFtpInfoFile
#   backupRestoreUtil -c startPortmap
#   backupRestoreUtil -c stopPortmap
#   backupRestoreUtil -c getDesc -a <archive_name>
#   backupRestoreUtil -c mount [-t <file_system_type>] [-o <options>] -s <server_info>
#
# Where:
#   -c   The operation to be performed.  Must be one of:
#        . writedesc - write the description text to the backup's description file;
#                      if no description is provided, an empty description file is written.                                         
#        . removedesc - delete the backup's description file
#        . unmount - unmount the filesystem used for the backup/restore archive
#        . removeFtpInfoFile - delete the archive information file used for 
#                              backup/restore operations via FTP
#        . startPortmap - start the portmap service
#        . stopPortmap - stop the portmap service
#        . getDesc - get the contents of the backup's description file from a
#                    specified archive file
#        . mount - mount the specified filesystem, which contains the backup archive
#
#   -a   The name of the archive file, including any path information below the
#        mount point.
#   -d   The description text for the current backup file.  Used for the 
#        writedesc operation.
#   -o   Any options to be included on the mount command
#   -s   The server and resource to be mounted.  Must be in a form appropriate
#        for the type of filesystem it provides.  e.g. //host/share, host:share
#   -t   The type of filesystem to be mounted, e.g., nfs, smbfs.
#
#ENDUSAGE
#****************************************************************************

function showUsage() {
   # Print out the prologue comments as usage info
   sed -e '/STARTUSAGE/,/ENDUSAGE/ s/^#//' -e '1,/STARTUSAGE/ d' -e '/ENDUSAGE/,$ d' "$0"
}

# Initialize some constants
descFile='/var/hsc/log/description'
ftpInfoFile='/var/hsc/log/ftp.info'
mountPoint='/mnt/remote'
backupLogFilePtr='/tmp/backuphdr.log.ptr'
backupListFile='/tmp/backup.list'

# Initialize variables that hold option values 
giveUsage=0
cmd=
desc=
archiveName=
fsType=
mountOptions=
server=

# Parse the options
while getopts 'c:a:d:o:s:t:?' optname; do
   case "$optname" in
      c) cmd="$OPTARG";;
      a) archiveName="$OPTARG";;
      d) desc="$OPTARG";;
      o) mountOptions="$OPTARG";;
      s) server="$OPTARG";;
      t) fsType="$OPTARG";;
      \?) giveUsage=1; break;;
   esac
   noopt=0
done

if [ "$giveUsage" -eq 1 ]; then
   showUsage
   exit 99
fi
      
if [ -z "$cmd" ]; then
   echo "Missing required argument command argument"
   showUsage
   exit 99
fi

#
# Handle the various requests
#

if [ "$cmd" == "writeDesc" ]; then
   # Write the description text, if any, to the backup description file
   echo "$desc" > "$descFile"

elif [ "$cmd" == "removeDesc" ]; then
   # Remove the backup description file
   rm -f "$descFile"
   exit $?

elif [ "$cmd" == "unmount" ]; then
   umount $mountPoint
   exit $?

elif [ "$cmd" == "removeFtpInfoFile" ]; then
   rm -f $ftpInfoFile
   exit $?

elif [ "$cmd" == "startPortmap" ]; then
   /etc/init.d/portmap start
   exit $?

elif [ "$cmd" == "stopPortmap" ]; then
   /etc/init.d/portmap stop
   exit $?

elif [ "$cmd" == "getDesc" ]; then
   # This request will write the description file to stdout
   if [ -z "$archiveName" ]; then
      echo "Missing required -a argument"
      showUsage
      exit 99
   fi
   # Extract the description file from the archive and, if successful, write it to stdout
   tar -xPzf $mountPoint/$archiveName $descFile || exit 1
   cat $descFile || exit 2
   rm -f $descFile
   exit 0

elif [ "$cmd" == "mount" ]; then
   # Build a string containing mount options
   if [ -n "$fsType" ]; then
      args="-t $fsType"
   fi
   if [ -n "$mountOptions" ]; then
      args="$args $mountOptions"
   fi
   if [ -n "$server" ]; then
      args="$args $server"
   else
      echo "Missing required -s argument"
      showUsage
      exit 99
   fi

   # If the mount point doesn't exist, create it now
   if [ ! -d $mountPoint ]; then
      mkdir -p $mountPoint || exit $?
   fi

   # We've got all of the pieces, now build and issue the actual mount command
   mount $args $mountPoint
   exit $?

else
   echo "Unknown command verb: $cmd"
   showUsage
   rc=99
fi
