We help IT Professionals succeed at work.

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

la_colibri
la_colibri asked
on
526 Views
Last Modified: 2013-11-17
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

Open in new window

Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.