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.

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.

  • I added those 2 lines at the end of my script but didn't receive email. – Jason Paddle Jun 26 '13 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 your@email.adr`)? – Anthon Jun 26 '13 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". – Jason Paddle Jun 26 '13 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 '13 at 12:56
  • U mean here tar -cvf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR to use only tar -cf...? – Jason Paddle Jun 26 '13 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"

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.