Operating System - Linux
1752778 Members
6145 Online
108789 Solutions
New Discussion

Re: rsync script to send email after completion or failure

 
SOLVED
Go to solution
Syedfurquan
Trusted Contributor

rsync script to send email after completion or failure

Hello,

 

I have written a script for rsync from my production RHEL server to DR RHEL server.

my rsync works fine and has been scheduled in crontab. Now I want to add email notification for each success and failed rsync but I am not able to get any email. email works fine from the server.

can anyone please check & let me know where I am doing mistake in my script:

#!/bin/sh
#
# This script calls rsync to copy respective directories
#
#
#!/bin/bash

/usr/bin/rsync -azrv --delete -e "ssh -i /root/rsync/servername-rsync-key" /directory/ root@xx.xx.xxx.xx:/copy-locations &

# send an email if backup failed

if [ $? -ne 1 ]

then

sendmail -v "rsync backup completed successfully" salam@xxx.com

rsync backup completed successfully @ $(date) for $(hostname)

else
sendmail -v salam@xxxx.xom

"rsync failed" @ $(date) for $(hostname)

fi

 

Any help where I am missing in the script. simple rsync sript work fine without any email options.

 

Regards,

Regards,
Syed
3 REPLIES 3
Patrick Wallek
Honored Contributor

Re: rsync script to send email after completion or failure

You're putting your rsync into the background with the '&' at the end of it.

As a result of that you're not getting a true status from the rsync as it hasn't finished yet when you are checking the return code.

 

Remove the '&' from the end of your rsync command and try again.

Steven Schweda
Honored Contributor

Re: rsync script to send email after completion or failure

> [...] but I am not able to get any email. [...]

> sendmail -v "rsync backup completed successfully" salam@xxx.com
> rsync backup completed successfully @ $(date) for $(hostname)

   I know nothing, but I don't see how that could work.  Have you
written _any_ script which _can_ send an e-mail message?  I'd start with
that, and _then_ try to add e-mail to your rsync script.

   A Forum search for keywords like, say:
      e-mail script
should find many examples.  Many of them use mailx rather than sendmail.
In any case, I'd expect most programs of that sort to take their message
input from stdin, and simply putting a line of text onto your script is
not a valid way to make that happen.  You could use a here-document, or
a pipeline construct like, say:
      echo "Your message here." | mailx [...]


> if [ $? -ne 1 ]


   And if the program can return more values than 0 or 1?  A better test
might be something more like, say:

      if [ $? -eq 0 ]

Syedfurquan
Trusted Contributor
Solution

Re: rsync script to send email after completion or failure

Thank you Patrick & Steven for the help.

Now my scripts working fine and able to get email notification for both success and failed rsync. Now my scripts look like this

#!/bin/bash
/usr/bin/rsync -azrv --delete -e "ssh -i /root/rsync/xxx-rsync-key" /apps root@xx.xx.xxx.xx:/apps

if [ $? -eq 0 ]
then
/usr/lib/sendmail -v "rsync backup completed successfully" salam@xxxx.comt<<EOF
rsync backup completed successfully @ $(date) for $(hostname)
EOF
else
/usr/lib/sendmail -v salam@xxx.com<<EOF
"rsync failed" @ $(date) for $(hostname)
EOF
fi

 

Regards,

Regards,
Syed