Operating System - HP-UX
1752802 Members
4956 Online
108789 Solutions
New Discussion

Shell script for rman backup

 
Alok_Behria
Advisor

Shell script for rman backup

 

Hi All,

 

I am trying to configure rman backup to kick off through shell. here , I wrote following code with the help of exesiting script, but it seems that "if then else"is still an issue for me. Please offer your advise , as I tried to get this through , but all in vein. I am also looking to write this in such a manner , where in , if the database is down , then it simply send me a mail and if the the database is up , then execute the code , which I wrote here. thanks a lot in advance.

 

#!/usr/bin/sh
. epmqa
SID=EPMQA
DATE=`date '+%m%d%y'`
MAIL_LIST="aajitkumar@csc.com"
LOG_DIR=/db/epmqa/export/rman
LOG_FILE=${LOG_DIR}/${DATE}.log
ps -ef | grep -i $SID | grep pmon
if [ $? -eq 0 ] then
rman connect target /
spool log to '/db/epmqa/export/rman/$LOG_FILE'
shutdown immediate ;
startup mount;
{  
backup database tag = 'wkly_cold_backup';
backup current controlfile tag = 'curr_ctl_file_bkp';
backup spfile tag = 'spfile_bkp';
}
grep "ORA-" $LOG_FILE
if [ $? -eq 0 ] then
mailx -s 'Rman backup of $SID has some issues ' $MAIL_LIST < $LOG_FILE
if
grep "RMAN-" $LOG_FILE
if [ $? -eq 0 ] then
mailx -s 'Rman backup of $SID has some issues ' $MAIL_LIST < $LOG_FILE
else
mailx -s 'Rman backup of $SID completed successfully $MAIL_LIST < $LOG_FILE
fi
fi
fi
fi

rman2.sh[9]: Syntax error at line 27 : `else' is not expected.

 P.S. This thread has been moved from HP-UX > General to HP-UX > languages. - Hp Forum Moderator

9 REPLIES 9
Dennis Handly
Acclaimed Contributor

Re: Shell script for RMAN backup

>"if then else"is still an issue for me.

 

It seems you have an "if" with nothing on the line.

It also helps if you have a consistent indentation style so we can see what matches.

 

>ps -ef | grep -i $SID | grep pmon

 

You should use "grep -q pmon" so you don't have this going to stdout.

 

>shutdown immediate ;

 

No need for semicolons.

 

{ ... }

 

No need for braces either.

 

>grep "ORA-" $LOG_FILE

 

Again, add -q to grep, here and below.

 

>if

>grep "RMAN-" $LOG_FILE

 

What was your intention with that "if"?  Missing expression and "then".

 

>mailx -s 'Rman backup of $SID has some issues '

 

Why have a trailing space in the subject?

 

 

 

 

Alok_Behria
Advisor

Re: Shell script for RMAN backup

First of all , thanks a lot. please find below my remarks.

 

>>It seems you have an "if" with nothing on the line.
  It also helps if you have a consistent indentation style so we can see what matches.

I am using if condirtion to test pmon proces , if it is there , then I need to invoke rman utility , which
is a database backup utility

if [ $? -eq 0 ] then
rman connect target /

 

>ps -ef | grep -i $SID | grep pmon

 

>>You should use "grep -q pmon" so you don't have this going to stdout.
got that.
 

>>shutdown immediate ;
since , it is database command , i need to put ";" there.

 

No need for semicolons.

 

{ ... }

 

>> No need for braces either.

Are you refering LOG_FILE=${LOG_DIR}/${DATE}.log here.
 

>>grep "ORA-" $LOG_FILE

you meant grep -q "ORA-"

 

Again, add -q to grep, here and below.

 

>if

>grep "RMAN-" $LOG_FILE

 

>> What was your intention with that "if"?  Missing expression and "then".

I need to grep ORA- or RMAN- from the log file output , if either of string found , then i need a mail stating backup has failed.
becuase. rman will throw rman- or ora- errors in that case.

 

>mailx -s 'Rman backup of $SID has some issues '

 

>> Why have a trailing space in the subject?

got that..

Dennis Handly
Acclaimed Contributor

Re: Shell script for RMAN backup

>I am using if condition to test pmon process

 

The empty "if" doesn't do that.

 

>>shutdown immediate ;
>it is database command, I need to put ";" there.

 

Unless you are using a here doc, these are all shell commands.  Perhaps you intended:

rman connect target / <<EOF

spool log to '/db/epmqa/export/rman/$LOG_FILE'

shutdown immediate ;

startup mount;

{

backup database tag = 'wkly_cold_backup';

backup current controlfile tag = 'curr_ctl_file_bkp';

backup spfile tag = 'spfile_bkp';

}

EOF
 
>> No need for braces either.
>Are you referring LOG_FILE=${LOG_DIR}/${DATE}.log here.

 

No the ones after: startup mount;
 
>you meant grep -q "ORA-"

 

Yes.
 
>I need to grep ORA- or RMAN- from the log file output, if either of string found

 

Then you should replace by one grep with -e:

grep -q -e "ORA-" -e "RMAN-" $LOG_FILE

if [ $? -eq 0 ]; then

   mailx -s 'Rman backup of $SID has some issues' $MAIL_LIST < $LOG_FILE

else

 

Note the semicolon before the "then".  You have that mistake in several places.

Alok_Behria
Advisor

Re: Shell script for RMAN backup

 

Thanks a lot for your help here. I tried to create a script based on your inputs, but it seems , it's not coming out from rman. the backup part has beed done, but somehow , it's not exiting from rman. Please find below my comments. thanks in advance.

 

#!/usr/bin/sh
. epmqa
SID=EPMQA
DATE=`date '+%m%d%y'`
MAIL_LIST="aajitkumar@csc.com"
LOG_DIR=/db/epmqa/export/rman
LOG_FILE=${LOG_DIR}/${DATE}.log
ps -ef | grep -i $SID | grep pmon
if [ $? -eq 0 ]; then
rman  <<EOF
connect target /
spool log to '$LOG_FILE'
shutdown immediate ;
startup mount;
{
backup current controlfile tag = 'curr_ctl_file_bkp';
backup spfile tag = 'spfile_bkp';
}
exit   #### problem here
EOF
grep -q -e "ORA-" -e "RMAN-" $LOG_FILE  ## Please explain this line
if [ $? -eq 0 ]; then
mailx -s 'Rman backup of $SID has errors ' $MAIL_LIST < $LOG_FILE
else
mailx -s 'Rman backup of $SID completed successfully' $MAIL_LIST < $LOG_FILE
fi
fi

 

Alok_Behria
Advisor

Re: Shell script for RMAN backup

 

Any inputs from your side Dennins. I even put the exit , but , it seems its not coming out from there.

 

 

Regards

Alok

Matti_Kurkela
Honored Contributor

Re: Shell script for RMAN backup

Does the RMAN log file exist?

 

(according to your script, it should be at /db/epmqa/export/rman/<date>.log)

 

If it exists, what does it say?

 

MK
Dennis Handly
Acclaimed Contributor

Re: Shell script for RMAN backup

>grep -q -e "ORA-" -e "RMAN-" $LOG_FILE ## Please explain this line

 

Do a quiet search for either ORA- or RMAN- in $LOG_FILE and set the exit status.

 

>it's not exiting from rman.

 

You have typed that input into rman manually and it likes it?  It's not waiting for you to type something?

 

But as Matti said, check the logfile.

 

Alok_Behria
Advisor

Re: Shell script for RMAN backup

 

Hi Dennis,

 

It says, backup finished. but, it's not coming out from rman. not sure, why. also, the process still active for some reason.

 

epmqa>  tail -f /db/epmqa/export/rman/091913.log
Starting backup at 19-SEP-13
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 19-SEP-13
channel ORA_DISK_1: finished piece 1 at 19-SEP-13
piece handle=/db/epmqa/export/rman/rman_EPMQA_1_18.rman tag=SPFILE_BKP comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 19-SEP-13

epmqa> ps -ef | grep rman
oraepmqa 23428 23196  0 09:49:38 pts/0     0:00 sh -x rman2.sh

 

Dennis Handly
Acclaimed Contributor

Re: Shell script for RMAN backup

>the process still active for some reason.

 

What are the ancestors of that rman2.sh process?

ps -f -p 23428 -p 23196  # etc

 

If that process was put into the background and wants to write to stdout/stderr, it will hang.

Try redirecting that output:

rman <<EOF > rman.out 2>&1

 

Then cat that file on the end of $LOG_FILE:

...

EOF

cat rman.out >> $LOG_FILE

rm -f rman.out

 

Or just send the whole output from rman to $LOG_FILE and not use "spool log to".