#!/bin/ksh

#===================================================================#
# #
# Name : hotbackup.sh #
# Synopsis : Performs an online backup of the database specified in #
# the script's only argument. #
# Please remember to customise the values in the #
# CONSTANTS section before attempting to use the script. #
# Arguments: The following parameters must be provided when #
# running this script: #
# 1) The sid of the database to run against #
# Source : http://www.oracle-dba.fr.pl #
# #
#===================================================================#

#--------------------------------------------------------------------
# ARGUMENTS

export ORACLE_SID=${1}

#--------------------------------------------------------------------
# CONSTANTS

export HOME=`awk -F: '/oracle/ {print $6}' /etc/passwd`
export DATESTAMP=`date '+%m-%d-%y_%H:%M:%S'`
export SPOOL=/tmp/hotbackup.lst

export BACKUP_DEST=/oracle/${ORACLE_SID}/backup
export SYSTEM_PWD=manager
export ORATAB=/var/opt/oracle/oratab

#--------------------------------------------------------------------
# FUNCTIONS

usage()
{

echo ""
echo "USAGE:"
echo "******"
echo "hotbackup.sh sid"
echo ""
echo "where sid = database sid to run against"

}

derive_oracle_paths()
{

if [ -f ${ORATAB} ]; then
export ORACLE_HOME=`awk -F: "/^${ORACLE_SID}:/ {print \\$2; exit}"\
${ORATAB} 2>/dev/null`
export PATH=${ORACLE_HOME}/bin:${PATH}
else
echo "" >> ${LOG}
echo "ERROR:" >> ${LOG}
echo "******" >> ${LOG}
echo "${ORATAB} not found" >> ${LOG}
echo "Aborting hotbackup.sh" >> ${LOG}
exit 1
fi

}

check_parameter()
{

if [ `grep -c ${ORACLE_SID} ${ORATAB}` -eq 0 ]; then
echo "" >> ${LOG}
echo "ERROR:" >> ${LOG}
echo "******" >> ${LOG}
echo "Oracle sid ${ORACLE_SID} not found in ${ORATAB}" >> ${LOG}
echo "Aborting hotbackup.sh" >> ${LOG}
exit 1
fi

}

do_backup()
{

sqlplus system/${SYSTEM_PWD} << _E_O_F

col tablespace_name noprint
col seqn noprint
set pagesize 0
set linesize 132
set feedback off
set sqlprompt ""

Whenever SQLERROR exit 1

select chr(1) tablespace_name
, -9999 seqn
, 'alter system switch logfile;'
from dual
UNION
select chr(1) tablespace_name
, -9998 seqn
, 'alter database backup controlfile to '''||'${BACKUP_DEST}/controlfile_${ORACLE_SID}.HOT.before'' reuse;'
from dual
UNION
select tablespace_name
, 0
, 'alter tablespace '||tablespace_name||' begin backup;'
from sys.dba_tablespaces
where status = 'ONLINE'
UNION
select tablespace_name
, file_id
, '!compress < '||file_name||'> ${BACKUP_DEST}/'||substr(file_name,instr(file_name,'/',-1) + 1) || '.Z'
from sys.dba_data_files
where status = 'AVAILABLE'
UNION
select tablespace_name
, 9999
, 'alter tablespace '||tablespace_name||' end backup;'
from sys.dba_tablespaces
where status = 'ONLINE'
UNION
select chr(255) tablespace_name
, 9998 seqn
, 'alter database backup controlfile to '''||'${BACKUP_DEST}/controlfile_${ORACLE_SID}.HOT.after'' reuse;'
from dual
UNION
select chr(255) tablespace_name
, 9999 seqn
, 'alter system switch logfile;'
from dual
ORDER BY 1,2

spool ${SPOOL}
/
spool off

start ${SPOOL}

exit
_E_O_F

}

#--------------------------------------------------------------------
# MAIN

if [ $# -lt 1 ]; then
usage
exit 1
fi

derive_oracle_paths
check_parameter
do_backup