#!/bin/bash
#
# nightly - execute scripts each night
#

# Go to directory where this script is placed
cd `dirname $0`


# -----
# Set environment
. ./oracle_env
if [ $? -ne 0 ] ; then
  echo
  echo "Error: Cannot source Oracle's environment script './oracle_env'"
  echo
  exit 1
fi


# -----
# Generate a daily report of the database performance snapshots

./ora_statspack REPORT DAILY_REPORT

# -----
# Generate a database configuration report

./ora_dbinfo


# -----
# Wait for up to 50 minutes (3000 seconds) before starting the rman backup
WAITFOR=$(( $RANDOM % 3000 ))
sleep $WAITFOR


# -----
# Backup of database

# Which weekday?
# 1:monday, ... 6:saturday, 7:sunday
WEEKDAY=`date +"%u"`

if [ $WEEKDAY -eq 7 ]; then

  # Do a full backup on sundays.
  # (The parameter must match a parameter in the ORARMAN section in the configuration file!)

  ./orarman LEVEL0

else

  # Do a cumulative incremental backup all other weekdays.
  # (The parameter must match a parameter in the ORARMAN section in the configuration file!)
  ./orarman LEVEL1

fi



# -----
# Remove old audit files
#   Set the path according to Oracle's parameter 'audit_file_dest'.
#
find /oracle/admin/$ORACLE_SID/?dump -follow -type f -mtime +10 -exec rm {} \; > /dev/null 2>&1

# -----
# Remove old archived redo logs
#   Only together with old style database backup!
#   RMAN will take care of the archived redo logs itself.
#   Set the path to 'log_archive_dest_?', perhaps you need
#   multiple entries. Adjust the '+10' (days) to your needs.
#   '-follow' is needed for linked directories.
#
# find /oracle/backup/$ORACLE_SID/archived_redo_logs/ -follow -type f -mtime +10 -exec rm {} \; > /dev/null 2>&1
# find /oracle/archived_redo_logs/$ORACLE_SID/ -follow -type f -mtime +10 -exec rm {} \; > /dev/null 2>&1

# -----
# Remove old connection protocol files.
#
find /oracle/admin/$ORACLE_SID/connection_protocol -name "prot_*" -follow -type f -mtime +10 -exec rm {} \; > /dev/null 2>&1


# -----
# Custom Section
#
# Create an executable script 'nightly_custom' for your special purposes.

[ -x ./nightly_custom ] && ./nightly_custom

