Now that I have Sendmail Started (Part I) and I can successfully send mail to my email address on the local network (Part II), I need to throw information in the email regarding my daily backup. I could use something like Mutt to email the whole backup file to myself, but the logs are 45MB, and that would add up over time, especially since I only need to see a few lines of the log file.

I first create a file called emailbkuplog.sh in /usr/local/sbin which will be the backup script. Then i change the permissions on this to 755 so it can properly be run. then i write the script:
#!/bin/sh
#Program to email backup logs to myself and cc them to whomever
ADDR1=mymailaddress@myemaildomain.com
ADDR2=myccmailaddress@myemailaddress.com
LOGDIR=/home/backup/logs
CURDIR=`pwd`
cd $LOGDIR
#grab last log
LASTLOG=`ls -t *log|head -1`
BUSTATUS=`grep Status= $LASTLOG`

echo `tail -6 LAST_Backup` | mail -s $BUSTATUS -c $ADDR2 $ADDR1
cd $CURDIR

I’ll explain the script above briefly. First i set up some variables to hold my email addresses, location of my backup log, and the current working directory so i can change back to it after the script is done. I then change the current directory to where my logs are. My logs are formatted like so when they are created:
04102010.log,
04112010.log, etc.
so i need a way to figure out which was the last log that was created. I do this by performing an ‘ls -t *.log’ which orders the directory by time, and I grab the first entry (most recent log) by piping it to ‘head -1’ Now that i have the name of the last log, i grab the line in it which gives me the backup status – this will be the subject of the email.

Now to create the email. The format and options that i’m using is like so:

mail -s “Hello” -c

The above alone will send an email with the subject “Hello” to the email address you specify, and CC it

Now I want to put some details into the body. You do this by piping information into the mail command like so.

echo “This is the message body” | mail -s “Hello” -c

My detail come by echoing the results of another file called LAST_Backup which contains more detailed backup information in the last 6 lines of the file, thus here’s my final command to send backup information as the subject, and more detailed info as the message body:

echo `tail -6 LAST_Backup` | mail -s $BUSTATUS -c $ADDR2 $ADDR1

Save the file, and then try to run it by typing it’s name in at a shell prompt.

if successful you will have an email in your inbox with the information you want. if not, you may want to check the mail log files in /var to see if there’s anything that indicates an error.

The last step is to place an entry in your cron file to get it to run on a regular basis. To bring up cron, enter the command “crontab -e” For my purposes, I need this to happen 6 days a week (we’re closed on sunday) Tuesday – Sunday at 7:00 AM. Here’s my entry to do that.

0 7 * * 2-7 /usr/local/sbin/emailbkuplog.sh > /dev/null 2>&1

If you want to understand that > /dev/null 2>&1, here’s a great article that explains what it means here

The above, emails me the status as the subject, and then fills me in on the details in the message body. It works great, and is an easy check on my phone when i’m away from the office as i don’t have ssh to the box, or open a 45mb attachment on my phone.

A few notes about this:

If you wanted to attach the file, there are several programs that will do this for you, just google them, but i don’t think the mail command alone will do this. One of them is using the mutt command, and another is uuencode. There’s plenty of information out there on this. Just google it.

I’ve found that the “echo” command doesn’t hold the format of the file that it echo’s, so if its 3 lines like
line 1
line 2
line 3
echo will output it like “line 1 line 2 line 3”
An alternate to this type of echo format is would be to format it with the “printf” command or so i have read, but it’s beyond what i’m willing to do right now. I wanted something quick and dirty and this did the job.

Hope this helps someone else. Ciao.

Advertisements