Wednesday, June 18, 2014

Automate Nightly database backup

Implement Nightly Oracle Hot  Backup – Non RMAN/OEM Approach
1.    Ensure the following files are located in your database server’s scripts directory (eg. /oraprod/oracle/scripts).
a.    hotbkup_orcl.sh
b.    hotbkup.sh
2.    Replace the existing script directory specified in hotbkup.sh to reflect the correct scripts directory.
Script file
# Invoke hot backup script. Make sure to specify the correct scripts directory.
cd /var/opt/oracle/scripts
./hotbkup_orcl.sh orcl
3.    Make appropriate changes to the values of the following parameters in hotbkup_orcl.sh file.
a.  export SPOOL=/tmp/hotbackup.lst
b.  export BACKUP_DEST=/oraprod/orabkup
c.  export OLD_BACKUP_DEST=/orabkup/oraprod
d.  export SYSTEM_PWD=yadayadayada

4.    Encrypt hotbkup_orcl.sh file to preserve sensitive information.
a.  $ gpg –c hotbkup_orcl.sh
  Enter passphrase: <enter your password here >

a.  To decrypt file use gpg command:
$ gpg hotbkup_orcl.sh.gpg
gpg: CAST5 encrypted data
gpg: encrypted with 1 passphrase
File `hotbkup_orcl.sh' exists. Overwrite? (y/N) y
gpg: WARNING: message was not integrity protected


5.    As oracle software owner OS id, run
a.    crontab –e
b.    Make an entry as follows to execute the backup script to run at 1.30 a.m.
                                          i.    59 23 * * 1,2,3,4,5 /var/opt/oracle_dm/scripts/hotbkup.sh – This entry executes the script orabkup.sh which in turn backs up the oracle database.

Automate archive log deletion
 Automate archive log deletion.
1.    Ensure the following files are located in your database server’s scripts directory (eg. /oraprod/oracle/scripts).
a.    delarch.sh
2.    Replace the existing archive destination specified in delarch.sh with the correct archive log destination.
Script File
# Script to delete archive log files older than 2 days.
find /u03/oracle/arch  -name '*.log' -mtime +2 -exec rm -f {} ';'
3.    As oracle software owner OS id, run
a.    Crontab -e
b.    Make an entry as follows to execute archive log deletion script to run at 12.30 a.m.
                                          i.    01 23 * * 1,2,3,4,5 /var/opt/oracle_dm/scripts/delarch.sh  -- This entry executes the script /home/oracle/scripts/delarch.sh which in turn deletes the archive logs older than 2 days.



Referenced scripts.

hotbkup.sh
# Move backup log file
mv /oraprod/orabkup/logs/orabkup.log /oraprod/orabkup/logs/orabkup.`date '+%y%m%d%H%M%S'`
# Start of hot backup
cd /var/opt/oracle_dm/scripts
./hotbkup_orcl.sh orcl


hotbkup_orcl.sh

#===================================================================#
#                                                                   #
# Name     : hotbkup_orcl.sh                                           #
# Synopsis : Performs an online backup of the database specified in #
#            the script's only argument.                            #
#            Please remember to customize 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            #
#===================================================================#

#--------------------------------------------------------------------
# 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=/oraprod/orabkup
export OLD_BACKUP_DEST=/oraprod/orabkup
export SYSTEM_PWD=bkuptemppw
export ORATAB=/etc/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' and tablespace_name not in ('TEMP')
      UNION
      select tablespace_name
      ,      file_id
      ,      '!gzip < '||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' and tablespace_name not in ('TEMP')
      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                                                             
#cp $ORACLE_HOME/dbs/init$ORACLE_SID.ora  $BACKUP_DEST                 


delarch.sh
# To delete archive log files older than 2 days.

find /oraprod/oracle/arch  -name '*.dbf' -mtime +2 -exec rm -f {} ';'

Reinstall Enterprise Manager Database Controll Oracle 11g

This is what I tried to remove and reinstall EM.

In last case, you can try to reinstall the EM with emca, maybe something like:

emca -deconfig dbcontrol db -repos drop
emca -config dbcontrol db -repos create

Tuesday, June 17, 2014

Temp space was 32GB - how to make it smaller.

Add new TEMPORARY TABLESPACE TEMP2
Set new TEMP2 as DEFAULT
Drop the original TEMP space.


  1. CREATE TEMPORARY TABLESPACE "TEMP2" TEMPFILE '/home/oracle/app/oracle/oradata/orcl/temp02.dbf' SIZE 1G REUSE AUTOEXTEND ON NEXT 16M MAXSIZE 8G;
  2. ALTER DATABASE DEFAULT TEMPORARY TABLESPACE temp2; 
  3. DROP TABLESPACE temp INCLUDING CONTENTS AND DATAFILES;
And that will make it smaller.