1

I have the following one-line cron command set up which gives me a nice simple message in the body of the email upon backup complete (thanks to @steeldriver).

tar -cpzf /home/user/backups/backup_files.tar.gz /home/user/public_html;
case $? in 0) echo "Files successfully backed up at $(date)" ;; *) echo "Backup failed" ;; esac

The issue is that the whole command line is output into the subject line of my email. This is not ideal as it gives the entire path to my backups across email. I'd prefer the subject to just say something simple like, "Backup user"

Is there a way to do this without upsetting the existing command? Thanks.

2

You don't need to use the default cron mail subsystem; you can declare your own. Create a single one-line cron entry containing this (which I've split over three lines for increased readability).

if tar czf /home/user/backups/backup_files.tar.gz -C / home/user/public_html;
then echo "Files successfully backed up at $(date)";
else echo 'Backup failed'; fi | mail -s 'Backup user' root

Some notes

  • I've removed the -p flag from tar since it's ignored when you're create an archive
  • I've added the -C flag to change directory so that the backup source is a relative file (which in turn stops the tar: Removing leading `/' from member names warning message)
  • I've provided root to take your user account's local email, but if your system is set up to handle network email you could replace that with any reachable address
  • Personally, I'd put it in a script and call the script from cron. This allows your script to be laid out more readably, and you can add necessary complexity (sophistication!) as required without hitting cron line limits and other surprises.
  • Yes, once the cron line gets too large, I always put it into a script. This is definitely over that limit :-) – Stephen Harris Mar 29 at 0:48
  • Thanks roaima and @stephen-harris. I actually did start off with a PHP script but the backed up file is heavy, just under 4gb. The script failed, never completed the task. Whereas it works fine on every other site I backup. Hence why I moved to a cron comand-line solution. I'll try the suggested single cron line tonight and report back. ;) – James Mar 29 at 1:26
  • Okay as a follow up, this didn't work as expected. It did the backup, but no email was received, not in Spam folder either. Any ideas? Perhaps the command is just too long now... – James Mar 30 at 6:05
  • Please replace $USER (root in my current edit) with your local username and see if that delivers. – roaima Mar 30 at 8:27
  • Thanks, Ill try this tonight. Could I also use any email instead of "root"? – James Apr 2 at 7:49

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.