Logo

UNIX Shell Script

Hot Database Backup

Tested on Oracle 8.0 Tested on Oracle 7

Updated : 29-July-1998
Version : 1.0

Description

Generates SQL script put the tablespace in backup mode and to do a unix copy to the backup directory. The datafiles can be copied to tape drive from the backup directory disk copy.

Parameters

$1 (MANDATORY) - ORACLE_SID of the database to hot backup
$2 (MANDATORY) - The tablespace name to back up
$3 (MANDATORY) - The directory to backup the database
$4 (MANDATORY) - The database connect string to export from.

Shell Source

#!/bin/ksh
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

Return to Index of SQL Scripts


Home | Company Profile | Services | Contact Us | SQL scripts and tips | Quiz
Legal

Logo