Thursday, July 11, 2013

Database auto start on server reboot: Linux

The objective of this script is to capture whatever databases and listeners were running prior to server reboot and stop and start them automatically.

Environment:
OS: Linux Server

Note: We are placing all the scripts at location /u01/app/oracle/scripts

Version:
Applicable to Oracle database version 9i, 10g, 11g

Steps to be executed as oracle user:

Step 1. Create the scripts 1-7 and keep them at location /u01/app/oracle/scripts
(once you have created the scripts go the directory /u01/app/oracle/scripts and execute the command chmod a+x *.sh)

Step 2. Create a crontab job to run the Script logcapture.sh at a particular time stamp.

Command for crontab Entry: 

[oracle@host1 ~]$ export EDITOR=vi
[oracle@host1 ~]$ crontab -e 

and add the lines mentioned below from the oracle userid.
-- we are capturing the logs every night at 8 PM

0 20 * * * sh /u01/app/oracle/scripts/logcapture.sh >> /dev/null 2>&1

save it

Steps to be executed as root user:

Step 3. create a file with the name dbora at location /etc/init.d   (Content are given below)

Step 4. Change the Permission of the dbora file
chmod 775 /etc/init.d/dbora

Step 5. create links to this script to be executed under different run levels using given commands:

ln -s /etc/init.d/dbora /etc/rc.d/rc0.d/K01dbora
ln -s /etc/init.d/dbora /etc/rc.d/rc1.d/S99dbora
ln -s /etc/init.d/dbora /etc/rc.d/rc2.d/S99dbora
ln -s /etc/init.d/dbora /etc/rc.d/rc3.d/S99dbora
ln -s /etc/init.d/dbora /etc/rc.d/rc4.d/S99dbora
ln -s /etc/init.d/dbora /etc/rc.d/rc5.d/S99dbora
ln -s /etc/init.d/dbora /etc/rc.d/rc6.d/K01dbora

Step 6. Add this to chkconfig and verify the same.

chkconfig –-add dbora

chkconfig –-list dbora (to verify its added to the run levels)


Scripts:

Script 1. startdb.sh
#!/bin/bash
# SCRIPT: startdb.sh
# PURPOSE: Automatically starts all the databases which were running prior to server reboot.
# Owner: Samrat
FILENAME=/u01/app/oracle/scripts/dbrunning.log
cat $FILENAME | while read LINE
do
ORACLE_SID=$LINE
export ORACLE_SID
echo $ORACLE_SID
ORACLE_HOME=`cat /etc/oratab|grep ^$ORACLE_SID:|cut -f2 -d':'`
export ORACLE_HOME
export PATH=$ORACLE_HOME/bin:$PATH
sqlplus -S "/as sysdba" @/u01/app/oracle/scripts/startdb.sql
done

Script 2. stopdb.sh
#!/bin/bash
# SCRIPT: stopdb.sh
# PURPOSE: Automatically stops all the databases which were running prior to server reboot.
# Owner: Samrat
FILENAME=/u01/app/oracle/scripts/dbrunning.log
cat $FILENAME | while read LINE
do
ORACLE_SID=$LINE
export ORACLE_SID
echo $ORACLE_SID
ORACLE_HOME=`cat /etc/oratab|grep ^$ORACLE_SID:|cut -f2 -d':'`
export ORACLE_HOME
export PATH=$ORACLE_HOME/bin:$PATH
sqlplus -S "/as sysdba" @/u01/app/oracle/scripts/stopdb.sql
done

Script 3. listenerstart.sh
#!/bin/bash
# SCRIPT: listenerstart.sh
# PURPOSE: Automatically starts all the listeners which were running prior to server reboot.
# Owner: Samrat
FILENAME=/u01/app/oracle/scripts/lsnrup.log
cat $FILENAME | while read LINE
do
ORACLE_HOME=`echo $LINE | cut -f1 -d' '`
export ORACLE_HOME
export PATH=$ORACLE_HOME/bin:$PATH
LISTENER=`echo $LINE|cut -f2 -d' '`
export LISTENER
echo $LISTENER
lsnrctl start $LISTENER
done

Script 4. listenerstop.sh
#!/bin/bash
# SCRIPT: listenerstop.sh
# PURPOSE: Automatically stops all the listeners which were running prior to server reboot.
# Owner: Samrat
FILENAME=/u01/app/oracle/scripts/lsnrup.log
cat $FILENAME | while read LINE
do
ORACLE_HOME=`echo $LINE | cut -f1 -d' '`
export ORACLE_HOME
export PATH=$ORACLE_HOME/bin:$PATH
LISTENER=`echo $LINE|cut -f2 -d' '`
export LISTENER
echo $LISTENER
lsnrctl stop $LISTENER
done

Script 5. startdb.sql
startup;
select * from global_name;
exit;

Script 6. stopdb.sql
select * from global_name;
shutdown immediate;
exit;

Script 7. logcapture.sh (add this script to run as crontab job)
ps aux | awk '{print $11}' | grep pmon | cut -f3 -d'_' > /u01/app/oracle/scripts/dbrunning.log
ps aux | awk '{print $11" " $12}' |grep -i tns |grep -v grep |sed 's/\/bin\/tnslsnr//' > /u01/app/oracle/scripts/lsnrup.log

Script. dbora

#!/bin/bash
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.
# Owner: Samrat
# Set ORA_OWNER to the user id of the owner of the oracle software

ORA_OWNER=oracle

case "$1" in
'start')
                su - $ORA_OWNER -c /u01/app/oracle/scripts/startdb.sh > /u01/app/oracle/scripts/startup_db.log 2>&1
su - $ORA_OWNER -c /u01/app/oracle/scripts/listenerstart.sh > /u01/app/oracle/scripts/startup_lsnr.log 2>&1
                touch /var/lock/subsys/dbora
                ;;
'stop')
                su - $ORA_OWNER -c /u01/app/oracle/scripts/stopdb.sh > /u01/app/oracle/scripts/shutdown_db.log 2>&1
su - $ORA_OWNER -c /u01/app/oracle/scripts/listenerstop.sh > /u01/app/oracle/scripts/shutdown_lsnr.log 2>&1
                rm -f /var/lock/subsys/dbora
                ;;

esac

No comments:

Post a Comment