- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- rsync script to send email after completion or fai...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2015 03:02 AM
11-09-2015 03:02 AM
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,
Syed
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2015 07:10 AM
11-09-2015 07:10 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2015 11:00 AM
11-09-2015 11:00 AM
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 ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2015 11:38 PM
11-10-2015 11:38 PM
SolutionThank 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,
Syed