troubleshooting Question

Can you look at this shell script and tell me where my backups are being saved?

Avatar of la_colibri
la_colibri asked on
Unix OSOracle Database
2 Comments2 Solutions530 ViewsLast Modified:
Can you look at this shell script and tell me where my backups are being saved? Thanks I am not knowledgeable with shell scripts and can't understand it very well.

 
# Initialize variables. 
#
ORACLE_SID=ADCORPRD                                    # what we are backing
trace="N"                                              # for debugging
name=`whoami`
t_date=`date +'%m%d%y%H%M'`              
 
ORATAB_FILE=/etc/oratab                                # oratab file location
PATH=/bin:/etc:/usr/bin:/usr/sbin:/usr/local/bin:/home/backup/bin
 
SRC_SYSTEM=gcunx2
DST_SYSTEM=hqunx2
SCRIPT_DIR=/home/scripts/backup/hot/$ORACLE_SID
SCRIPT_DIR1=/home/scripts/notify
LOG_DIR=/home/scripts/backup/hot/$ORACLE_SID/log
LOG_FILE=/home/scripts/backup/hot/$ORACLE_SID/log/$ORACLE_SID.$t_date
 
ARCHIVE_LOG_DEST=""
 
 
########################################################################
if [ -z $SCRIPT_DIR ] ; then
   echo "SCRIPT_DIR not set, Exiting"
   exit 5
else
  if ! [ -d $SCRIPT_DIR ] ; then
    echo "SCRIPT_DIR nonexistant or Invalid. Exiting"
    exit 4
  fi
fi
 
cd $SCRIPT_DIR
 
########################################################################
# Quick sanity check to make sure we are running as oracle.
 
if [ $name != 'oracle' ] ; then
  echo "You must be oracle to run this program"
  exit 1
fi
 
########################################################################
# Make sure that the instance name exists in the oratab file to
# insure that the variables get set correctly. If not bail out now.
 
grep $ORACLE_SID $ORATAB_FILE > /dev/null
ret_val=$?
if [ $ret_val -ne 0 ] ; then                             # No Oratab entry
  echo "Invalid SID $ORACLE_SID in ORATAB,  Exiting"
  exit 4
fi
 
## Check to see that the ORACLE_SID variable contains something meaningful
if [ -z $ORACLE_SID ] ; then
  echo "Oracle SID not set or Invalid. Exiting"
  exit 5
fi
 
 
########################################################################
#
# Have oracle script set the appropriate shell variables
#
export ORAENV_ASK="NO"                                 # set oracle paths etc
. /usr/local/bin/oraenv
 
#####################################################################
# Remove stuff left over since last run.
#
rm -f errfile                                # last error file
rm -f ex_hotbackup.sql                       # last command file
 
 
#####################################################################
# Query Oracle to determine directories where things are located.
# Determine where the User Dump Files are located
# First determine where trace file is located.
 
USER_DUMP_DEST=`sqlplus -s / << EOF
set feedback off;
set echo off;
set heading off;
set pagesize 0;
select value from v\\$parameter where name = 'user_dump_dest';
quit;
EOF
`
if [ -z $USER_DUMP_DEST ] ; then
   echo "USER_DUMP_DEST not set, Exiting"
   exit 5
else
  if ! [ -d $USER_DUMP_DEST ] ; then
    echo "USER_DUMP_DEST nonexistant or Invalid. Exiting"
    exit 5
  fi
fi
 
 
#####################################################################
# Backup the current control file to trace in case we need it.
if [ $trace = 'Y' ] ; then
  echo "Creating and Backing up Trace Control file"
  read a
fi
 
sqlplus -s / << EOF
set feedback off;
set echo off;
alter database backup controlfile to trace;
quit;
EOF
> /dev/null
 
# Now move the tracefile we just created to the backup directory.
# Look in the user dump dest (where Oracle writes controlfile), and 
# find the 10 newest files and examine each to see if it is a controlfile.
# Copy the first backup controlfile to destination directory.
 
ls -1t $USER_DUMP_DEST/*.trc | head | while read t_file
do
  grep -l "CREATE CONTROLFILE DATABASE" $t_file > /dev/null  # is a ctl file?
  retval=$?
  if [ $retval -eq 1 ]; then                                 # yep, copy it
    fname=backup_controlfile.$ORACLE_SID
    mv $t_file  $SCRIPT_DIR/$fname
    rcp $SCRIPT_DIR/$fname $DST_SYSTEM:$SCRIPT_DIR/$fname
    if ! [ -s $SCRIPT_DIR/$fname ]; then
      echo "Warning: Controlfile could not be copied "
    fi
    break;
  fi
done
 
 
############################################################################
# Generate script to take each tablespace offline, and 
# back up all the datafiles that comprise the tablespace.
#
if [ $trace = 'Y' ] ; then
  echo "Generating Scriptfile for hotbackup"
  read a
fi
 
sqlplus -s / @gen_hotbackup.sql | awk -f gen_hotbackup.awk -v dest_sys=$DST_SYSTEM > ex_hotbackup.sql
 
#####################################################################
# test to see if the SQL we just created actually exists
 
if ! [ -s $SCRIPT_DIR/ex_hotbackup.sql ] ; then
  echo "Backup Script missing or 0 length. Exiting"
  exit 7
fi
 
#####################################################################
# test to see if the SQL we just created contains any errors 
 
if [ -s $SCRIPT_DIR/ex_hotbackup.sql ] ; then
  grep 'ORA-' $SCRIPT_DIR/ex_hotbackup.sql > /dev/null
  ret_val=$?
  if [ $ret_val -eq 0 ] ; then
    echo "Backup Script Contains Errors. See $SCRIPT_DIR/ex_hotbackup.sql. Exiting"
    exit 8
  fi
else
  echo "Backup Datafile missing or 0 length. Exiting"
  exit 8
fi
 
 
 
#####################################################################
# here is where the main portion of the backup gets done. Go ahead and
# run the script we just created.
 
echo "BACKUP Started at `date`" > $LOG_FILE
 
if [ $trace = 'Y' ] ; then
  echo "Executing Backup SQL"
fi
 
sqlplus -s / @$SCRIPT_DIR/ex_hotbackup.sql  >> $LOG_FILE
 
echo "BACKUP Ended at `date`" >> $LOG_FILE
 
grep 'ORA-' $LOG_FILE > /dev/null
ret_val=$?
if [ $ret_val -eq 0 ] ; then
    echo "WARNING Backup Script Contains Errors." 
    echo "See $LOG_FILE Exiting"
fi
 
#####################################################################
# Force a log switch, and then copy all but the last file to the destination
if [ $trace = 'Y' ] ; then
  echo "Forcing a log switch"
  #read a
fi
 
SWITCH=`sqlplus -s / << EOF
set feedback off;
set echo off;
set heading off;
set pagesize 0;
alter system switch logfile;
quit;
EOF
`
 
#####################################################################
# test to see if the errfile created by the script exists
if ! [ -s $SCRIPT_DIR/errfile ] ; then
  echo "Warning errfile missing or 0 length"
  exit 9
fi
 
###################################
# Notify of success if notify.good exists
#
 
ret_val=`cat errfile | awk '{ x+=$1} END{print x}'`
if [ $ret_val -eq 0 ] ; then
   SUBJ="$ORACLE_SID Hotbackup Successful"
   if [ -s $SCRIPT_DIR1/notify.good ] ; then               
    cat $LOG_FILE | sed -e '/^$/d'|mail -s "$SUBJ" `cat $SCRIPT_DIR1/notify.good`
  fi
else
   SUBJ="$ORACLE_SID Hotbackup FAILURE"
   if [ -s $SCRIPT_DIR1/notify.list ] ; then               
    echo $SUBJ | mail -s "$SUBJ" `cat $SCRIPT_DIR1/notify.list`
   fi
    cat $LOG_FILE | sed -e '/^$/d'|mail -s "$SUBJ" `cat $SCRIPT_DIR1/notify.list`
fi
 
#
#####################################################################
# Remove any log files created by this script that are older than 10 days
find $SCRIPT_DIR/log  \( -name "$ORACLE_SID.*" \) -mtime +10 -exec rm {} \;
 
exit 0
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros