oracle-base - oracle shell scripting

Upload: theahmadkhan

Post on 07-Jan-2016

217 views

Category:

Documents


0 download

DESCRIPTION

racle Shell Scripting

TRANSCRIPT

  • Home(/index.php) / Articles(/articles) / Misc(/articles/misc) / Here

    OracleShellScriptingThisarticlepresentssomebasictechniquesforcreatingWindowsbatchfilesandUNIX/LinuxshellscriptsthatconnecttoSQL*PlusandRMAN.

    WindowsUNIXandLinux(Method1)UNIXandLinux(Method2)UNIXandLinux(ReturningvaluesfromSQL)CheckingtheDatabaseisUp

    WindowsTorunaSQLscriptusingSQL*Plus,placetheSQLalongwithanySQL*Pluscommandsinafileandsaveitonyouroperatingsystem.Forexample,savethefollowingscriptinafilecalled"C:\emp.sql".

    CONNECTscott/tigerSPOOLC:\emp.lstSETLINESIZE100SETPAGESIZE50SELECT*FROMemp;SPOOLOFFEXIT;

    Next,createabatchfilecalled"C:\get_emp.bat"containingthefollowingcommand.

    sqlplus/nolog@C:\emp.sql

    Theresultingbatchfilecanberunmanually,bydoubleclickingonit,orscheduledusingtheScheduledTasksWizard(Start>Programs>Accessories>SystemTools>ScheduledTasks)ortheATscheduler.

    ThemethodisverysimilarwhenusingRecoveryManager(RMAN).Asanexample,placethefollowingRMANcommandsinafilecalled"C:\cmdfile.txt".

    RUN{ALLOCATECHANNELch1TYPEDISKFORMAT'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/tigerSPOOL/u01/emp.lstSETLINESIZE100SETPAGESIZE50SELECT*FROMemp;SPOOLOFFEXIT;

    Next,createashellscriptcalled"/u01/get_emp.ksh"containingthefollowinglines.

    #!/bin/kshsqlplus/nolog@/u01/emp.sql

    Thefollowingcommandmakesthefileexecutableforthefileowner.

    chmodu+x/u01/get_emp.ksh

    Theresultingshellscriptcanberunmanuallyfromthecommandline,orscheduledusingCRON.

    ForRMAN,placethefollowingRMANcommandsinafilecalled"/u01/cmdfile.txt".

    RUN{ALLOCATECHANNELch1TYPEDISKFORMAT'/u01/backup/DB10G/%d_DB_%u_%s_%p';BACKUPDATABASEPLUSARCHIVELOG;RELEASECHANNELch1;}EXIT;

  • Nextcreateabatchfilecalled"/u01/backup.ksh"containingthefollowinglines.

    #!/bin/kshrmantarget=/@/u01/cmdfile.txt

    Thiscommandcanincludea catalog= entryifarecoverycatalogisused.Onceagain,resultingshellscriptmustbemadeexecutableusingthefollowingcommand.

    chmodu+x/u01/backup.ksh

    Theshellscriptisnowreadytorun.

    UNIXandLinux(Method2)UNIXandLinuxenvironmentsalsoallowtheSQL*PlusandRMANcommandstobepipeddirectlyfromthecommandline.Forexample,savethefollowingcommandsinafilecalled"/u01/get_emp.ksh".

    #!/bin/kshsqlplus/nolog

  • #EnvironmentvariablesnecessaryforOracleInstantClientexportLD_LIBRARY_PATH=/home/tomcat/scripts/instantclient_11_2exportPATH=$PATH:$LD_LIBRARY_PATH

    functioncheck_db{CONNECTION=$1RETVAL=`sqlplussilent$CONNECTION