Script for Export backup!!

Dear Team,

Can you please help me to write the script for export backup.
I've written some of the part and stuck in "if" condition Apprecatied if you help me.

Requirement:- Export backup completion sent the email if it is success (sent backup success email)or if fails (sent backup fails email).

My Shell Script:-
------------

######################### START OF SCRIPT #########################
#!/bin/bash
#Backup script by Export backup.
export HOST_NAME=`uname -n`
CDATE=`date +%d%m%Y`
export CDATE
echo $CDATE
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/product/10.2.0
ORACLE_SID=icba
PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
export ORACLE_BASE ORACLE_HOME PATH ORACLE_SID
ADMIN_EMAIL=xxxxx@email.id
LOGFILE=/home/oracle/dump/expdp_$CDATE.log
EXPORT_PATH=/home/oracle/dump/export_backup
DMP_FILE=expdp_$CDATE.dmp
LOG_FILE=expdp_$CDATE.log
export DMP_FILE LOG_FILE
echo Starting . > $LOGFILE
expdp system/password schemas=XXXXX directory=dump_dir DUMPFILE=$DMP_FILE LOGFILE=$LOG_FILE
#expdp mrmrs/mrmrs TABLES=resident,sejour DIRECTORY=EXPORT_BACKUP_DIR DUMPFILE=$DMP_FILE LOGFILE=$LOG_FILE
cat $EXPORT_PATH/$LOG_FILE >> $LOGFILE
echo Backup Completed. >> $LOGFILE
echo gzipping . >> $LOGFILE
gzip $EXPORT_PATH/$DMP_FILE >> $LOGFILE
echo gzipping Completed. >> $LOGFILE
#echo Deleting old files >> $LOGFILE
#find $EXPORT_PATH -type f -mtime +31 | xargs rm >> $LOGFILE
echo Delete Completed >> $LOGFILE
uuencode $LOGFILE expdb_$CDATE | mail -s "$HOST_NAME:$SID Export backup success on `CDATE`" $ADMIN_EMAIL
[oracle@testdb dump]$

Thank you,
Mohammed Fareed.

At which place do you want to check for success or failure? And on which criteria?

Thanks for update Anchal, after this command runs (expdp system/manager1 schemas=PRODUCTION1 directory=dump_dir DUMPFILE=$DMP_FILE LOGFILE=$LOG_FILE) should check the logfile if it is success send success email if failed send backup failed.

Thank you.

What if the command fails for some reason?
I am not aware with that command but I guess surely you could check with the exit status of the command.

After you run the command, you could do something like..

if [ $? -eq 0 ]; then
 #success mail
 #update logfile
else
 #fail mail
 #update logfile
fi

Check the manual for your command to confirm the return values.

AFAIR, oracle doesn't return the job completion status to the command line such that you can query $? after the expdp command.

So you have to refer to a line in the output file $LOG_FILE in your case for a pattern like

I googled for data pump and got this.
ORACLE-BASE - Oracle Database 10g Data Pump (expdp and impdp)
http://www.oracle-base.com/articles/10g/expdpEMP_DEPT.log

It is apparent that there is no "return value" as we want.

OK

Thanks for your update both of you.I tried with the given code it dozen work.
I also agree with Ananthp update.

My requirement is script should check export log($LOG_FILE)file should be checked if they found ("ORA-" "Linux-x86_64 Error" "stopped" "Failed" ) then sent backup failed email.

orelse sent backup successfull email. Hope you understand my needy.

Thank you.
Fareed.

Ok. As per your requirement to check from logs,
you could grep the keywords like...

egrep -q "ORA-|Linux-x86_64 Error|stopped|Failed" $LOG_FILE
if [ $? -ne 0 ]; then
 #success mail
 #update logfile
else
 #fail mail
 #update logfile
fi

Thank you Anchal for update.

I tried this code manually seems grep is not working ..

[oracle@prodb dump]$  egrep -q "ORA-|Linux-x86_64 Error|stopped|Failed" failelog07032012.log
[oracle@prodb dump]$ grep -i ORA- failelog07032012.log
ORA-31693: Table data object "PRODUCTION1"."TR33MTH2" failed to load/unload and is being skipped due to error:
ORA-19502: write error on file "/home/oracle/dump/07032012.dmp", blockno 162017 (blocksize=4096)
ORA-27072: File I/O error
ORA-31693: Table data object "PRODUCTION1"."CF89LOG" failed to load/unload and is being skipped due to error:
ORA-19502: write error on file "/home/oracle/dump/07032012.dmp", blockno 162273 (blocksize=4096)
ORA-27072: File I/O error
ORA-31693: Table data object "PRODUCTION1"."CF01OLAY" failed to load/unload and is being skipped due to error:
ORA-19502: write error on file "/home/oracle/dump/07032012.dmp", blockno 162529 (blocksize=4096)
ORA-27072: File I/O error
ORA-31693: Table data object "PRODUCTION1"."IV33DETAIL" failed to load/unload and is being skipped due to error:
ORA-19502: write error on file "/home/oracle/dump/07032012.dmp", blockno 162785 (blocksize=4096)
ORA-27072: File I/O error
ORA-31693: Table data object "PRODUCTION1"."GL13IMPORT" failed to load/unload and is being skipped due to error:
ORA-19502: write error

can you confirm me codes is correct?

egrep -q "ORA-|Linux-x86_64 Error|stopped|Failed" $LOG_FILE
if [ $? -eq 0 ]; then
uuencode $LOGFILE expdb_$CDATE | mail -s "$HOST_NAME:$SID Export backup success on `CDATE`" $ADMIN_EMAIL   
#update logfile----? wat i have to mention.
else
 #uuencode $LOGFILE expdb_$CDATE | mail -s "$HOST_NAME:$SID Export backup Failed on `CDATE`" $ADMIN_EMAIL   
 #update logfile----?wat i have to mention.
fi

Thanks you,
Fareed.

-q is for [q]uiet mode. That doesn't output the matched records.
If you do want to check, re-run the command without -q option.

---------- Post updated at 16:06 ---------- Previous update was at 16:02 ----------

The command will return 0 if it finds the matches (eg.errors in your case)
I guess you should revert the if/else cases. (as I mentioned in my previous post)

Great Help!!! I'm finally i made this possible with the help of you,Thank you Anchal Khare!! I Apprecatied if you allow me in your Add list of this Group.