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?


2 Answers 2


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



  • Thanks @Ramesh! Unfortunately, I don't have mutt on the server and I cannot install it (shared hosting). Would you use stackoverflow.com/a/17422/1422096 then or another one ? If possible, I'd like to use a one-line solution, so I could avoid creating a .sh script.
    – Basj
    Nov 10, 2014 at 7:50
  • @Basj, the above link suggested answer works for me. But am not sure if piping will be a problem in cron if you want it as a single command.
    – Ramesh
    Nov 10, 2014 at 14:29
  • uuencode is an older alternative to the (nowadays) more usual MIME attachments. It's a different process on the receiving end, but it may be the only thing you're able to do, though. Another alternative would be doing it as an scp to a central server that you control which itself has a cronjob that mails it off as a MIME attachment.
    – Bratchley
    Nov 10, 2014 at 15:03
  • Ok @JoelDavis. What would you use as a one-liner code for sending an email with the .tgz file as attachment?
    – Basj
    Nov 10, 2014 at 15:18
  • That's basically what I'm saying. That uuencode option may be the only thing you have available to you. I was just pointing out that it doesn't send it as a MIME attachment so you were prepared for that. Another possibility is to just ask the hosting company to install mutt for you. That's kind of an innocuous request that they may fulfill.
    – Bratchley
    Nov 10, 2014 at 16:03

I would install ZIP first, then schedule a backup.

zip system.zip /etc/* /var/www/* /var/lib/mysqlbackup/default/* && sleep 3 |  mutt -a "system.zip" -s "Daily backup as of $(date +%x)" -- user@domain.com && rm -rf system.zip

Then crontab it

You can also alias it in bashrc file, so you can execute the whole command whenever you like.

At the very bottom of your bashrc file, append:

alias backup='zip system.zip /etc/* /var/www/* /var/lib/mysqlbackup/default/* && sleep 3 |  mutt -a "system.zip" -s "Daily backup as of $(date +%x)" -- laith.alobaidy@yahoo.com && rm -rf system.zip '

So your crontab would be like

0 0 * * * backup

And you can run this backup at any time with your own backup command.

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.