Howdy y’all!
I decided to add some additional features to my Oracle Init script.
In this version I added Database Backup and Archive Log delete with RMAN as well as show status.
Oh…and I included start/stop for Oracle Rest Data Services to the Database start/stop commands.
Usage: /etc/init.d/oractl start|stop|restart|dbbackup|delete_archivelogs|status
Hope you find useful!
#!/bin/bash
#
# init script for starting, stoping, restarting Oracle
#
# Author: Matt D (mattdee@gmail.com)
# Created: 02.18.2014
# Updated: 03.4.2014
# Updated: 07.9.2014 - added Oracle Rest Data Services stop/start, database backup, delete archivelogs, and stack status
#
#
# chkconfig: 123 69 68
# description: Oracle Startup Script
#
#
# Install instructions
#
# Place in /etc/init.d
# chgrp dba oractl
# chmod 750 oractl
# ln -s /etc/init.d/oractl /etc/rc0.d/K01oractl
# ln -s /etc/init.d/oractl /etc/rc3.d/S99oractl
# Set Environment
# Change based on site
export LISTENER_HOME=/u01/app/oracle/product/12.1.0/dbhome_1
export ORA_OWNER=oracle
export PATH=${PATH}:$ORACLE_HOME/bin
mount -o remount,size=16G /dev/shm
# Functions
function start_it()
{
echo "Starting Database Stack"
echo "."
# Start the listener
export ORACLE_HOME=$LISTENER_HOME
mount -o remount,size=16G /dev/shm
su $ORA_OWNER -c "$LISTENER_HOME/bin/lsnrctl start"
# Get a list of the ORACLE_HOME & SID
for i in `cat /etc/oratab`
do
unset ORACLE_SID
unset ORACLE_HOME
unset STARTUP
export ORACLE_SID=`echo $i | grep -v \<| grep : |cut -d: -f1`
export ORACLE_HOME=`echo $i | grep -v \<| grep :|cut -d: -f2`
export STARTUP=`echo $i | grep -v \<| grep : |cut -d: -f3`
if test "${STARTUP}" = Y
then
echo "Starting SID: "${ORACLE_SID}
su $ORA_OWNER -c "$ORACLE_HOME/bin/sqlplus / as sysdba <<EOF
startup;
exit;
EOF
"
else
echo "SID is NOT marked to start!"
fi
done
echo "Starting Oracle Rest Data Services..."
su $ORA_OWNER -c "java -jar /u01/app/oracle/product/ORDS/ords.war standalone --apex-images /u01/app/oracle/product/12.1.0/dbhome_1/apex/images &"
}
function stop_it()
{
echo "Stopping Database Stack"
echo "."
# Stop the database
# Get a list of the ORACLE_HOME & SID
for i in `cat /etc/oratab`
do
unset ORACLE_SID
unset ORACLE_HOME
export ORACLE_SID=`echo $i | grep -v \<| grep : |cut -d: -f1`
export ORACLE_HOME=`echo $i | grep -v \<| grep :|cut -d: -f2`
echo "Stopping Oracle Rest Data Services..."
echo "."
kill -9 `ps -ef | grep -v grep | grep ORDS | awk '{print $2}'`
su $ORA_OWNER -c "$ORACLE_HOME/bin/sqlplus / as sysdba <<EOF
shutdown immediate;
exit;
EOF
"
done
# Stop the listener
export ORACLE_HOME=$LISTENER_HOME
su $ORA_OWNER -c "$LISTENER_HOME/bin/lsnrctl stop"
}
function dbbackup()
{
# Get a list of the ORACLE_HOME & SID
for i in `cat /etc/oratab`
do
unset ORACLE_SID
unset ORACLE_HOME
unset STARTUP
export ORACLE_SID=`echo $i | grep -v \<| grep : |cut -d: -f1`
export ORACLE_HOME=`echo $i | grep -v \<| grep :|cut -d: -f2`
export STARTUP=`echo $i | grep -v \<| grep : |cut -d: -f3`
if test "${STARTUP}" = Y
then
RUNTIME=`date +%m%d%y%H%M`; export RUNTIME
#export ORACLE_SID=$SID
su $ORA_OWNER -c "$ORACLE_HOME/bin/rman target / <<EOF
startup mount;
backup database spfile current controlfile PLUS ARCHIVELOG
#shutdown;
exit;
EOF
"
else
echo "#######################"
echo "Only databases marked with a Y in /etc/oratab will be backed up."
echo "#######################"
exit 1
fi
done
}
function delete_archivelogs()
{
# Get a list of the ORACLE_HOME & SID
for i in `cat /etc/oratab`
do
unset ORACLE_SID
unset ORACLE_HOME
unset STARTUP
export ORACLE_SID=`echo $i | grep -v \<| grep : |cut -d: -f1`
export ORACLE_HOME=`echo $i | grep -v \<| grep :|cut -d: -f2`
export STARTUP=`echo $i | grep -v \<| grep : |cut -d: -f3`
if test "${STARTUP}" = Y
then
#export ORACLE_SID=$SID
su $ORA_OWNER -c "$ORACLE_HOME/bin/rman target / <<EOF
startup mount;
delete noprompt archivelog all;
#shutdown;
exit;
EOF
"
fi
done
}
function stack_status()
{
echo "########################"
echo "Oracle Database Status"
echo "########################"
ps -ef | grep -v grep | grep pmon
echo "########################"
echo "Listener Status"
echo "########################"
export ORACLE_HOME=$LISTENER_HOME
su $ORA_OWNER -c "$LISTENER_HOME/bin/lsnrctl status"
}
case "$1" in
start)
start_it
;;
stop)
stop_it
;;
restart)
echo "Restarting Database Stack"
echo "."
stop_it
start_it
;;
dbbackup)
echo "Back up Database"
dbbackup
;;
delete_archivelogs)
echo "Deleting Archivelogs"
delete_archivelogs
;;
status)
stack_status
;;
*)
echo "Usage: /etc/init.d/oractl start|stop|restart|dbbackup|delete_archivelogs|status"
exit 1
;;
esac
Leave a comment