Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a BASH script that "reads" the backup logs, reports the success/failure of the last backup, lists the contents of the backup directory then emails the results. After moving the backups to a new server and turning the email routine into functions, it spams me with email. Looks like it's expanding the date and sending an email for each date it expands to. I can't see how for the life of me. Here's some code from "MAIN":

YDATE=$(date -s "yesterday" '+%a%b%d')
BKPLOG="/mnt/nfs/horizon/backuplog_$YDATE.log"

STATUS=$(cat /mnt/nfs/horizon/backuplog_$YDATE.log)

# Catenate logs and status into message body -- email report to 
# Support.
func_make_msg $MSG "$RPT" "$STATUS" "$DB_SERV_BACKUP_LISTING"
func_send $MSG "fake@admin.org" # Send it!

func_make_message just creates the message. Note that neither function are in a for or while loop. Here's the send function:

function func_send ()
{
    local _message=$1
    local _cc=$2
    local _recipient=$3
    $PRINT --columns=2 -t < $_message | $MAIL -s "Backup Logs" "$_cc"
}

My inbox shows an email from each date in the backup logs every time I run it. Thankfully I tarred up the old ones otherwise my inbox would still be going nuts.

Backup server is Debian. Script runs on a cron job:

0 7 * * *      bash /home/bkpadmin/scripts/backup_mail.sh

any ideas? The time was off by a mile, but I eliminated the ntp, cron factor by running it manually. I get the same results even after fixing the time/date and running it manually.

Here's the func_make_msg function for the curious:

function func_make_msg ()
{
    local _msg_body=$1
    local _file=$2
    local _backup_status=$3
    local _db_backups=$4
    cat > $_msg_body << EOM 
## Nightly backup logs ###########################
** Backup Status **
File name: $_file
$_backup_status
---------------

** Important DB Server ****
$_db_backups
---------------
EOM
}

Thanks ~

Bubnoff

UPDATE Thanks Dennis. That was it. A stupid, stupid mistake on my part. The resulting loop-esque results are interesting. I am twice the fool, as I actually read the man page to remind myself how to calculate "yesterday". Swapped 's' for 'd'. 'S' as in "string" I guess. ...For more stupidity ...read on ...

This didn't happen during testing because the script belonged to, and was run in ...a regular user account. The date -s command failed but still output yesterday ...just didn't set the system date ...lacking permissions and all. Points to my second ...even more idiotic mistake ...the cron entry was for root. Another example of why one should never run scripts as root, or, even log in as root unless there is no other way.

The script was able to change the system date date and massively screw up as the variable was repeatedly expanded, thanks to having rootly powers. I am turning myself in to the proper authorities as we speak. Sorry World.

Can anyone see any other stupid mistakes while we're on the subject?

share|improve this question
    
What are $PRINT, $MAIL and func_make_msg? –  Dennis Williamson Oct 1 '10 at 17:19
    
$PRINT = the pr command, $MAIL = the mail command. I usually use the full path for non-builtins in a variable to ensure the cron environment finds them. I will post the make_msg function –  Bubnoff Oct 1 '10 at 17:34

1 Answer 1

up vote 1 down vote accepted

My first recommendation is that you quote $MSG in these lines:

func_make_msg "$MSG" "$RPT" "$STATUS" "$DB_SERV_BACKUP_LISTING"
func_send "$MSG" "fake@admin.org" # Send it!

and $_message in this line

$PRINT --columns=2 -t < "$_message" | $MAIL -s "Backup Logs" "$_cc"

But I presume those are filenames and that they're not the source of your problem.

You can eliminate cat in this line:

STATUS=$(< /mnt/nfs/horizon/backuplog_$YDATE.log)

But, again, it's not your real problem.

Edit:

I think I found your problem: date -s sets the date and time. Try removing the -s.

share|improve this answer
    
They are file names. I don't usually quotes file names. I supposed for the BKPRPT variable I could quote like so: ${YDATE}, but I don't know if this is the issue either. I'm stumped ...completely. –  Bubnoff Oct 1 '10 at 17:37
    
@Bubnoff: %a%b%d (on my system, today, it's "FriOct01") is pretty unusual for dating filenames. You should check your locale and make sure it's outputting what you expect (compare cron to command-line). –  Dennis Williamson Oct 1 '10 at 17:42
    
It corresponds to the way another sysadmin setup the backup dates in the actual backup/rotation scripts. My script just verifies that the backups are taking place. Cron is getting the same result because the resulting emails are in the same format. –  Bubnoff Oct 1 '10 at 17:46
    
@Bubnoff: date -s sets the date and time. Try removing the -s. –  Dennis Williamson Oct 1 '10 at 17:53
    
That's it! I used the s instead of the d option. I need to calculate yesterday. That should be date -d "yesterday" '+%a%b%d' ...correct? I was setting the date instead. Funny thing is, I actually looked at the man page before setting this up. –  Bubnoff Oct 1 '10 at 18:17

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.