Last active
July 20, 2018 07:40
-
-
Save sravanrox/d2ac7544c5757b8a0cf054ca844ce040 to your computer and use it in GitHub Desktop.
Non ASM DB Rman backup script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/sh | |
# $Header: dbname_daily_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 | |
ORACLE_SID=dbname | |
export ORACLE_SID | |
ORAENV_ASK=NO | |
#--------------------------------------------------------------------------- | |
# Put output in <this file name>.out. Change as desired. | |
# Note: output directory requires write permission. | |
# --------------------------------------------------------------------------- | |
RMAN_LOG_FILE=${LOG}/dbname_daily_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 | |
ORACLE_HOME=/opt/app/oracle/product/11.2.0/11.2.0.4 | |
export ORACLE_HOME | |
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 | |
if [ "$DOW" = "sunday" ] | |
then | |
echo "Full backup requested" >> $RMAN_LOG_FILE | |
RMAN_COMMAND="backup as compressed backupset INCREMENTAL LEVEL 0 TAG 'db_%TAG' DATABASE" | |
else | |
echo "Default - incremental backup requested" >> $RMAN_LOG_FILE | |
RMAN_COMMAND="backup as compressed backupset INCREMENTAL LEVEL 1 TAG 'db_%TAG' DATABASE" | |
fi | |
CMD_STR=" | |
$RMAN target $TARGET_CONNECT_STR msglog $RMAN_LOG_FILE append << EOF | |
RUN { | |
crosscheck backup; | |
crosscheck archivelog all; | |
delete noprompt expired backup; | |
delete noprompt expired archivelog all; | |
allocate channel ch01 type disk format '${BACKUP_HOME}/db_bkp/dbname_${DOW}_db_%s_%p_%T'; | |
allocate channel ch02 type disk format '${BACKUP_HOME}/db_bkp/dbname_${DOW}_db_%s_%p_%T'; | |
allocate channel ch03 type disk format '${BACKUP_HOME}/db_bkp/dbname_${DOW}_db_%s_%p_%T'; | |
allocate channel ch04 type disk format '${BACKUP_HOME}/db_bkp/dbname_${DOW}_db_%s_%p_%T'; | |
${RMAN_COMMAND}; | |
sql 'alter system archive log current'; | |
RELEASE CHANNEL ch01; | |
RELEASE CHANNEL ch02; | |
RELEASE CHANNEL ch03; | |
RELEASE CHANNEL ch04; | |
# 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'; | |
BACKUP AS COMPRESSED backupset TAG 'arc_%TAG' ARCHIVELOG ALL DELETE INPUT; | |
RELEASE CHANNEL ch00; | |
RELEASE CHANNEL ch01; | |
RELEASE CHANNEL ch02; | |
ALLOCATE CHANNEL ch00 TYPE disk format '${BACKUP_HOME}/ctrl_bkp/dbname_${DOW}_ctl_%s_%p_%T'; | |
BACKUP TAG 'ctl_%TAG' CURRENT CONTROLFILE; | |
RELEASE CHANNEL ch00; | |
} | |
EOF | |
" | |
# Initiate the command string | |
echo "$CMD_STR" >> $RMAN_LOG_FILE | |
if [ "$CUSER" = "root" ] | |
then | |
echo " dbname database backup job start at `/bin/date`" | mailx -s " dbname Database Backup Notification" $page_dba | |
su - $ORACLE_USER -c "$CMD_STR" >> $RMAN_LOG_FILE | |
RSTAT=$? | |
else | |
echo " dbname database backup job start at `/bin/date`" | mailx -s " dbname Database 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 Successfully end`/bin/date`" | mailx -s " dbname DB End Backup Notification: SUCCESS" $page_dba | |
else | |
echo " dbname DB Backup Failed please contact DBA immediately `/bin/date`" | mailx -s " dbname DB End Backup Notification: FAILED" $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 "Start delete obsolete backup " >> $RMAN_LOG_FILE | |
$RMAN target $TARGET_CONNECT_STR msglog $RMAN_LOG_FILE append << EOF | |
allocate channel for maintenance type disk; | |
delete noprompt obsolete device type disk; | |
release channel; | |
EOF | |
echo "Complete delete obsolete backup " >> $RMAN_LOG_FILE | |
echo "Sync with recovery catalog" | |
$RMAN target $TARGET_CONNECT_STR 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