9

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

quanta
  • 51,413
  • 19
  • 159
  • 217
tetranz
  • 315
  • 2
  • 6
  • 14

3 Answers3

14

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
larsks
  • 43,623
  • 14
  • 121
  • 180
  • Thanks. I'll give it a go. I assumed I'd get every file in the log because that's what I see on the screen if I try it manually without verbose. The mail thing looks good. – tetranz Jan 06 '11 at 15:23
  • If you're running rsync manually without the `-v` flag and you're still seeing per-file output, there's something wrong. The rsync man page says, "By default, rsync works silently. A single -v will give you information about what files are being transferred and a brief summary at the end." – larsks Jan 06 '11 at 15:32
  • Well that's definitely what's happening. -v seems to be on by default. I just did the log redirection as you suggest and the log has a line for every file. I'm not saying you're wrong. CentOS must have compiled rsync differently. I've added excludes for /proc and /sys. – tetranz Jan 06 '11 at 16:15
  • I'm running CentOS, and rsync behaves as described. What does your command line actually look like? – larsks Jan 06 '11 at 16:26
  • I ran the file manually. It contains: #!/bin/sh rsync -avz --exclude "/proc/" --exclude "/sys/" -e ssh root@example.com:/ /mybackup/ >> /var/log/rsync.log 2>&1 – tetranz Jan 06 '11 at 16:29
  • 1
    See, you're running in verbose mode. You've got a `-v` there. – larsks Jan 06 '11 at 16:31
4

Take out the -v option and add -q. This will only give the errors

Matt
  • 41
  • 1
1

Maybe you could use the syslog via the command: logger.

I run scripts this way:

rsync -v what user@host 2>&1 | logger -t backup

This will write to /var/log/messages by default.

user897079
  • 211
  • 2
  • 3