Rsync via cron. How to enable logging

rsync

I'm backing up a remote server to another computer using rsync.

In cron.daily I have a file with this:

rsync -avz -e ssh root@example.com:/ /mybackup/

It uses a public / private key pair to login. This seems to work well most of the time however, I've (foolishly) only ever really checked it by looking at the dates on some important files (MySQL dumps) that I know change every day. Obviously, an error could occur after that file.

Sometimes it fails. When I run it manually, something like "client reset" sometimes happens.

What is the best way to log it so that I can check with certainty if it completed or not? The cron log doesn't indicate any errors. I haven't tried it but the rsync man page on the oldish version of CentOS on the backup machine doesn't show the –log-file option. I guess I could redirect stdout with > but I don't really want to know about every file. I just want to know if it all worked or not..

Thanks

Best Answer

I think you've already solved your own question. If you redirect both stdout and stderr to a file, you won't get output for every file rsync transfers -- this is only produced if you're running in verbose (-v) mode. The default behavior of rsync is to only produce output in the event of an error. So you can do this...

rsync ... > /var/log/rsync.log 2>&1

...and inspect that file to see if the most recent rsync was successful or not. I'm explicitly using >, which will overwrite the log file each time rsync runs.

You could also take advantage of the fact that rsync exits with a non-zero code when the transfer fails, so you could so something like this:

rsync ... || echo "rsync failed" | mail -s 'rsync falied' you@example.com