Remember your dreams

Remember your dreams
Remember your dreams

Wednesday, November 27, 2019

Oracle RMAN (catalog) full backup script


Changes you need to make are highlighted in yellow. Database name, Oracle home location, user executing the script (oracle user here), and email to at the end. Optional RMAN configuration items such as filesperset, archive log files to keep, and tag name also highlighted.


#!/bin/sh
# ----------------------------------------------------
# Determine the user which is executing this script.
# ----------------------------------------------------

CUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`

# Set begin time of script
c_begin_time_sec=`date +%s`

# ----------------------------------------------------
# Put output in .out. Change as desired.
# Note: output directory requires write permission.

# RMAN_LOG_FILE=${0}.out
RMAN_LOG_FILE=${0}.`date +%Y%m%d_%H%M%S`.out

# ----------------------------------------------------
# You may want to delete the output file so that
# backup information does not accumulate.
#  If not, delete the following lines.
# ----------------------------------------------------

if [ -f "$RMAN_LOG_FILE" ]
then
        rm -f "$RMAN_LOG_FILE"
fi

# ----------------------------------------------------
# Initialize the log file.
# ----------------------------------------------------
set NLS_DATE_FORMAT="YYYY/MM/DD HH24:MI:SS"
set echo on
echo >> $RMAN_LOG_FILE
chmod 666 $RMAN_LOG_FILE

# ----------------------------------------------------
# Log the start of this script.
# ----------------------------------------------------

echo Script $0 >> $RMAN_LOG_FILE
echo ==== started on `date` ==== >> $RMAN_LOG_FILE
echo >> $RMAN_LOG_FILE

# ----------------------------------------------------
# Replace below, with the Oracle home path.
# ----------------------------------------------------

ORACLE_HOME=/oracle/app/
export ORACLE_HOME

# ----------------------------------------------------
# Replace below, with the Oracle SID of
# the target database.
# ----------------------------------------------------

ORACLE_SID=ORCL
export ORACLE_SID

# ----------------------------------------------------
# Replace below, with the Oracle DBA user id (account).
# -----------------------------------------------------

ORACLE_USER=oracle
# -----------------------------------------------------
# Set the target connect string.
# Replace "sys/manager", below,
# with the target connect string.
# -----------------------------------------------------

TARGET_CONNECT_STR=/
CATALOG_CONNECT_STR=catalog_login/password@sid_of_catalog;

# -----------------------------------------------------
# Set the Oracle Recovery Manager name.
# -----------------------------------------------------

RMAN=$ORACLE_HOME/bin/rman

# -----------------------------------------------------
# Print out the value of the variables set by this script.
# -----------------------------------------------------


echo >> $RMAN_LOG_FILE
echo   "RMAN: $RMAN" >> $RMAN_LOG_FILE
echo   "ORACLE_SID: $ORACLE_SID" >> $RMAN_LOG_FILE
echo   "ORACLE_USER: $ORACLE_USER" >> $RMAN_LOG_FILE
echo   "ORACLE_HOME: $ORACLE_HOME" >> $RMAN_LOG_FILE

# -----------------------------------------------------
# NOTE: This script assumes that the database
# is properly opened. If desired,
# this would be the place to verify that.
# -----------------------------------------------------

echo >> $RMAN_LOG_FILE

#     schedule type is                BACKUP_TYPE is
#     ----------------                --------------
# Automatic Full                     INCREMENTAL LEVEL=0
# Automatic Differential Incremental INCREMENTAL LEVEL=1
# Automatic Cumulative Incremental   INCREMENTAL LEVEL=1 CUMULATIVE
#
# Note that we use incremental level 0 to specify full backups.
# That is because, although they are identical in content, only
# the incremental level 0 backup can have incremental backups of
# level > 0 applied to it.
# ----------------------------------------------------

ORA_FULL=1;
ORA_INCR=0;
ORA_CINC=0;

if [ "$ORA_FULL" = "1" ]
then
    echo "Full backup requested" >> $RMAN_LOG_FILE
    BACKUP_TYPE="INCREMENTAL LEVEL=0"
elif [ "$ORA_INCR" = "1" ]
then
    echo "Differential incremental backup requested" >> $RMAN_LOG_FILE
    BACKUP_TYPE="INCREMENTAL LEVEL=1"

elif [ "$ORA_CINC" = "1" ]
then
    echo "Cumulative incremental backup requested" >> $RMAN_LOG_FILE
    BACKUP_TYPE="INCREMENTAL LEVEL=1 CUMULATIVE"

elif [ "$BACKUP_TYPE" = "" ]
then
    echo "Default - Full backup requested" >> $RMAN_LOG_FILE
    BACKUP_TYPE="INCREMENTAL LEVEL=0"
fi
# -------------------------------------------------
c_begin_time_sec=`date +%s`

CMD_STR="
ORACLE_HOME=$ORACLE_HOME
export ORACLE_HOME
ORACLE_SID=$ORACLE_SID
export ORACLE_SID
$RMAN target $TARGET_CONNECT_STR catalog $CATALOG_CONNECT_STR msgno append << EOF
RUN {
BACKUP
    $BACKUP_TYPE
    SKIP INACCESSIBLE
    TAG orcldb_full_db_bk
    FILESPERSET 8
    DATABASE;
   sql 'alter system archive log current';
# backup all archive logs and then delete them
BACKUP
    filesperset 8
    ARCHIVELOG ALL;
DELETE NOPROMPT ARCHIVELOG
    UNTIL TIME 'SYSDATE - 1';
}
EOF
"
# Initiate the command string
/bin/sh -c "$CMD_STR" >> $RMAN_LOG_FILE
RSTAT=$?


c_end_time_sec=`date +%s`
echo >> $RMAN_LOG_FILE
v_total_execution_time_sec=`expr ${c_end_time_sec} - ${c_begin_time_sec}`
echo "Script execution time is $((v_total_execution_time_sec / 60)) Minutes" >> $RMAN_LOG_FILE
# echo "Script execution time is $v_total_execution_time_sec" >> $RMAN_LOG_FILE
echo "Wow, that is really good time." >> $RMAN_LOG_FILE
echo >> $RMAN_LOG_FILE

# -----------------------------------------------------
# Log the completion of this script.
# -----------------------------------------------------
if [ "$RSTAT" = "0" ]
then
    LOGMSG="ended successfully"
else
    LOGMSG="ended in error"
fi

echo >> $RMAN_LOG_FILE
echo Script $0 >> $RMAN_LOG_FILE
echo ==== $LOGMSG on `date` ==== >> $RMAN_LOG_FILE
echo >> $RMAN_LOG_FILE

#
# Email log file and status to DBA
cat $RMAN_LOG_FILE | mailx -s "$((v_total_execution_time_sec / 60)) Mins. ${ORACLE_SID} on `uname -n` $BACKUP_TYPE $LOGMSG - EXIT Status = $RSTAT" yourname@company.com

exit $RSTAT

 

Friday, November 22, 2019

Alert log checker

Script to monitor the Alert Log for all instance on a server for errors and email if found. 


Even with the arrival (now years ago) of ADRCI (Automatic Diagnostic Repository Command Interpreter), I insist on monitoring the Oracle Alert Log. I will until it goes away. 

I have been running some form of this script since the days of 8i and still find it useful. Customize it to suit your specific needs. Better to know there is an issue before someone tells you. 

Add this to your crontab.
# Check Alert log for errors and email DBA
5,20,35,50 7-16 * * 0-7 /oracle/scripts/monitor/check_alert.sh

The check_alert.sh script


#!/bin/sh
# File-name: check_alert.sh
#-----------------------------------------------------
# Checks Oracle alert log files for all databases
# 1. Gets database name from oratab.
# 2. Checks ORA errors in the alert log file
# 3. Checks fail errors in the alert log file
# 4. Checks Fatal NI connect errors
# If error(s) found)then
# 5. Makes a copy of the alert file

# 6. Cleans the alert log file
# 7. Sends an e-mail with results to DBA

#-----------------------------------------------------

ORACLE_BASE=/oracle/app/oracle
export ORACLE_BASE
TMPDIR=/tmp
export TMPDIR
ORATAB=/etc/oratab
export ORATAB

#-----------------------------------------------
# Get the Oracle instances from the oratab file
# No blank lines
#-----------------------------------------------
cat ${ORATAB}/oratab | while read LINE
do
  case $LINE in
  \#*)            ;;      #comment-line in oratab
  *)
    ORACLE_SID=`echo $LINE | awk -F: '{print $1}' -`

    if [ "$ORACLE_SID" = '*' ] ; then
      ORACLE_SID=""
    fi

    export ORACLE_SID;
    ORACLE_HOME=`echo $LINE | awk -F: '{print $2}' -`; export ORACLE_HOME
    SHLIB_PATH=$ORACLE_HOME/lib:/usr/lib; export SHLIB_PATH
    LD_LIBRARY_PATH=$ORACLE_HOME/lib; export LD_LIBRARY_PATH

#------------------------------------------------------------------------
# Initialization
#------------------------------------------------------------------------
    l_err=0
    l_found=0
    l_date=`date '+%c'`
    l_filedate=`date '+%m%d%H%M'`

    l_log=${ORACLE_BASE}/diag/rdbms/${ORACLE_SID}/${ORACLE_SID}/trace/check_alert_${ORACLE_SID}.log

    l_alertfile=${ORACLE_BASE}/diag/rdbms/${ORACLE_SID}/${ORACLE_SID}/trace/alert_${ORACLE_SID}.log

    echo $l_date "*** log BEGIN ***" > $l_log
    echo "---------------------------------------------------------------------" >> $l_log
    echo "Script : "${0} >> $l_log
    echo "Database : "$ORACLE_SID >> $l_log
    echo "Server : "`uname -n` >> $l_log
    echo "Alert Log : "$l_alertfile >> $l_log
    echo "Copy To : "${l_alertfile}.${l_date} >> $l_log
    echo "---------------------------------------------------------------------" >> $l_log

#------------------------------------------------------------------------
# Verify the existance of the Oracle environment variables
#------------------------------------------------------------------------
    if test `env | grep ORACLE_SID | wc -l` -ne 1 ; then
      l_err=1
      echo "ORACLE_SID is not set \n" >> $l_log
    fi

    if test `env | grep ORACLE_HOME | wc -l` -ne 1 ; then
      l_err=1
      echo "ORACLE_HOME is not set \n" >> $l_log
    fi
#------------------------------------------------------------------------
# Check the alert log file for any errors and clean it
#------------------------------------------------------------------------
    if test -f ${l_alertfile} ; then

      if test `grep "ORA-" ${l_alertfile} | wc -l` -ne 0 ; then
        l_err=1
        l_found=1
        echo "There is an ORA- error in the Oracle alert log file!" >> $l_log
        grep "ORA-" ${l_alertfile} >> $l_log
      fi

      if test `grep -i "fail" ${l_alertfile} | wc -l` -ne 0 ; then
        l_err=1
        l_found=1
        echo "--------------------------------------------------------------" >> $l_log
        echo "There is a fail error in the Oracle alert log file!" >> $l_log
        grep -i "fail" ${l_alertfile} >> $l_log
      fi

      if test `grep -i "Fatal NI connect error" ${l_alertfile} | wc -l` -ne 0 ; then
         if test `grep -i "Fatal NI connect error" ${l_alertfile} | wc -l` -gt 25 ; then
            l_err=1
            l_found=1
            echo "--------------------------------------------------------------" >> $l_log
            echo "There is a fail error in the Oracle alert log file!" >> $l_log
            grep -i "Fatal NI connect error" ${l_alertfile} >> $l_log
            echo " " >> $l_log
            echo "Clients" >> $l_log
            grep -i "Client address: (ADDRESS=(PROTOCOL=tcp)(HOST=" ${l_alertfile} >> $l_log
            grep -i "Time: " ${l_alertfile} >> $l_log
         fi

         if test $l_err -eq 0 ; then
           echo "There are NO errors in the alert log file" >> $l_log
         fi
      fi

# Make a copy of the alert log file only if it's not empty and there are errors
#------------------------------------------------------------------------------
         if test $l_err -eq 1 ; then
           if test `cat ${l_alertfile} | wc -l` -ne 0 ; then
             cat ${l_alertfile} >> ${l_alertfile}.${l_filedate}
#              rm ${l_alertfile}
#              touch ${l_alertfile}
           fi
         fi
     echo "--------------------------------------------------------------" >> $l_log
     echo ${l_date} "*** log END ***" >> $l_log
     fi # Check the alert log
#------------------------------------------------------------------------
#------------------------------------------------------------------------
# Send errors to DBA
#------------------------------------------------------------------------
         if test $l_err -eq 1 ; then
           mail -s "${ORACLE_SID} on `uname -n` : ERRORS in alert_$ORACLE_SID.log" "your_email@company.com" > /dev/null < $l_log
         fi

   esac
done

#-----------------------------------------------------
# End of script





Wednesday, November 20, 2019

Move controlfile Oracle 12c



I've done this several times now and can confirm it's pretty straight forward. You can move and/or rename the controlfiles using this method. This is easy, but it does require some quick downtime. Here are the steps to move and rename the database controlfiles.

  • Query your current controlfile locations.
  • Alter your system to update to the new names/locations.
  • Shutdown the database.
  • Move/rename the controlfiles on the server.
  • Startup the database.
  • Verify the new names and locations.
  • Update your pfile from your spfile.
  • Cleanup old location.

Query your current controlfile locations.

SQL> select name from v$controlfile;
--------------------------------------------------------------------------------
/u01/oradata/orcl/ORCL/controlfile/o1_mf_dhngfcms_.ctl
/u02/oradata/orcl/ORCL/controlfile/o1_mf_dhngfcn5_.ctl
/u03/oradata/orcl/ORCL/controlfile/o1_mf_dhngfcng_.ctl

Alter your system to update to the new names/locations.

SQL> show parameter control_files

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
control_files                        string     

/u01/oradata/orcl/ORCL/controlfile/o1_mf_dhngfcms_.ctl, /u02/oradata/orcl/ORCL/controlfile/o1_mf_dhngfcn5_.ctl, /u03/oradata/orcl/ORCL/controlfile/o1_mf_dhngfcng_.ctl

SQL> alter system set control_files='/oracle/u01/oradata/orcl/controlfile/control01.ctl', -
> '/oracle/u02/oradata/orcl/controlfile/control02.ctl', -
> '/oracle/u03/oradata/orcl/controlfile/control03.ctl' scope=spfile;
System altered.

Shutdown the database.

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL>

Move/rename the controlfiles on the server.

Create new directory locations.

$ cd /u01/oradata/orcl
$ mkdir controlfile

$ cd /u02/oradata/orcl
$ mkdir controlfile

$ cd /u03/oradata/orcl
$ mkdir controlfile

cd to the current location of the control files and move each one to new location, renaming in the process.

$ cd /u01/oradata/orcl/ORCL/controlfile/
$ mv o1_mf_dhngfcms_.ctl ../../controlfile/control01.ctl

$ cd /u02/oradata/orcl/ORCL/controlfile/
$ mv o1_mf_dhngfcn5_.ctl ../../controlfile/control02.ctl

$ cd /u03/oradata/orcl/ORCL/controlfile/
$ mv o1_mf_dhngfcng_.ctl ../../controlfile/control03.ctl

Startup the database.
SQL> startup
ORACLE instance started.
...
Database mounted.
Database opened.

Verify the new names and locations.

SQL> select name from v$controlfile;

NAME
--------------------------------------------------------------------------------
/oracle/u01/oradata/orcl/controlfile/control01.ctl
/oracle/u02/oradata/orcl/controlfile/control02.ctl
/oracle/u03/oradata/orcl/controlfile/control03.ctl

SQL> show parameter control_files

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
control_files                        string      /oracle/u01/oradata/orcl/con
                                                 trolfile/control01.ctl, /oracl
                                                 e/u02/oradata/orcl/controlfi
                                                 le/control02.ctl, /oracle/u03/
                                                 oradata/orcl/controlfile/con
                                                 trol03.ctl

Update your pfile from your spfile.

SQL> create pfile from spfile;
File created.

Cleanup old location.

$ rmdir controlfile
$ cd .. 
$ rmdir ORCL

Backup your database!

Proactive Oracle DBA

This is a series of posts. I am working this to share some of the many scripts I schedule to automatically run to alert me of any current o...