Hot backup script

This UNIX shell script will facilitate a hot backup of one Oracle database using parametric cursors.

This UNIX shell script will facilitate a hot backup of one Oracle database. It has been tested on 7x, 8x and 9x, and was developed using parametric cursors to be more compact. You must edit this file to replace the relevant variables and paths to suit your environment.
# Set env variables

ORACLE_SID=DBADB2; export ORACLE_SID
ORACLE_HOME=/prod/applc/ora9201; export ORACLE_HOME
LD_LIBRARY_PATH=$ORACLE_HOME/lib; export LD_LIBRARY_PATH
PATH=$PATH:$ORACLE_HOME/bin; export PATH
TIMESTAMP=`/bin/date +%d%m%Y_%H%M`

# Check whether the instance is in Arcive log mode or not.
# Create the Backup Directories and log files only if the instance is in 
# archive log mode.

arc=`ps -ef | grep arc | grep ${ORACLE_SID} | grep -v grep`
                if [ -z "$arc" ]; then
                       
echo ""
echo "***********************************************************"
echo "The instance  ${ORACLE_SID}  is NOT in archive log mode!!!"
echo "***********************************************************"
echo ""
                       exit
  else
   DEST=/db/dba/backup
   mkdir $DEST/HotBackup_${ORACLE_SID}_${TIMESTAMP}
   BKUPDEST=$DEST/HotBackup_${ORACLE_SID}_${TIMESTAMP}
   LOGDIR=/db/dba/logs
   BKUPLOG=$LOGDIR/HotBackup_"${ORACLE_SID}"_"${TIMESTAMP}".log

   echo ""
   echo "**********************************************************************"
   echo "Hot Backup Of Database "${ORACLE_SID}" Started..... Please Wait !!!"
   echo "**********************************************************************"
   echo ""

  fi

 # Figure out version of database.
            
                if [ -f $ORACLE_HOME/bin/svrmgrl ] ; then
                    SQLDBA="sqlplus internal"
                else
                    SQLDBA="sqlplus /nolog"
      SQLUSR="connect / as sysdba"
                fi
        
# Now generate the backup commands

${ORACLE_HOME}/bin/$SQLDBA <
 
   /dev/null 
$SQLUSR
set serverout on
set echo off
set pause off
set feed off
set head off
set lines 250
spool hotbackup
declare
   cursor ts is select distinct(tablespace_name) from dba_data_files;
  cursor df(p_ts varchar2) is select file_name from dba_data_files
  where tablespace_name=p_ts;
  v_tsname dba_data_files.tablespace_name%type;
  v_dfname dba_data_files.file_name%type;
begin
   open ts;
  loop
  fetch ts into v_tsname;
  exit when ts%notfound;
   dbms_output.put_line('alter tablespace '||v_tsname||' begin backup;');
   open df(v_tsname);
                 loop
                  fetch df into v_dfname;
                  exit when df%notfound;
    dbms_output.put_line('!cp '||v_dfname||' '||'$BKUPDEST');
    end loop;
    close df;
   dbms_output.put_line('alter tablespace '||v_tsname||' end backup;');
 end loop;
        close ts;
end;
/
select 'alter system archive log current;' from dual;
select 'alter system archive log current;' from dual;
select 'alter system archive log current;' from dual;
select 'alter database backup controlfile to '||chr(10)||'''$BKUPDEST/control.bkp'''||';' from dual;
spool off
exit;
EOF

# Clean up the hotbackup.lst and redirect the output to hotbackup1.lst.

tail +25 hotbackup.lst |grep -v SQL> > hotbackup1.lst

# Now do the backup and then check the backup mode of the tablespaces.

${ORACLE_HOME}/bin/$SQLDBA <
  
    ${BKUPLOG}
$SQLUSR

set echo on
set termout on

@hotbackup1.lst
set pages 1000 lines 132;

column recover heading "Requires|Recovery?" format a10;
column time heading "Date Of|Last Backup" format a12;
column ts heading "TsName" format a15;
column df heading "FileName" format a30;
column mode heading "Mode" format a8;

select tablespace_name ts ,name df,
       a.status "Status",
       decode(fuzzy,'YES','BACKUP','NORMAL') "mode" ,recover, time
from v$datafile_header a, v$backup b
where a.file#=b.file#
order by tablespace_name,name;
exit
EOF
 if test $? -eq 0 ; then
  echo ""
  echo "****************************************************************"
  echo "Hot Backup Of Database "${ORACLE_SID}" Completed Successfully."
  echo "****************************************************************"
 else
  echo ""
  echo "*************************************************************"
         echo "Hot Backup Of Database "${ORACLE_SID}" Failed Due To Errors."
  echo "*************************************************************"
  echo ""
 fi

# Clean up the .lst files

rm hotbackup.lst
rm hotbackup1.lst

  
 

This was first published in February 2004

Dig deeper on Oracle database backup and recovery

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close