Find the answer to your Linux question:
Results 1 to 9 of 9
hello all, Currently I have a backup script which every night will write to a tape, and store the tape output to a log file. In the same script it ...
  1. #1
    Just Joined!
    Join Date
    Jan 2007
    Posts
    38

    Exclamation backup script

    hello all,

    Currently I have a backup script which every night will write to a tape, and store the tape output to a log file.

    In the same script it will then email out the log file to email address i specify.

    However, also have (If any errors are encountered) that it writes these errors to a 'error log' file.

    However, the problem I have is that when the backup fails - i.e no tape, it just emails a blank email (Which would of been the log file), is there anyway I can email error file (in the same email) or even better would be that if the backup failed, instead of using the log file it emails the error file.

    Please see below for my current syntax (minus the variables):

    rm /utils/tar.all.log
    rm /utils/tar.all.error.log

    cd $STARTDIR

    tar cvf $TAPEDEVICE $DATESTAMP * 2>$TAPEERRORLOGFILE > $TAPELOGFILE

    echo "Backup of X Server $STARTDIR finished" $NOW >> $DATESTAMP
    tar tvf $TAPEDEVICE |mail -s "System backup of X Server $(date)" -c $C
    USTOMEREMAIL $CUSTOMEREMAIL2 $MAIL
    tar tvf $TAPEDEVICE >> $TAPELOGFILE

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Check the return status after tar runs. If it's zero, the backup worked. If it's non-zero, then it (probably) failed.

    Code:
    tar cvf $TAPEDEVICE $DATESTAMP * 2>$TAPEERRORLOGFILE > $TAPELOGFILE
    if [ $? = 0 ]
       then mail_log_file
       else mail_error_file
       fi

  3. #3
    Just Joined!
    Join Date
    Jan 2007
    Posts
    38
    Quote Originally Posted by vsemaska View Post
    Check the return status after tar runs. If it's zero, the backup worked. If it's non-zero, then it (probably) failed.

    Code:
    tar cvf $TAPEDEVICE $DATESTAMP * 2>$TAPEERRORLOGFILE > $TAPELOGFILE
    if [ $? = 0 ]
       then mail_log_file
       else mail_error_file
       fi
    Thanks for your help.
    This is how it looks at the moment, but i havent ran it yet as I am working remotely.

    rm /utils/tar.all.log
    rm /utils/tar.all.error.log

    cd $STARTDIR

    tar cvf $TAPEDEVICE $DATESTAMP * 2>$TAPEERRORLOGFILE > $TAPELOGFILE

    if[$? =0]

    mail -s "System backup of $CUSTOMER completed $(date)" -c $CUSTOMEREMAIL1 $CUSTOMEREMAIL2 $EMAIL >$TAPELOGFILE

    else

    mail -s "System backup of $CUSTOMER completed $(date)" -c $CUSTOMEREMAIL1 $CUSTOMEREMAIL2 $EMAIL > $TAPEERRORLOGFILE

    fi

  4. #4
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    bash is a bit touchy when it comes to spaces so:

    Code:
    if[$? =0]
    won't work. This will:

    Code:
    if [ $? = 0 ]
    You also forgot the 'then' before the 1st mail.

  5. #5
    Just Joined!
    Join Date
    Jan 2007
    Posts
    38
    Quote Originally Posted by vsemaska View Post
    bash is a bit touchy when it comes to spaces so:

    Code:
    if[$? =0]
    won't work. This will:

    Code:
    if [ $? = 0 ]
    You also forgot the 'then' before the 1st mail.
    Thanks, I noticed that after uploading it onto the forums!

    rm /utils/tar.all.log
    rm /utils/tar.all.error.log

    cd $STARTDIR

    tar cvf $TAPEDEVICE $DATESTAMP * 2>$TAPEERRORLOGFILE > $TAPELOGFILE

    if [ $? = 0 ]
    then

    mail -s "System backup of $CUSTOMER completed $(date)" -c $CUSTOMEREMAIL1 $CUSTOMEREMAIL2 $VALEEMAIL >$TAPELOGFILE

    else

    mail -s "System backup of $CUSTOMER Failed $(date)" -c $CUSTOMEREMAIL1 $CUSTOMEREMAIL2 $VALEEMAIL > $TAPEERRORLOGFILE

    fi

    This is how it looks now, but when I run it I get a problem with the mail command saying it is expecting:

    mail: option requires argument --c

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    One thing I see is your directing output from mail to the log files, not specifying the files as input. The '>' when it should be '<'.

    Code:
    if [ $? = 0 ]
    then
    
    mail -s "System backup of $CUSTOMER completed $(date)" -c $CUSTOMEREMAIL1 $CUSTOMEREMAIL2 $VALEEMAIL < $TAPELOGFILE
    
    else
    
    mail -s "System backup of $CUSTOMER Failed $(date)" -c $CUSTOMEREMAIL1 $CUSTOMEREMAIL2 $VALEEMAIL < $TAPEERRORLOGFILE
    
    fi

  7. #7
    Just Joined!
    Join Date
    Jan 2007
    Posts
    38
    Quote Originally Posted by vsemaska View Post
    One thing I see is your directing output from mail to the log files, not specifying the files as input. The '>' when it should be '<'.

    Code:
    if [ $? = 0 ]
    then
    
    mail -s "System backup of $CUSTOMER completed $(date)" -c $CUSTOMEREMAIL1 $CUSTOMEREMAIL2 $VALEEMAIL < $TAPELOGFILE
    
    else
    
    mail -s "System backup of $CUSTOMER Failed $(date)" -c $CUSTOMEREMAIL1 $CUSTOMEREMAIL2 $VALEEMAIL < $TAPEERRORLOGFILE
    
    fi
    thanks for pointing that out, but it still does the same error:

    [root@centos utils]# ./diskspacecheck
    mail: option requires an argument -- c
    Usage: mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...
    [- sendmail-options ...]
    mail [-iInNv] -f [name]
    mail [-iInNv] [-u user]
    [root@centos utils]#

  8. #8
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    OK, didn't look too closely at the error message. Sounds like variables CUSTOMEREMAIL1, CUSTOMEREMAIL2, VALEEMAIL, aren't defined. You can see what the actual mail command looks like by running the script like this:

    Code:
    sh -vx ./diskspacecheck

  9. #9
    Just Joined!
    Join Date
    Jan 2007
    Posts
    38
    Thanks that worked brilliantly

    You were correct, it wasn't setting the variable for one of the email addresses. Doh!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...