Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a few linux machines and each of them have a few Oracle databases. On each machine we have 2 shell scripts to run the backups. The TSM scheduler calls a script which calls a different backup script for each DB on the machine.

The problem is if I create an array of SIDs and create a loop through the SIDs the backup fails.

#!/bin/bash

LEVEL="$1"

declare -a SIDS=('SID1' 'SID2' 'SID3')
su - oracle

for SID in ${SIDS[@]}
do
  ORACLE_SID=$SID; export ORACLE_SID
  export ORACLE_HOME='/u01/app/oracle/product/10.2.0.2'
  ORAENV_ASK=NO
  . /usr/local/bin/oraenv
  LD_LIBRARY_PATH=$ORACLE_HOME/lib; export LD_LIBRARY_PATH
  /usr/local/adm/backup-incrn.sh $LEVEL $SID 35
done

There is no error in the Logs on the TSM server and it is almost as if the scripts is never called. The logs that are on the machine are not updated with any error messages.

If I use this syntax the backups work perfectly

su - oracle -c "/usr/local/adm/backup-incrn.sh $LEVEL SID1 35"

su - oracle -c "/usr/local/adm/backup-incrn.sh $LEVEL SID2 35"

I think this has to be an error in my script, some variable not being set properly.

Can anyone see the obvious mistake I've made and point it out to me?

Gísli

share|improve this question

migrated from stackoverflow.com Dec 29 '12 at 19:45

This question came from our site for professional and enthusiast programmers.

    
I would like to know why this was migrated. The problem was that the shell script was not correct, although the shell script was used for database administration. –  Gisli Jan 2 '13 at 16:12

2 Answers 2

up vote 1 down vote accepted
"su - oracle" is the culprit

Remove that line from the script. You can switch the user before you actually run the script. if you switch user in shell script, then it gives you new shell environment loosing further shell instructions. (there are other ways to do it)

share|improve this answer
    
I have to set the user to oracle in the script. I don't have control over anything else than the script. –  Gisli Dec 28 '12 at 22:00
    
then you need to do: "su - oracle -c '<all your shell statements>'" –  Icarus3 Dec 28 '12 at 22:03
    
I think this does the trick. Once I have confirmed that the backups run I will mark this as the answer and post the modified script. –  Gisli Dec 28 '12 at 23:11

I would guess that because you're issuing a login your declaration of SIDS is not transferred to the shell started by the su command. You can test this by checking the value of SIDS after you issue the "su - oracle"

share|improve this answer
    
So if I move the su - oracle to the top, the script would work? –  Gisli Dec 28 '12 at 21:58
    
I think it should work. Try it and see. You don't even need to actually run the backup stuff to examine the environment variables. –  DCookie Dec 28 '12 at 22:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.