Files
nokia-hpux-oracle/rman_db.sh
2025-10-24 16:56:57 +01:00

121 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
#
# RMAN DB backup with incremental Merge
# Written for Avon
# v1.0 - James Pattinson - October 2023
#
# usage: rman_db.sh <ORACLE_SID>
MYDIR="$(dirname "$(readlink -f "$0")")"
export ORACLE_SID=$1
. $HOME/.profile
export ORAENV_ASK=NO
export ORACLE_SID=$1
. oraenv
export ORAENV_ASK=YES
source $MYDIR/rbk_api.conf
source $MYDIR/oracle_funcs.sh
#ORACLE_SID=$1
usage() { echo "Usage: $0 <DBNAME>]" 1>&2; exit 1; }
if [ -z "${ORACLE_SID}" ]; then
usage
fi
#ORAENV_ASK=NO
#. oraenv
export NLS_DATE_FORMAT='mm-dd-yyyy hh24:mi:ss'
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
MOUNTPOINT=$MOUNTPOINT_PREFIX$ORACLE_SID
mkdir -p $RMAN_LOG_DIR/$ORACLE_SID/
RMAN_LOG=$RMAN_LOG_DIR/$ORACLE_SID/rman_${ORACLE_SID}_DB_$(date +%d%m%y).log
# Disk space check
dusage=$(df -Ph | grep -E "$MOUNTPOINT" | sed s/%//g | awk -v spaceWarn=$MV_SPACE_WARN '{ if($5 >= spaceWarn) print $0;}')
if [ "$dusage" != "" ]; then
echo "WARNING: Disk Space Alert - sending email"
echo "$dusage" | mailx -s "WARNING: Rubrik MV Disk Space Alert On $(hostname) at $(date)" $ALERT_EMAILS
else
echo "INFO: Rubrik MV Disk usage is under threshold of ${MV_SPACE_WARN}%"
fi
get_data_mv
open_mv
if [ $? -ne 0 ]; then
echo ERROR: Unable to open MV, aborting
exit_with_error
fi
echo Running RMAN with log to $RMAN_LOG
if [[ $numChannels -eq 1 ]]; then
allocate="allocate channel 'ch1' device type disk format '$MOUNTPOINT/%U';"
release="release channel ch1;"
channel0="$MOUNTPOINT"
else
for i in $(seq 0 $(($numChannels - 1))); do
allocate+="allocate channel 'c$i' device type disk format '$MOUNTPOINT/c$i/%U';"
release+="release channel c$i;"
done
channel0="$MOUNTPOINT/c0"
fi
# Save the current time (minus one hour) to ensure we catch all archive logs
startTime=$(date +%m-%d-%Y\ %H:%M:%S -d '-1 hour')
# RMAN Part Here
###############################################################################
#cat > /tmp/rman.cmd <<EOF
# Now perform the backup and merge
rman nocatalog log $RMAN_LOG append > /dev/null <<EOF
connect target /
set echo on;
configure retention policy to recovery window of 1 days;
run {
set controlfile autobackup format for device type disk to '$channel0/cf_%F';
$allocate
backup incremental level 1 for recover of copy with tag 'rubrik_snap' database;
recover copy of database with tag 'rubrik_snap';
backup as copy current controlfile;
backup archivelog from time '$startTime' tag 'rubrik_snap_logs';
$release
}
allocate channel for maintenance device type disk;
crosscheck backup;
crosscheck copy;
delete noprompt obsolete device type disk;
delete noprompt backup of archivelog all completed before 'sysdate-3' tag rubrik_snap_logs;
release channel;
EOF
###############################################################################
rmanStatus=$?
if [ $rmanStatus -ne 0 ]; then
echo "WARNING: RMAN DB backup exited with Non-zero status $rmanStatus. Emailing log"
cat $RMAN_LOG | mailx -s "RMAN Log backup Failed on ${HOST} for ${ORACLE_SID}" $ALERT_EMAILS
elif [ $EMAIL_SUCCESS -eq 1 ]; then
echo "SUCCESS: RMAN DB backup exited with zero status"
echo "RMAN job finished with Zero status. Note that this does not guarantee the contents of the backup." | mailx -s "SUCCESS: RMAN DB backup exited with zero status on ${HOST} for ${ORACLE_SID}" $ALERT_EMAILS
fi
# Change permission of backup files to enable alternate host restore
find $MOUNTPOINT -type f -exec chmod 644 -- {} + 2>/dev/null
close_mv