1. Get rid of all advertisements and get unlimited access to documents by upgrading to Premium Membership. Upgrade to Premium Now and also get a Premium Badge!

Help with script

Discussion in 'Server Administration and Options' started by rajclb, Sep 8, 2015.

  1. rajclb

    rajclb Active Member

    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    55
    Location:
    Charlotte
    -- Run truncate of the table only after complete backup

    I am running the backup for the table with the below script
    Would you please help me with the truncate part of the script
    the truncate of the table should run only for successful complete of the table backup


    #!/bin/sh
    #set -x
    #


    export MAILADDRESS=nrrajdba@gmail.com


    #-----------------------------------------------
    export ORACLE_SID=ORA
    export ORACLE_BASE=/apps/oracle
    export ORACLE_HOME=$ORACLE_BASE/11.2.0.3
    export EXP_DIR=/backups/oracle/Table_backup
    #-----------------------------------------------
    BOX='uname -n'
    #-----------------------------------------------
    date
    #-----------------------------------------------
    #


    find $EXP_DIR/ -mtime +1 -name "*.dmp*" -exec rm -f {} \;
    find $EXP_DIR/ -mtime +1 -name "*.log*" -exec rm -f {} \;

    $ORACLE_HOME/bin/expdp \'/ as sysdba\' directory=XPL_BKP DUMPFILE=`date '+%m%d%y_%H%M%S'`_XPL_%U.dmp LOGFILE=`date '+%m%d%y_%H%M%S'`_xpl.log PARALLEL=2 TABLES=SIGMA.XPL compression=all > /dev/null


    Any help is appreciated

    --
    Thanks
     
  2. rajclb

    rajclb Active Member

    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    55
    Location:
    Charlotte
    Looking for If else condition in the script
    truncate should run only if the backup is successful else it should return without truncating table
     
  3. zargon

    zargon Community Moderator Forum Guru

    Messages:
    2,351
    Likes Received:
    348
    Trophy Points:
    1,430
    Location:
    Aurora, CO
    You need to understand shell script constructs so you can access return codes. The $? construct holds the return code of the last executable and can be accessed within the script; this will return the exit code of the expdp command:

    $ORACLE_HOME/bin/expdp \'/ as sysdba\' directory=XPL_BKP DUMPFILE=`date '+%m%d%y_%H%M%S'`_XPL_%U.dmp LOGFILE=`date '+%m%d%y_%H%M%S'`_xpl.log PARALLEL=2 TABLES=SIGMA.XPL compression=all > /dev/null

    RETCD=$?

    Once you access $? it gets reset to 0 so you need to use the construct shown above to store the return code so you can use it throughout the script. Now you can use $RETCD in an if-then-else block and conditionally execute what you want to execute. expdp returns three exit codes:

    0 -- Success
    5 -- Success , but with non-fatal errors visible in the log and sent to the screen
    1 -- Fatal error

    If I understand this correctly you want to truncate the table ONLY on a 0 exit code so the following should work for you:

    if [ $RETCD -eq 0 ]
    then
    <code to truncate your table here>
    else
    echo "Export generated errors -- please check log file"
    fi

    Again, understanding the shell is key to writing good scripts.
     
  4. rajclb

    rajclb Active Member

    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    55
    Location:
    Charlotte
    Hi David,

    Thanks for your help and time
    As recommended understanding the shell will definitely help

    Thank you