NyQuest logo
HomeBanner 7 FAQTips & Tricksnavtab
menubar
Oracle "Hot Backup" Script (UNIX)

#!/bin/ksh
#
# Unix script to perform Hot/Online backups of Oracle Datafiles
#
function Usage
{
   echo ""
   echo " Usage : hot_backup.ksh <ORACLE_SID> <TABLESPACE NAME>"
   echo ""
   echo "where ORACLE_SID (Mandatory) - SID of database to start"
   echo "       TABLESPACE NAME : Tablespace to backup "
   echo ""
   echo " e.g.  $ hot_backup.ksh PROD USERS"
   echo ""
}

#########################################################
# START OF MAIN SCRIPT
#########################################################

DATABASE=$1
TABLESPACE=$2; export TABLESPACE
ORA_BACKUP=$3; export ORA_BACKUP
USERPASS=$4

# Check that we have a database
if [ -z "${DATABASE}" ]
then
   echo
   echo "No Database Specified !"
   echo
   Usage
   exit 1
fi

# Check that we have a tablespace name to back up
if [ -z "${TABLESPACE}" ]
then
   echo
   echo "No Tablespace Name Specified !"
   echo
   Usage
   exit 1
fi

# Check the backup directory exists
if [ ! -d "${ORA_BACKUP}" ]
then
  echo
  echo "$ORA_BACKUP does not exist !"
  echo
  exit 1
fi

# Check we have an Oracle username and password
if [ -z "${USERPASS}" ]
then
   echo
   echo "No Database connect string specified !"
   Usage
   exit 1
fi

# Get the ORACLE_HOME from the oratab file
ORACLE_HOME=`cat /etc/oratab | grep $DATABASE | awk -F: '{print $2}'`
if [ ! -d "${ORACLE_HOME}" ]
then
  echo
  echo "$ORACLE_HOME does not exist !"
  echo
  exit 1
fi

ORACLE_SID=$DATABASE
export ORACLE_SID
PATH=.:$ORACLE_HOME/bin:$PATH; export PATH

# See if we can find svrmgrl, if not use sqldba instead
if [ -x "${ORACLE_HOME}/bin/svrmgrl" ]
then
    SVR="svrmgrl"
else
    SVR="sqldba lmode=y"
fi

# Generate the backup commands
#
 sqlplus -s $USERPASS << EOF
set feed off pages 0 head off echo off line 250 ver off
col col1 newline
spool /tmp/hot1.sql
select 'select * from v$backup;' from dual;
select 'alter tablespace '||'${TABLESPACE}'||' begin backup;' from dual;
select 'host cp -p '||file_name||
       ' ${ORA_BACKUP}/'||
       substr(file_name,instr(file_name,'dbs/')+7,length(file_name)) col1
from dba_data_files where tablespace_name=upper('${TABLESPACE}');
select 'select * from v$backup;' from dual;
select 'alter tablespace '||'{TABLESPACE}'||' end backup;' from dual;
select 'select * from v$backup;' from dual;
exit
EOF

#
# Now start the Backup
#
$SVR << EOF
connect internal
@/tmp/hot1.sql
exit
EOF

exit 0
 



The contents of www.nyquest.com are Copyright © 2007 by Nyquest Consulting.
Sungard® and Banner® are registered trademarks of Sungard® Corporation.
Nyquest is not affiliated in any way with Sungard®.
All Rights Reserved.