Send backup by email with crontab

backupcronemail

I use this cron job to do a backup of /home/blah/ each day at 01:00 a.m. :

0 1 * * * tar -zcf /home/blah/backup.tgz /home/blah/

In fact I would prefer that an email is sent to me with the .tgz file as attachment. (yes, the filesize will always be < 5 MB because my folder is very small)

Can I do something like :

0 1 * * * mail -s "Backup blah" "blah@blah.com" --attachment=(tar -zcf /home/blah/backup.tgz /home/blah/)

(this is pseudo-code at the end) inside the cron job ? What cron syntax should I use?

Best Answer

This following command worked for me when I tested in my machine.

echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient@domain.com

So probably the approach to follow will be something like,

tar -zcf /home/blah/backup.tgz /home/blah/
echo "Please find attached the backup file" | mutt -a "/home/blah/backup.tgz" -s "File attached" -- recipient@domain.com

I will save the above script as backup_email.sh and schedule the cron job as,

0 1 * * * /path/to/backup_email.sh

References

https://stackoverflow.com/a/9524359/1742825

Related Question