DBAQuest.com



Oracle Cold Backup Script for UNIX
Date Saturday, February 02 @ 12:31:35
Topic Backup and Recovery


A simple cold backup script for Oracle on UNIX environments. Since this script uses SQLPlus to shut the database down, you have to be running Oracle version 8i or greater. For older versions, use Server Manager.
This script takes the instance name as a parameter. Usage example is shown below:

$ cold_backup.ksh prod

Make sure you change the environment variables to reflect your settings. You can download a zipped version of this script from our downloads area.

#!/usr/bin/ksh
# -------------------------------------------------------------------------------
# Name: $HOME/scripts/cold_backup.ksh
# Author: Murali Sreedhar
# Usage: cold_backup.ksh SID
# Description: Performs cold backup of specified database. Change the environment
# variables according to your specific environment.
# Disclaimer: Use this script at your own risk. The author is not responsible
# for any loss arising out of the use of this script.
# -------------------------------------------------------------------------------

# Check for number of arguments
if (($# == 0 || $# > 1))
then
print "\nUsage: cold_backup.ksh sidname\n"
exit 1
fi

# Set the environment variables, with ORACLE_SID being the first argument
# entered on the command line.
export ORACLE_SID=$1
export ORACLE_HOME=/u01/app/oracle/product/9.0.1
export ORACLE_BASE=/u01/app/oracle
export USER_PWD=/

export BACKUP_DIR=/u03/backup
export SCRIPTS=/export/home/oracle/scripts
export LOGFILE=$SCRIPTS/log/coldbkp.log
export COPY_CMDFILE=$SCRIPTS/cmd/backup_child_${ORACLE_SID}.ksh

# Check if instance is up. If not, then abort backup

BGPROC_COUNT=`ps -ef | grep smon_${ORACLE_SID} | egrep -c -v grep`

if [[ $BGPROC_COUNT -ne 0 ]]
then
print "Instance is up.\n" >$LOGFILE
else
print "Instance not up. How am I supposed to get the file names? Aborting backup...\n" >$LOGFILE
exit 1
fi

# Log into the DB, get file info and shutdown the DB

print "Gathering file information...\n" >> $LOGFILE
$ORACLE_HOME/bin/sqlplus -s "$USER_PWD as sysdba"</dev/null
set head off
set pages 0
set feedback off
set verify off
spool $COPY_CMDFILE
select 'cp '||file_name||' ${BACKUP_DIR}/' from dba_data_files;
select 'cp '||name||' ${BACKUP_DIR}/' from v\$controlfile;
spool off
shutdown immediate
exit
EOF
print "Database shut down at `date`. Starting filecopy.\n" >>$LOGFILE

# Check if filelist exists. If it doesnt, we stop here.

if [[ ! -f $COPY_CMDFILE ]]
then
print "$COPY_CMDFILE: Non-existent file, or not accessible\n" >>$LOGFILE
exit 2

# For each file in our log, strip the filename from the absolute path
# and copy all files to the backup directory.
else
. $COPY_CMDFILE
print "Copying init${ORACLE_SID}.ora to backup directory...\n" >>$LOGFILE
cp ${ORACLE_BASE}/admin/${ORACLE_SID}/pfile/init.ora ${BACKUP_DIR}/init${ORACLE_SID}.ora
fi

# Delete the child script to avoid confusion

rm $COPY_CMDFILE

# Backup is complete, now start the database

print "Done copying files at `date`, now starting database.\n" >>$LOGFILE
$ORACLE_HOME/bin/sqlplus -s "$USER_PWD as sysdba"/dev/null
startup
exit
EOF

# Check the smon process to see if instance really did come up

BGPROC_COUNT=`ps -ef | grep smon_${ORACLE_SID} | egrep -c -v grep`

if [[ $BGPROC_COUNT -ne 0 ]]
then
print "Instance has been started successfully.\n" >>$LOGFILE
else
print "Could not start the instance $ORACLE_SID. Please check alert log for information.\n" >>$LOGFILE
exit 1
fi


This article comes from DBAQuest.com
http://www.dbaquest.com

The URL for this story is:
http://www.dbaquest.com/modules.php?op=modload&name=News&file=article&sid=14