PSoft clone script, 1st part


#!/bin/ksh
#
# Script:     psoft_CLONE.ksh
# Purpose:    this script will automatically clone a PSoft
#             database. The only pre-requisite is that you
#             ensure TWO THINGS:
#             1. You have stopped the PSoft AppServer and Process
#             schedulers, AND
#             2. You KNOW the name of the database you’re cloning
#             (i.e., tstct90)
# Usage:      psoft_CLONE.ksh <db_name>
#               
#             for instance:      # psoft_CLONE.ksh tstct90
#
# Created:    10/26/2012, CWMack
# Modified:
#
###################################################################################
#
unset ORACLE_HOME
unset ORACLE_SID
#
MAIL=/bin/mailx
MAILTO=”the-gang@abc.com,gandalf@abc.com”
#
# let’s run this on nrac01 only……
#
#
BOX=`hostname | awk -F. ‘{print $1}’`
#
if [[ $BOX != “nrac01” ]]
then
   echo “This script must be run on nrac01…..”
   echo “”
   exit
fi
#
#
do_not ()
{
   echo “DO NOT use an instance name (i.e. tstct901)”
   sleep 2
   echo “Use a database name (i.e., tstct90)”
   sleep 2
   echo “”
   echo “Notice the missing ONE at the end…. YOU DON’T NEED THE ONE!”
   echo “”
}
usage ()
{
   clear
   echo “”
   echo “”
   echo “This command needs an argument, for instance:”
   echo “”
   echo “# clone_db.ksh tstct90”
   echo “”
   echo “In this example, tstct90 would be the argument, which is the”
   echo “name of the database you want cloned.”
   echo “”
   echo “”
   sleep 5
   do_not
}
#
clear
#
if [[ $# != 1 ]]
then
   usage
   exit
fi
#
printf ‘\t\t\t ################################\n’
echo “”
printf ‘\t\t\t CHECK your Odyssey Access Client\n’
echo “”
printf ‘\t\t\t ################################\n\n\n’
#
print -n “Have you renewed/extended your session?: [y/n] “
read odyssey
if [[ $odyssey != [yY] ]]
then
   echo “”
   echo “Renew/Extend your session and then re-run this script…..”
   echo “”
   exit
fi
#
echo “”
echo “”
#
printf ‘\t\t\t ###############################\n’
echo “”
printf ‘\t\t\t Have you brought down Services?\n’
echo “”
printf ‘\t\t\t ###############################\n\n\n’
#
print -n “Have services been brought down for this database?: [y/n] “
read services
if [[ $services != [yY] ]]
then
   echo “”
   echo “Shutdown services then restart the clone script…..”
   echo “”
   exit
fi
#
clear;sleep 1
printf ‘\t\t\t #######################\n’
printf ‘\t\t\t PeopleSoft Clone Script\n’
printf ‘\t\t\t #######################\n’
echo “”
echo “”
#
#=-=-= give the user a chance to bail if they typed the incorrect database
#
#
echo “Current list of running databases:”
echo “”
ps -ef | grep smon | egrep -v ‘ASM|grep’ | awk ‘{print $8}’ | awk -F_ ‘{print $3}’ | sort | \
tr ‘[A-Z]’ ‘[a-z]’ | sed ‘s/01/0/g’ | paste – –
#
echo “”
echo “”
echo “You’ve selected $1 as the database to clone”
echo “”
#
print -n “Is this the correct name of the database you want cloned?: [y/n] “
read answer
if [[ $answer != [yY] ]]
then
   echo “OK then, let’s try again”
   sleep 1
   echo “”
   print -n “Enter the correct database (i.e., tstct90): “
   read answer
   set $answer $1
fi
#
echo $1 > /tmp/sid.tmp
sid=`cat /tmp/sid.tmp`
SID=`echo $1 | tr ‘[a-z]’ ‘[A-Z]’`
PWD2=`sed -e ‘s/90//’ /tmp/sid.tmp`
#
# let’s grab some database/SID stuff and build an environment
#
#
if grep ct90 /tmp/sid.tmp
   then
      export ORACLE_HOME=/opt/app/oracle/product/11.2.0/cs
      export ORACLE_SID=”${SID}1″
      export SERV=prdct90
      export SERV1=PRDCT90
      export LOG_DIR=/s01/dbback/${SERV1}
      export RB=/s01/dbback/${SERV1}/rmanback
      PWD=`sed -e ‘s/[a-z]\{3\}//’ -e ‘s/90//’ /tmp/sid.tmp`
elif grep ft90 /tmp/sid.tmp
   then
      export ORACLE_HOME=/opt/app/oracle/product/11.2.0/fs
      export ORACLE_SID=”${SID}1″
      export SERV=prdft90
      export SERV1=PRDFT90
      export LOG_DIR=/s03/dbback/${SERV1}
      export RB=/s03/dbback/${SERV1}/rmanback
      PWD=`sed -e ‘s/[a-z]\{3\}//’ -e ‘s/90//’ /tmp/sid.tmp`
elif grep ht90 /tmp/sid.tmp
   then
      export ORACLE_HOME=/opt/app/oracle/product/11.2.0/hc
      export ORACLE_SID=”${SID}1″
      export SERV=prdht90
      export SERV1=PRDHT90
      export LOG_DIR=/s02/dbback/${SERV1}
      export RB=/s02/dbback/${SERV1}/rmanback
      PWD=`sed -e ‘s/[a-z]\{3\}//’ -e ‘s/90//’ /tmp/sid.tmp`
else
   echo “”
   echo “Oops… are you sure about that database name?”
   sleep 2
   echo “I can’t formulate a correct tns service name…..”
   sleep 2
   echo “Exiting the script, please try again.”
   echo “”
   exit
fi
#
export RUN_THIS=$LOG_DIR/run_this
export GAWD=/opt/app/11.2.0/grid/bin/srvctl
export LD=$LOG_DIR
export CLONE_DIR=/s01/shared_scripts/clone_pfiles
export SQL=$ORACLE_HOME/bin/sqlplus
export RMAN=$ORACLE_HOME/bin/rman
#
#
# this file gets touched when the ASM cleanout script is running….
# we need to check if the asm cleanout file is there and give the user a
# chance to bail if he/she wants
#
if [[ -e $CLONE_DIR/asm_cleanout_running.tmp ]]
then
   echo “”
   echo “There appears to be an ASM cleanout script already running for `cat $CLONE_DIR/asm_cleanout_running.tmp`”
   echo “that was created on `ls -l $CLONE_DIR/asm_cleanout_running.tmp | awk ‘{print $6,$7,”at”,$8}’`.”
   echo “”
   echo “If your building a clone for a different database, then continue, OR,”
   echo “If you’re unsure, there is NO harm done if you exit now and re-run later.”
   echo “”
   print -n “Do you wish to continue with the clone?: [y/n] “
   read asm
   if [[ $asm == [yY] ]]
   then
      rm -f $CLONE_DIR/asm_cleanout_running.tmp
   else
      echo “”
      echo “……. exiting”
      exit
   fi
fi
#
CFIN=/tmp/clone_finish.txt
CFIN2=/tmp/clone_finish2.txt
echo “$SID clone started at `date`” > $CFIN
echo “” >> $CFIN
#
#
# check to ensure the script is there and bail if not…
#
if [[ -e $RB/run_this-${sid}.txt ]]
then
   :
else
   echo “”
   echo “the RMAN duplicate script appears to be missing…”
   sleep 1
   echo “file should be in the $RB directory”
   sleep 1
   echo “exiting……”
   echo “”
   exit
fi
#
#
# let the analyst see that the backup files are there before
# proceding…
#
echo “”
ls -l $RB
echo “”
echo “”
print -n “Are the files there and current (less than 24hours old?: [y/n] “
read answer
if [[ $answer != [yY] ]]
then
   echo “”
   echo “Let’s exit the script so you can talk to the DBA and ensure”
   echo “the files are what you need for the clone.”
   sleep 2
   echo “”
   exit
fi
#
$MAIL -s “Clone of $SID begun at `date`” < $CFIN $MAILTO
#
#=-=-=-=-=-=-=-=-=-=
# Bring’em down already…..
#=-=-=-=-=-=-=-=-=-=
#
echo “bringing down the database…..”
echo “”
#
$GAWD stop database -d $SID
#
SMON=`ps -ef | egrep -v grep | grep smon | grep $sid`
#
if (( $? == 0 ))
then
   echo “The database is still running!”
   echo “”
   echo “script is exiting…..”
   echo “”
   exit
fi
#
# at this point, I need to clear out ASM….
# 30 second loop checking for the 2nd trigger script
# and when found, continue on….
#
# need to echo out the SID to the ASM script…. or put in a file for it
# to pickup we’ll use this as the trigger file for the ASM script to
# pickup and run out of cron
#
#
echo $SID > $CLONE_DIR/asm_SID.tmp
chmod 777 $CLONE_DIR/asm_SID.tmp
#
# run a 10 second loop and look for the trigger file from the ASM clean
# out script
#
#
while true
do
   if [[ -e /tmp/asm_done.tmp ]]
   then
      break
   else
      sleep 10
      date
   fi
done
#
echo “looks like ASM is done…. continuing on with the RMAN duplicate portion….”
echo “”
#
# need to startup node 1 RAC instance with static vanilla spfile
#
#
$SQL /nolog <<EOF
connect / as sysdba
create spfile from pfile=’${CLONE_DIR}/init.${SID}.ora’;
startup nomount;
exit;
EOF
#
#
#perform the RMAN duplicate process….
#
$RMAN target sys/PASSWD${PWD}1@${SERV} auxiliary / @$RB/run_this-${sid}.txt
#
PFILE=$LD/pfile.tmp
#
#
rm $PFILE $LD/pfile_[1-3].tmp
#
$SQL /nolog <<EOF
connect / as sysdba
create pfile=’$PFILE’ from spfile;
exit;
EOF
#
mv $ORACLE_HOME/dbs/spfile${ORACLE_SID}.ora /tmp
#
$SQL /nolog <<EOF
connect / as sysdba
shutdown immediate;
exit;
EOF
#
# Getting the control files for rebuilding after the clone is done
#
#
grep control_files $PFILE | awk -F, ‘{print $1}’ | awk -F= ‘{print $2}’ > $LD/pfile_1.tmp
grep control_files $PFILE | awk -F, ‘{print $2}’ > $LD/pfile_2.tmp
#
echo -n `cat $LD/pfile_1.tmp $LD/pfile_2.tmp` > $LD/pfile_3.tmp
CFILES=`sed -e “s/’ ‘/’,’/g” -e “s/#Restore Controlfile//g” $LD/pfile_3.tmp`
#
#
$SQL /nolog <<EOF
connect / as sysdba
startup nomount;
alter system set control_files=$CFILES scope=spfile;
shutdown immediate;
startup mount;
alter database flashback off;
alter database noarchivelog;
alter database open;
alter user sys identified by PASSWD${PWD2}1;
alter user system identified by PASSWD${PWD2}2;
create or replace directory dpump1 as ‘/s01/dbback/exports/${SID}’;
create or replace directory dpump2 as ‘/s02/dbback/exports/${SID}’;
create or replace directory dpump3 as ‘/s03/dbback/exports/${SID}’;
grant read,write on directory dpump1 to system;
grant read,write on directory dpump2 to system;
grant read,write on directory dpump3 to system;
update psdbowner set dbname = ‘${SID}0’;
update sysadm.psoptions set GUID = ‘ ‘;
update sysadm.psprcsrqst set runstatus = ‘9’ where runstatus in (‘4′,’5′,’6′,’7’);
update sysadm.psprcsrqst set diststatus = ‘5’ where diststatus <> ‘5’;
commit;
@/home/oracle/dba_scripts/sqlt/sqlt/install/sqcsilent2.sql ” PASSWD#1 USERS TEMP ” T
exit;
EOF
#
$GAWD start instance -d $SID -i ${SID}2
$GAWD start instance -d $SID -i ${SID}3
$GAWD start instance -d $SID -i ${SID}4
#
echo “$SID clone finished at `date`” >> $CFIN
echo “” >> $CFIN
echo “Below is a status of the new clone at this time….” >> $CFIN
echo “” >> $CFIN
echo “” >> $CFIN
#
$SQL /nolog <<EOF
connect / as sysdba
set trims on
spool $CFIN2
select instance_name,
       host_name,
       status
from gv\$instance
order by 1;
spool off;
exit;
EOF
#
cat $CFIN2 >> $CFIN
sed -e ‘/^SQL/d’ -e ‘/^ [0-9]/d’ -e ‘/^ [0-1]/d’ $CFIN > $CFIN2
#
$MAIL -s “$SID clone is complete” < $CFIN2 $MAILTO

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s