3

I have (from one tutorial) cron job which make weekly backup on my website and is working fine.

#!/bin/bash
NOW=$(date +"%Y-%m-%d-%H%M")
FILE="mysite.com.$NOW.tar"
BACKUP_DIR="/home/user/backups/"
WWW_DIR="/home/user/public_html/"

DB_USER="my_site_db_username"
DB_PASS="password"
DB_NAME="mysite_dn_name"
DB_FILE="mysite.com.$NOW.sql"

WWW_TRANSFORM='s,^home/username/public_html,www,'
DB_TRANSFORM='s,^home/username/backups,database,'

tar -cvf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR
mysqldump -u$DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/$DB_FILE

tar --append --file=$BACKUP_DIR/$FILE --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE
rm $BACKUP_DIR/$DB_FILE
gzip -9 $BACKUP_DIR/$FILE

Question is, can someone help me how can I made when backup is done to send me email?

I'm using bash for first time and I'm not sure what I doing.

2 Answers 2

6

Edit your crontab file and add:

MAILTO=your.email@your_provider.com

and at the end of the script add:

echo 'backup finished', $FILE

cron normally sends any output from the command it runs per email. Your script seems to be running silently, hence no email.

If you don't add the MAILTO, the mail will go to the user running the crontab, IMHO it is better to make that explicit.

6
  • I added those 2 lines at the end of my script but didn't receive email. Jun 26, 2013 at 12:39
  • Have you set your MAILTO in the crontab correctly and have you tested by hand that the webite's server actually can send email ( ` echo 'test' | mail [email protected]`)?
    – Anthon
    Jun 26, 2013 at 12:41
  • Actually was my mistake. I forgot to set my email in crontab. After I set proper emial now is received. But any chance to customize a little text body in email because now is 500+ rows?... I just want to write "Backup for website-date is done". Jun 26, 2013 at 12:50
  • Don't use the verbose flag (tar -v), or else redirect the stdout from your commands. Then you'll be left with simply what you put to stdout specifically, e.g. "Backup done"
    – dougBTV
    Jun 26, 2013 at 12:56
  • U mean here tar -cvf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR to use only tar -cf...? Jun 26, 2013 at 12:58
4

You can add the following line at the end of your bash script:

echo -e "Sending Backup report :\n Backup of $DB_NAME and $FILE completed " | mutt -s "Backup report for $NOW"

2
  • This is working also fine. Thank for your help too. I don't have 15 reputation to vote up. Sorry. Jun 26, 2013 at 13:03
  • Now, you do :-)
    – q9f
    Jun 30, 2013 at 20:20

You must log in to answer this question.

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