0

I have setup a cron job from cpanel to email me daily so I know the site files have been backed up. However, it emails the entire file paths and the whole job. Snippet of the email output I receive;

tar: Removing leading `/' from member names
/home/user/public_html/
/home/user/public_html/test/
/home/user/public_html/test/index.html

I really just want it to email a simple message when done, like this; Files successfully backed up at 02:00

Current Cron Job: tar -cvpzf /home/user/backups/backup_files.tar.gz /home/user/public_html

NOTES: This cron job works just fine and does the backup. I just don't want the whole job with all the file paths emailed to me (but do want a basic notification).

For my other sites I use a PHP backup script, but this doesn't work on this site as the backup file is simply too large I think for PHP to handle. The end tar.gz file is just under 4GB.

Any guidance appreciated.

2

You can remove the -v (verbose) option, and instead use the tar command's exit status to determine what message to send.

Ex. at its simplest,

tar -cpzf /home/user/backups/backup_files.tar.gz /home/user/public_html && echo "Files successfully backed up at $(date)"

or (slightly more nuanced)

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

See man tar for the meanings of various non-zero exit status values.

  • Thanks @steeldriver - I like the look of the nuanced version. But not sure about the "esac" at the end. Assuming the line ends at ";;". I'll use the simple version until I can confirm this. Obviously not a cron pro here, so baby steps for me ;) – James Mar 22 at 1:10
  • @James you need the esac to close the case: see for example What is the reason for the weird syntax of the “case” statement in a bash/zsh script? (applies to the POSIX /bin/sh shell as well - which is more likely what cron is using) – steeldriver Mar 22 at 1:13
  • Of course, makes perfect sense now. I'll try it tonight. Thanks. – James Mar 22 at 1:18
  • Thanks @steeldriver this works like a charm. Next issue is to have the subject line of the email output something really basic like "username backup" without upsetting the command above. Currently the subject line uses the entire command line. Any ideas? – James Mar 25 at 0:22
0

I'd suggest have the process which generates your notification be piped through

tail -n 

to parse out the last n lines, then send just that to your email.

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.