#!/bin/ksh
########################################################################
#*                                                                     *
#*             Copyright (c) Ultra Electronics Limited 2007            *
#*                         All rights reserved                         *
#*                                                                     *
#***********************************************************************
#*
#* Filename:           move_files_to_tape.ksh
#* Subsystem Name:     Database backup
#* Originating Author: Gary Roberts
#* Design Reference:   UAS/FALCP/SY/02478
#*
#***********************************************************************
#*
#* Description:
#*   This script is intended to move backed up files to magnetic tape.
#*   The script searches a supplied logfile for the names of the backup
#*   -pieces, which should be of the form: 
#*
#*      "piece handle=<backup piece filename>".
#*
#*   These files will then be moved to tape using gnutar if available, 
#*   or otherwise tar will be used. The -r flag dictates whether
#*   or not the tape should be retained once the move is complete. If
#*   the -r flag is not supplied then the tape will be ejected. 
#* 
#*
#* Usage:
#*       move_files_to_tape.ksh [-e <expired directory>]
#*                              [-l <rman_log_name>] 
#*                              [-r ]
#*                              [-t <tape device>]
#*
#*
#* Options:
#*
#*     -e Specify an optional directory whose contents will be dumped
#*        to the tape along with the RMAN backup pieces. Intended for
#*        use with the directory into which archive exports expired
#*        data.
#*
#*     -l Name of log file containing names of the files to send to
#*        tape.
#*
#*     -r This option instructs the script to retain the tape once the
#*        tar operation has finished.
#*
#*     -t Specifies the tape device to be used. If no device is given
#*        then the script defaults to /dev/null and logs an error.
#*
#*
#***********************************************************************
#* Modification History
#*
#* Rel Date/Time      Proj/Id   CA Ref/Details Of Change
#* --- -------------- --------  ---------------------------------------
#* 1   17/05/07 17:00 RMAN	SY02478 - UltraTrak RMAN backup process
#*
#**********************************************************************/


#***********************************************************************
#  Initialise the variables that will be used to store command line
#  arguments to their default values.
#***********************************************************************

RMAN_LOG_NAME=""
RETAIN_TAPE="no"
TAPE_DEVICE="/dev/null"

#***********************************************************************
#  USAGE function
#***********************************************************************

USAGE="Usage: $0 [-e <expired directory] [-l rman_log_name] [-r] [-t <tape device>]"


#***********************************************************************
#  Read in the command line options
#***********************************************************************

while getopts e:l:rt: options
do
case $options in
e) EXPIRED_DIR=$OPTARG ;;
l) RMAN_LOG_NAME=$OPTARG ;;
r) RETAIN_TAPE="yes" ;;
t) TAPE_DEVICE=$OPTARG ;;
[?]) echo $USAGE 
     exit 1 ;;
esac
done

if [[ ${RMAN_LOG_NAME} = "" ]];
then
echo $USAGE
exit 1
fi

if [[ ${RETAIN_TAPE} != "yes" && ${RETAIN_TAPE} != "no" ]];
then
echo $USAGE
exit 1
fi

#***********************************************************************
#  Detect OS Version 
#***********************************************************************
OSVER=`uname`

#***********************************************************************
#  Check that the RMAN log exists, is not empty and is readable
#***********************************************************************
if [[ ! -s ${RMAN_LOG_NAME} || ! -r ${RMAN_LOG_NAME} ]];
then
    logger "move_files_to_tape.ksh: ERROR: RMAN log file,"\
           "$RMAN_LOG_NAME, does not exist, is empty or is not readable."
    exit 1
fi

#**********************************************************************
#  Read backup piece names from backup log
#**********************************************************************
piece_name_list=`grep "^piece handle=" $RMAN_LOG_NAME | sed 's/^[^ ]*\
[ ][^ ]*=//g' | awk '{print $1}'`

piece_name_list=`echo "${piece_name_list} ${EXPIRED_DIR}" | sed 's/$//g'`


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create a variable holding the name of a temporary file which will be
# used to see if gnutar is available.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TEMPFILE=/tmp/gnutar_test$$


#**********************************************************************
# Write an entry in the RMAN_LOG informing the user that we are
# dumping to tape. 
#**********************************************************************

echo "" | tee -a ${RMAN_LOG_NAME}
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++" | tee -a ${RMAN_LOG_NAME}
echo "Dumping to tape. Output of tar/gnutar command follows" | tee -a ${RMAN_LOG_NAME}
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++" | tee -a ${RMAN_LOG_NAME}

#**********************************************************************
#  If detected OS version is AIX 
#**********************************************************************
if [[ $OSVER = "AIX" ]];
then
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Check for existence of gnutar
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    touch ${TEMPFILE}
    gnutar -cvf /dev/null ${TEMPFILE} >/dev/null 2>&1
    if [[ $? -eq 0 ]];
    then
        #
        # gnutar is available, set gnutar flag
        #
        USE_GNUTAR="Y"
    else
        USE_GNUTAR="N"
    fi
    rm -f ${TEMPFILE}
   
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Tar up each of the backuppieces one by one.
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if [[ ${USE_GNUTAR} = "Y" ]];
    then
        gnutar -cvf ${TAPE_DEVICE} `echo $piece_name_list` 2>&1 | tee -a ${RMAN_LOG_NAME}
        # Check for errors during the move
        if [[ $? -ne 0 ]];
        then
            logger "move_files_to_tape.ksh: ERROR: Return code of"\
                   "gnutar command is non-zero for backup piece:"\
                   "$piecename. The rest of the archive process"\
                   "will now be aborted." 
            exit 1
        fi
    else 
        tar -cvf ${TAPE_DEVICE} `echo $piece_name_list` 2>&1 | tee -a ${RMAN_LOG_NAME}
        if [[ $? -ne 0 ]];
        then
            logger "move_files_to_tape.ksh: ERROR Return code of"\
                   "tar command is non-zero for backup piece:"\
                   "$piecename. The rest of the backup process"\
                   "will now be aborted."
        fi
    fi

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Eject the tape if required
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if [[ $RETAIN_TAPE = "no" ]];
    then
        mt -f ${TAPE_DEVICE} offline
    fi

#***********************************************************************
#  If detected OS version is SunOS (Solaris) - tar will therefore be used.
#***********************************************************************
elif [[ $OSVER = "SunOS" ]];
then
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Check for existence of gnutar and set USE_GNUTAR flag
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    touch ${TEMPFILE}
    gnutar -cvf /dev/null ${TEMPFILE} >/dev/null 2>&1
    if [[ $? -eq 0 ]];
    then
        #
        # gnutar is available, set gnutar flag
        #
        USE_GNUTAR="Y"
    else
        USE_GNUTAR="N"
    fi
    rm -f ${TEMPFILE}

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Tar up each of the backuppieces one by one.
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    if [[ x${USE_GNUTAR} = "xY" ]];
    then
        gnutar -cvf ${TAPE_DEVICE} `echo $piece_name_list` 2>&1 | tee -a ${RMAN_LOG_NAME}
        # Check for errors during the move
        if [[ $? -ne 0 ]];
        then
            logger "move_files_to_tape.ksh: ERROR: Return code of"\
                   "gnutar command is non-zero for backup piece:"\
                   "$piecename. The rest of the backup process"\
                   "will now be aborted."
            exit 1
        fi
    else
        tar -cvf ${TAPE_DEVICE} `echo $piece_name_list` 2>&1 | tee -a ${RMAN_LOG_NAME}
        if [[ $? -ne 0 ]];
        then
            logger "move_files_to_tape.ksh: ERROR Return code of"\
                   "tar command is non-zero for backup piece:"
                   "$piecename. The rest of the backup process"\
                   "will now be aborted"
        fi
    fi
    

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Eject the tape if required
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if [[ $RETAIN_TAPE = "no" ]];
    then
        mt -f ${TAPE_DEVICE} offline
    fi

else
    logger "move_files_to_tape.ksh: ERROR: unable to detect OS"\
           "version. The rest of the archive process will now be"\
           "aborted."
    exit 1
fi
