Tuesday, November 11, 2008

Reduce patch timing - Shell script

#!/bin/sh
#
# adpatch_auto a.k.a aa
#
# Ver Date Author Description
# --- --------- ---------- ---------------------------
# 1.0 30-OCT-00 G. Roberts Created
# 1.1 09-MAY-07 G. Roberts Extended for zip files
#
# Automated adpatch for R11i
# Only use for patches you've already done manually, or in non-PROD environments
# Notes: You must have your environment set correctly
# You must have precreated defaults.txt
# Patch must be in PT (see config below)
# Patch driver number must be same as patch number

if [ "$#" != "2" -a "$#" != "3" -a "$#" != "4" -a "$#" != "5" ]; then
echo "Usage: aa PATCH_NUM DRIVERFILE_PREFIX [RESTART=YN [INTERACTIVE=YN]]"
echo " Simple e.g: aa 123456 u"
echo " Restart e.g: aa 123456 u Y Y"
exit 1
fi

# CONFIGURATION ITEMS
# Set PT to where your base patch directory is
PT=/patch; export PT
# LOGFILE is a file keeping track of aa run history
LOGFILE=$APPL_TOP/admin/p.log; export LOGFILE
# DEFFILE is your defaults.txt probably defaults.txt or adalldefaults.txt
DEFFILE=$APPL_TOP/admin/${TWO_TASK}/adalldefaults.txt
# Set DOUNZIP to Y if you want aa to search for and unzip p*.zip
DOUNZIP=Y; export DOUNZIP
# Set SETMAINT to Y to automatically switch on and off maintenance mode
# Only needed for late'ish versions 11.5.10 or higher
SETMAINT=Y; export SETMAINT
# Date format for logging
DF="+%y%m%d %H:%M:%S"

# Check CONTEXT_NAME is set
if [ -z "${CONTEXT_NAME}" ]; then
echo "CONTEXT_NAME is not set!"
exit 1
else
CTXT=${CONTEXT_NAME}; export CTXT
fi

ORIGDIR="`pwd`"

PNUM=$1; export PNUM
DRIV=$2; export DRIV
PATCH=$PNUM; export PATCH
if [ ! -z "$3" ]; then
if [ "$3" = "N" ]; then
REST="restart=no"; export REST
else
REST="restart=yes"; export REST
fi
fi
if [ "$#" = "4" ]; then
if [ "$4" = "Y" ]; then
INTERACT=yes; export INTERACT
else
INTERACT=no; export INTERACT
fi
else
INTERACT=no; export INTERACT
fi

echo "Patch number set to $PNUM"
echo "Driver set to $DRIV"
echo "Restart set to $REST"
echo "Interactive set to $INTERACT"
echo
echo `date "$DF"`" Starting $DRIV $PNUM $REST $INTERACT"
echo `date "$DF"`" Starting $DRIV $PNUM $REST $INTERACT" >> $LOGFILE

cd $PT
if [ "$DOUNZIP" = "Y" ]; then
if [ ! -d "$PNUM" ]; then
echo `date "$DF"`" Looking for zip files to unzip"
ZFILE=`ls -tr *$PNUM*.zip awk '{print $1}'`; export ZFILE
if [ -n "$ZFILE" ]; then
if [ -f "$ZFILE" ]; then
echo `date "$DF"`" Unzipping $ZFILE"
echo `date "$DF"`" Unzipping $ZFILE" >> $LOGFILE
unzip "$ZFILE"
else
echo `date "$DF"`" ERROR: File $PT/*$PNUM*.zip does not exist."
echo `date "$DF"`" ERROR: File $PT/*$PNUM*.zip does not exist." >> $LOGFILE
exit 1
fi
else
echo `date "$DF"`" ERROR: File $PT/*$PNUM*.zip does not exist."
echo `date "$DF"`" ERROR: File $PT/*$PNUM*.zip does not exist." >> $LOGFILE
exit 1
fi
fi
fi

if [ ! -d $PNUM ]; then
echo `date "$DF"`" ERROR: Directory $PT/$PNUM does not exist."
echo `date "$DF"`" ERROR: Directory $PT/$PNUM does not exist." >> $LOGFILE
exit 1
fi

if [ "$SETMAINT" = "Y" ]; then
echo `date "$DF"`" Enable maintenance mode"
echo `date "$DF"`" Enable maintenance mode" >> $LOGFILE
sqlplus -s apps/`get_pw db apps` @$AD_TOP/patch/115/sql/adsetmmd.sql ENABLE
fi

echo `date "$DF"`" Calling adpatch"
echo `date "$DF"`" Calling adpatch" >> $LOGFILE
adpatch \
defaultsfile=$DEFFILE \
logfile=${DRIV}${PNUM}_$CTXT.log \
patchtop=$PT/$PATCH \
driver=${DRIV}${PNUM}.drv \
workers=6 interactive=$INTERACT options=novalidate $REST
#workers=6 interactive=$INTERACT options=novalidate,nocheckfile $REST

if [ -d $PT/$PATCH/backup ]; then
echo `date "$DF"`" mv $PT/$PATCH/backup to $PT/$PATCH/backup_$CTXT"
echo `date "$DF"`" mv $PT/$PATCH/backup to $PT/$PATCH/backup_$CTXT" >>$LOGFILE
mv $PT/$PATCH/backup $PT/$PATCH/backup_$CTXT
fi

if [ "$SETMAINT" = "Y" ]; then
echo `date "$DF"`" Disable maintenance mode"
echo `date "$DF"`" Disable maintenance mode" >> $LOGFILE
sqlplus -s apps/`get_pw db apps` @$AD_TOP/patch/115/sql/adsetmmd.sql DISABLE
fi

cd "$ORIGDIR"

echo `date "$DF"`" Finished applying $DRIV $PNUM $INTERACT"
echo `date "$DF"`" Finished applying $DRIV $PNUM $INTERACT" >> $LOGFILE