[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 535
  • Last Modified:

backup script with email notification

I want to have a backup script that would backup using the rsync and run everyday. when the script is done I would like it to send me a notification email with result. for example if I put

rsync -av /Data/ /backup/

I would like to see the result of that as if I am running it from the shell. Please provide the script if possible. I have latest kernal. Thank you
0
fm250
Asked:
fm250
  • 4
  • 3
1 Solution
 
m1tk4Commented:
rsync -av /Data/ /backup/ | mail myemailaddress@somwehere.com

rsync -av /Data /backup/ 2>&1  | mail myemailaddress@somwehere.com

if you want to send the error messages (stderr) too.

See

man mail

for more switches (subject line, etc.)
0
 
fm250Author Commented:
So
>> rsync -av /Data/ /backup/ | mail myemailaddress@somwehere.com
will this one wait for all of output of the command
rsync -av /Data/ /backup/
and then send it to email.  what if you want more nice email for example
subject: backup
body: result of the backup sync    -----

result here
--end---
also what does this one do.
rsync -av /Data /backup/ 2>&1  | mail myemailaddress@somwehere.com

and finally this should be in a file whatevername.sh and put in the /etc/cron.daily/
correct?
0
 
m1tk4Commented:
>>So
>> rsync -av /Data/ /backup/ | mail myemailaddress@somwehere.com
>>will this one wait for all of output of the command
>>rsync -av /Data/ /backup/
>>and then send it to email.

Yes.

>>what if you want more nice email for example
>>subject: backup
>>body: result of the backup sync    -----

You do it this way:

echo "result of the backup sync" > a.tmp; echo " " >> a.tmp; echo "----" >> a.tmp; rsync -av /Data /backup/ 2>&1 >>a.tmp; mail myemailaddress@somwehre.com -s "backup" <a.tmp

>>and finally this should be in a file whatevername.sh and put in the /etc/cron.daily/
>>correct?

Yes, pretty much. Don't forget a shebang (#!/bin/bash) in the first line and to make the file executable (chmod a+x whatevername.sh).

Also, if you are executing it from crond, you have to use full versions of some commands as path is not always available, i.e.

/bin/mail instead of mail and /usr/bin/rsync instead of rsync.


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
fm250Author Commented:
will
>> a.tmp

append to the file or replaced it?
0
 
m1tk4Commented:
>> appends

> replaces and starts anew.
0
 
fm250Author Commented:
What does 2>&1 means?  in  rsync -av /Data /backup/ 2>&1 >>a.tmp;
also how do I add ther errors?

Thanks!
0
 
m1tk4Commented:
2>&1 means "redirect the stream #2 (stderr) to stream #1(stdout)", i.e. append all error messages to your standard output.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now