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 "[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
$_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?
My first recommendation is that you quote
$MSG
in these lines:and
$_message
in this lineBut I presume those are filenames and that they're not the source of your problem.
You can eliminate
cat
in this line: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
.