Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
845 views

ORACLE-BASE - Oracle Shell Scripting

This document discusses techniques for creating shell scripts to connect to Oracle databases using SQL*Plus and RMAN from Windows and UNIX/Linux environments. It provides examples of creating batch files and shell scripts to run SQL and RMAN commands, retrieve values from SQL queries, and check if a database is available before running applications.

Uploaded by

theahmadkhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
845 views

ORACLE-BASE - Oracle Shell Scripting

This document discusses techniques for creating shell scripts to connect to Oracle databases using SQL*Plus and RMAN from Windows and UNIX/Linux environments. It provides examples of creating batch files and shell scripts to run SQL and RMAN commands, retrieve values from SQL queries, and check if a database is available before running applications.

Uploaded by

theahmadkhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Home(/index.

php) / Articles(/articles) / Misc(/articles/misc) / Here

OracleShellScripting
ThisarticlepresentssomebasictechniquesforcreatingWindowsbatchfilesandUNIX/Linuxshellscriptsthat
connecttoSQL*PlusandRMAN.
Windows
UNIXandLinux(Method1)
UNIXandLinux(Method2)
UNIXandLinux(ReturningvaluesfromSQL)
CheckingtheDatabaseisUp

Windows
TorunaSQLscriptusingSQL*Plus,placetheSQLalongwithanySQL*Pluscommandsinafileandsaveiton
youroperatingsystem.Forexample,savethefollowingscriptinafilecalled"C:\emp.sql".

CONNECTscott/tiger
SPOOLC:\emp.lst
SETLINESIZE100
SETPAGESIZE50
SELECT*
FROMemp;
SPOOLOFF
EXIT;

Next,createabatchfilecalled"C:\get_emp.bat"containingthefollowingcommand.
sqlplus/nolog@C:\emp.sql

Theresultingbatchfilecanberunmanually,bydoubleclickingonit,orscheduledusingtheScheduledTasksWizard(Start>Programs>Accessories>System
Tools>ScheduledTasks)ortheATscheduler.
ThemethodisverysimilarwhenusingRecoveryManager(RMAN).Asanexample,placethefollowingRMANcommandsinafilecalled"C:\cmdfile.txt".
RUN{
ALLOCATECHANNELch1TYPE
DISKFORMAT'C:\oracle\backup\DB10G%d_DB_%u_%s_%p';
BACKUPDATABASEPLUSARCHIVELOG;
RELEASECHANNELch1;
}
EXIT;

Nextcreateabatchfilecalled"C:\backup.bat"containingthefollowingcommand.
rmantarget=/@cmdfile.txt

Thiscommandcanincludea catalog= entryifarecoverycatalogisused.Onceagain,resultingbatchfilecanberunmanuallyorscheduled.

UNIXandLinux(Method1)
ThepreviousmethodsworksequallywellinUNIXandLinuxenvironments.Forexample,savethefollowingscriptinafilecalled"/u01/emp.sql".
CONNECTscott/tiger
SPOOL/u01/emp.lst
SETLINESIZE100
SETPAGESIZE50
SELECT*
FROMemp;
SPOOLOFF
EXIT;

Next,createashellscriptcalled"/u01/get_emp.ksh"containingthefollowinglines.
#!/bin/ksh
sqlplus/nolog@/u01/emp.sql

Thefollowingcommandmakesthefileexecutableforthefileowner.
chmodu+x/u01/get_emp.ksh

Theresultingshellscriptcanberunmanuallyfromthecommandline,orscheduledusingCRON.
ForRMAN,placethefollowingRMANcommandsinafilecalled"/u01/cmdfile.txt".
RUN{
ALLOCATECHANNELch1TYPE
DISKFORMAT'/u01/backup/DB10G/%d_DB_%u_%s_%p';
BACKUPDATABASEPLUSARCHIVELOG;
RELEASECHANNELch1;
}
EXIT;

Nextcreateabatchfilecalled"/u01/backup.ksh"containingthefollowinglines.
#!/bin/ksh
rmantarget=/@/u01/cmdfile.txt

Thiscommandcanincludea catalog= entryifarecoverycatalogisused.Onceagain,resultingshellscriptmustbemadeexecutableusingthefollowing


command.
chmodu+x/u01/backup.ksh

Theshellscriptisnowreadytorun.

UNIXandLinux(Method2)
UNIXandLinuxenvironmentsalsoallowtheSQL*PlusandRMANcommandstobepipeddirectlyfromthecommandline.Forexample,savethefollowing
commandsinafilecalled"/u01/get_emp.ksh".
#!/bin/ksh
sqlplus/nolog<<EOF
CONNECTscott/tiger
SPOOL/u01/emp.lst
SETLINESIZE100
SETPAGESIZE50
SELECT*
FROMemp;
SPOOLOFF
EXIT;
EOF

Noticethe"<<EOF"and"EOF"tags,indicatingthestartandendofthecommandbeingpipedintotheSQL*Plusexecutable.Theshellscriptismadeexecutable
usingthefollowingcommand.
chmodu+x/u01/get_emp.ksh

TheshellscriptisreadytoberunmanuallyfromthecommandlineorscheduledusingCRON.
ThefollowingexampleshowshowRMANcanusethesamemethod.Createafilecalled"/u01/backup.ksh"withthefollowingcontents.
#!/bin/ksh
rmantarget=/<<EOF
RUN{
ALLOCATECHANNELch1TYPE
DISKFORMAT'/u01/backup/DB10G/%d_DB_%u_%s_%p';
BACKUPDATABASEPLUSARCHIVELOG;
RELEASECHANNELch1;
}
EXIT;
EOF

Onceagain,thescriptcanbemadeexecutableusingthefollowingcommand.
chmodu+x/u01/backup.ksh

Theshellscriptisnowreadytorun.

UNIXandLinux(ReturningvaluesfromSQL)
Thefollowingcodeshowascripttopulltheoutputofaqueryintoashellscriptvariable.
#!/bin/bash
RETVAL=`sqlplussilentscott/tiger<<EOF
SETPAGESIZE0FEEDBACKOFFVERIFYOFFHEADINGOFFECHOOFF
SELECT*FROMemp;
EXIT;
EOF`
if[z"$RETVAL"];then
echo"Norowsreturnedfromdatabase"
exit0
else
echo$RETVAL
fi

Ifyouarereturningasinglevalue,thismethodworkswell.Ifyouarereturningmultiplerowsofmultiplecolumnsitgetsabitmessyandforcesyoutoparsethe
returnvalue.

CheckingtheDatabaseisUp
Itissometimesnecessarytocheckthedatabaseisupbeforeperformingatask.Inthefollowingexample,wecheckthedatabaseisupbeforestartingaTomcat
applicationserver.Ifthedatabaseisdown,thescriptsleepsfor5minutes,thenchecksagain.
ThescriptrequiresanOracleClienttomakeconnectionstothedatabase.Thiscouldbeafullclientinstallation,oranOracleInstantClient(oracleinstantclient
installation.php)installation.
Createascriptcalled"check_db.sh"willthefollowingcontents.Thisisthescriptthatwillcheckifthedatabaseisupornot.

#EnvironmentvariablesnecessaryforOracleInstantClient
exportLD_LIBRARY_PATH=/home/tomcat/scripts/instantclient_11_2
exportPATH=$PATH:$LD_LIBRARY_PATH
functioncheck_db{
CONNECTION=$1
RETVAL=`sqlplussilent$CONNECTION<<EOF
SETPAGESIZE0FEEDBACKOFFVERIFYOFFHEADINGOFFECHOOFF
SELECT'Alive'FROMdual;
EXIT;
EOF`
if["$RETVAL"="Alive"];then
DB_OK=0
else
DB_OK=1
fi
}

Next,createascriptcalled"tomcat_start_dev.sh"withthefollowingcontents.
scriptPath=${0%/*}
source$scriptPath/check_db.sh
CONNECTION="up_check_user/password@//hostname:1523/service"
echo"WaituntilDBisup"
check_db$CONNECTION
while[$DB_OK=1]
do
echo"DBnotupyet.Sleepingfor5mins(CTRL+Ctoexit)"
sleep300
check_db$CONNECTION
done
echo"Starting"
echo"DEV:/u01/dev"
/u01/dev/bin/tomcatstart

Thissourcesthe"check_db.sh"script,soitisincludedasifitwerepartofthisscript.Itcouldbecombined,butthisallowsthe"check_db.sh"scripttobesharedby
multiplescripts.The"tomcat_start_dev.sh"scriptloopsround,checkingtoseeiftheDBisup.Onceitisup,itfallsthroughtotheTomcatstartcommand.
Translate

Formoreinformationsee:

SQL*PlusUser'sGuideandReference(http://docs.oracle.com/cd/B19306_01/server.102/b14357/toc.htm)
OracleDatabaseBackupandRecoveryReference(http://docs.oracle.com/cd/B19306_01/backup.102/b14194/toc.htm)
UNIXCommandsforDBAs(/articles/misc/unixfordbas.php)
Hopethishelps.RegardsTim...
BacktotheTop.

Pl Sql Expert
Download the 30 day trail version for PL/SQL IDE!

11comments,read/addthem...(/misc/comments.php?page_id=340)

Home(/index.php)|Articles(/articles/articles.php)|Scripts(/dba/scripts.php)|Blog(/blog/)|Certification(/misc/ocpcertification.php)|Misc
(/misc/miscellaneous.php)|About(/misc/siteinfo.php)
Copyright&Disclaimer(/misc/siteinfo.php#copyright)
HTML(http://validator.w3.org/check?uri=referer)CSS(http://jigsaw.w3.org/cssvalidator/check/referer)

You might also like