Skip to content

Instantly share code, notes, and snippets.

@sravanrox
Created September 13, 2016 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sravanrox/933bb8586706359004349d791dd8c953 to your computer and use it in GitHub Desktop.
Save sravanrox/933bb8586706359004349d791dd8c953 to your computer and use it in GitHub Desktop.
Archive log backup for Non ASM db's
#!/bin/sh
# $Header: dbname_archive_backup.sh
# ---------------------------------------------------------------------------
# Determine the user which is executing this script.
# ---------------------------------------------------------------------------
CUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`
BACKUP_HOME=/loc/host/dbname/rman
DATE=`/bin/date +%m%d%Y%H%M%S`
DOW=`date +%A | tr -s '[:upper:]' '[:lower:]'`
HOME=/export/home/oracle/scripts
LOG=${HOME}/logs
page_dba=xxx@gmail.com
sqlfile="${HOME}/check_rman.sql"
spoolfile="${HOME}/rman_list.txt"
ORACLE_SID=dbname
ORACLE_HOME=/opt/app/oracle/product/11.2.0/11.2.0.4
LD_LIBRARY_PATH_64=$ORACLE_HOME/lib
LD_LIBRARY_PATH=$ORACLE_HOME/lib
export ORACLE_SID
export ORACLE_HOME
export LD_LIBRARY_PATH_64
export LD_LIBRARY_PATH
echo "ORACLE_SID=$ORACLE_SID"
echo "ORACLE_HOME=$ORACLE_HOME"
echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
SQLPLUS=$ORACLE_HOME/bin/sqlplus
ORAENV_ASK=NO
# ----------------------------------------------------------------------------
# Check archive dest space if it more then max allow, start the archive
# backup
# ----------------------------------------------------------------------------
echo "started `date`"
#-------------------------------------------------------------
## Check space usage on /oracle/archive
#-------------------------------------------------------------
default_percent=51
max_percent=80
if [ -n "$1" ]
then
case "$1" in
*[0-9]*)
if [ $input_val -gt $max_percent ]
then
echo "input value large then ${max_percent}%"
echo "set input value to default ${default_percent}%"
input_val=$max_percent
fi
;;
*)
echo "The input parameter need to be an numeric number."
echo "Usage:- $0 <number>"
echo "where <nunber> is from 1 to 85"
exit
;;
esac
else
echo "No input value. will defualt to $default_percent"
input_val=$default_percent
fi
echo "input is $input_val"
max=$input_val
disk="/oracle/archive"
percent=`df -h |grep ${disk} |awk '{ print $5}' |cut -d"%" -f1`
echo "Check $disk space usage"
echo "Percent use for $disk is ${percent}%"
if [ $percent -ge $max ]
then
echo "Over the target of $max"
echo "start the Archive log backup"
else
echo "Did not hit the max ratio of ${max}%. bye"
exit
fi
#-----------------------------------------------------------------
## Check DB online status
#----------------------------------------------------------------
#
pchk=`ps -ef |grep pmon |grep $ORACLE_SID |grep -v grep |wc -l`
if [ $pchk -eq 0 ]
then
echo "DB is offline bye."
exit
fi
#-----------------------------------------------------------------
# Check for other active rman backup process
#-----------------------------------------------------------------
pcnt=`ps -ef |grep rman |grep -v grep |wc -l`
if [ $pcnt -gt 0 ]
then
echo "Other Rman process is running. bye"
exit
fi
echo "set echo off" > $sqlfile
echo "set pages 0" >> $sqlfile
echo "set feedback off" >> $sqlfile
echo "set lines 80" >> $sqlfile
echo "spool ${spoolfile}" >> $sqlfile
echo "select A.USERNAME,A.PROGRAM" >> $sqlfile
echo "from gv\$session A , gv\$sql B" >> $sqlfile
echo "where A.PREV_SQL_ID = B.sql_id and A.inst_id = B.inst_id" >> $sqlfile
echo "and A.program like 'rman%';" >> $sqlfile
echo "spool off" >> $sqlfile
echo "exit" >> $sqlfile
$SQLPLUS "/ as sysdba" @$sqlfile
chk=`cat $spoolfile | wc -l`
if [ $chk -gt 0 ]
then
echo "Other Rman process is running in other node. bye"
rm $spoolfile
rm $sqlfile
exit
fi
rm $spoolfile
rm $sqlfile
#---------------------------------------------------------------------------
# Put output in <this file name>.out. Change as desired.
# Note: output directory requires write permission.
# ---------------------------------------------------------------------------
RMAN_LOG_FILE=${LOG}/dbname_archive_backup.sh.${DATE}.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.
# -----------------------------------------------------------------
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
# ----------------------------------------------------------------------------
# Check all archive dest space if it more the the max allow, start the archive
# backup
# ----------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Replace /db/oracle/product/ora81, below, with the Oracle home path.
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Replace ora81, 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=/
# ---------------------------------------------------------------------------
# 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
# ---------------------------------------------------------------------------
# Print out the value of the variables set by bphdb.
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# NOTE: This script assumes that the database is properly opened. If desired,
# this would be the place to verify that.
# ---------------------------------------------------------------------------
echo >> $RMAN_LOG_FILE
CMD_STR="
$RMAN target $TARGET_CONNECT_STR msglog $RMAN_LOG_FILE append << EOF
RUN {
configure backup optimization on;
# backup all archive logs
ALLOCATE CHANNEL ch00 TYPE disk format '${BACKUP_HOME}/arch_bkp/dbname_${DOW}_arc%s_%p_%T';
ALLOCATE CHANNEL ch01 TYPE disk format '${BACKUP_HOME}/arch_bkp/dbname_${DOW}_arc%s_%p_%T';
ALLOCATE CHANNEL ch02 TYPE disk format '${BACKUP_HOME}/arch_bkp/dbname_${DOW}_arc%s_%p_%T';
ALLOCATE CHANNEL ch03 TYPE disk format '${BACKUP_HOME}/arch_bkp/dbname_${DOW}_arc%s_%p_%T';
crosscheck archivelog all;
backup as compressed backupset archivelog all delete input;
RELEASE CHANNEL ch00;
RELEASE CHANNEL ch01;
RELEASE CHANNEL ch02;
RELEASE CHANNEL ch03;
}
EOF
"
# Initiate the command string
#RSTAT="0"
echo "$CMD_STR" >> $RMAN_LOG_FILE
if [ "$CUSER" = "root" ]
then
echo "dbname Arch log backup job start at `/bin/date`" | mailx -s "New dbname ARC Backup Notification" $page_dba
su - $ORACLE_USER -c "$CMD_STR" >> $RMAN_LOG_FILE
RSTAT=$?
else
echo "dbname Arch log backup job start at `/bin/date`" | mailx -s "New dbname ARC Backup Notification" $page_dba
/usr/bin/sh -c "$CMD_STR" >> $RMAN_LOG_FILE
RSTAT=$?
fi
# ---------------------------------------------------------------------------
# Log the completion of this script.
# ---------------------------------------------------------------------------
if [ "$RSTAT" = "0" ]
then
LOGMSG="ended successfully"
echo "dbname ARCH BCK Successfully end`/bin/date`" | mailx -s "New dbname Arch log End Backup Notification" $page_dba
else
echo " ARCH BCK Failed please contact DBA immeditately `/bin/date`" | mailx -s "New dbname Arch Backup ERROR" $page_dba
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
echo "Sync with recovery catalog"
$RMAN target / catalog user/password@catalog msglog $RMAN_LOG_FILE append << EOF
resync catalog;
EOF
exit $RSTAT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment