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')

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

# Catenate logs and status into message body -- email report to 
# Support.
func_send $MSG "[email protected]" # 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

** Important DB Server ****

Thanks ~


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?

  • What are $PRINT, $MAIL and func_make_msg? Oct 1, 2010 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, 2010 at 17:34

1 Answer 1


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

func_make_msg "$MSG" "$RPT" "$STATUS" "$DB_SERV_BACKUP_LISTING"
func_send "$MSG" "[email protected]" # 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.


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

  • 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, 2010 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). Oct 1, 2010 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, 2010 at 17:46
  • @Bubnoff: date -s sets the date and time. Try removing the -s. Oct 1, 2010 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, 2010 at 18:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .