Shell script for rman backup (2253 Views)
Reply
Advisor
Alok_Behria
Posts: 28
Registered: ‎09-14-2011
Message 1 of 10 (2,253 Views)

Shell script for rman backup

[ Edited ]

 

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

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,879
Registered: ‎03-06-2006
Message 2 of 10 (2,242 Views)

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?

 

 

 

 

Please use plain text.
Advisor
Alok_Behria
Posts: 28
Registered: ‎09-14-2011
Message 3 of 10 (2,239 Views)

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..

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,879
Registered: ‎03-06-2006
Message 4 of 10 (2,235 Views)

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.

Please use plain text.
Advisor
Alok_Behria
Posts: 28
Registered: ‎09-14-2011
Message 5 of 10 (2,211 Views)

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

 

Please use plain text.
Advisor
Alok_Behria
Posts: 28
Registered: ‎09-14-2011
Message 6 of 10 (2,201 Views)

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

Please use plain text.
Honored Contributor
Matti_Kurkela
Posts: 6,271
Registered: ‎12-02-2001
Message 7 of 10 (2,198 Views)

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
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,879
Registered: ‎03-06-2006
Message 8 of 10 (2,196 Views)

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.

 

Please use plain text.
Advisor
Alok_Behria
Posts: 28
Registered: ‎09-14-2011
Message 9 of 10 (2,190 Views)

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

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,879
Registered: ‎03-06-2006
Message 10 of 10 (2,187 Views)

Re: Shell script for RMAN backup

[ Edited ]

>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".

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation