How can I check whether rsync transfered files or not?

2

I use rsync (cygwin) to transfer files between server and my PC.

Rsync runs every 2 minutes, and sometimes there are some files to transfer, sometimes not. I need to execute some actions, but only if files were transferred from server to PC.

How do I know, whether files were transferred or not?

P.S. I tried to use rsync's exit codes, but it returns 0 in both when files are transfered and when they don't.

temur_p

Posted 2016-07-01T06:51:21.567

Reputation: 41

Answers

5

rsync -rtv <source> <dest> | wc -l

If the result is more than 4 something changed

matzeri

Posted 2016-07-01T06:51:21.567

Reputation: 1 662

Thank you, that works great :) Can you explain what number 4 means? – temur_p – 2016-07-01T10:51:07.297

wc -l counts the number of lines . A clean run with option -v have 4 lines in the output. Any change has more than 4 – matzeri – 2016-07-01T12:17:11.850

3

I use the option

 rsync ....  --log-file=/path/to/log/file

This adds to the log-file, so you can find out whether anything at all has been transferred, and, if so, what exactly, even much later than when the backup took place.

MariusMatutiae

Posted 2016-07-01T06:51:21.567

Reputation: 41 321

yep, i already use --log-file and what if i need to pass file names to the script? Guess i have to use awk or sed to extract them from log file and that seems a bit tricky to me ) – temur_p – 2016-07-01T11:02:43.340

1@temur_p Awk and sed tricky? Lol. – MariusMatutiae – 2016-07-01T11:33:36.617

they are for me. Besides, it was preferable to solve this using just rsync's options. – temur_p – 2016-07-01T11:47:44.403

1@temur_p Just for the record: --log-file is an option of rsync's. – MariusMatutiae – 2016-07-01T11:50:45.887

I was talking about using awk or sed. It seems like you didnt understand the problem - if i'm gonna use --log-file, then i'll have to extract file names from it by using awk or sed. And by using solution, proposed in first message, i can simply handle the problem by adding if condition. – temur_p – 2016-07-01T12:03:52.923

1

rsync --stats

You can use the option --stats or --info=stats2 eventually filtering with a grep.

  rsync -avr ORIG DEST  --stats | grep "Number of created files"

Number of created files: 14


From the manual of rsync you can read

--stats
This tells rsync to print a verbose set of statistics on the file transfer, allowing you to tell how effective rsync’s delta-transfer algorithm is for your data. This option is equivalent to --info=stats2 if combined with 0 or 1 -v options, or --info=stats3 if combined with 2 or more -v options.

Further notes:
I suggest you to use a logfile as proposed by MariusMatutiae in case you will have the curiosity to see which files were transferred.
For the grep filter you can choose each line that you find interesting:

  • Example of output with some file transferred:

    Number of files: 15 (reg: 14, dir: 1)
    Number of created files: 14 (reg: 14)
    Number of deleted files: 0
    Number of regular files transferred: 14
    Total file size: 280,860 bytes
    Total transferred file size: 280,860 bytes
    Literal data: 280,860 bytes
    Matched data: 0 bytes
    File list size: 0
    File list generation time: 0.001 seconds
    File list transfer time: 0.000 seconds
    Total bytes sent: 281,842
    Total bytes received: 285

  • The same command with no file transferred:

    Number of files: 15 (reg: 14, dir: 1)
    Number of created files: 0
    Number of deleted files: 0
    Number of regular files transferred: 0
    Total file size: 280,860 bytes
    Total transferred file size: 0 bytes
    Literal data: 0 bytes
    Matched data: 0 bytes
    File list size: 0
    File list generation time: 0.001 seconds
    File list transfer time: 0.000 seconds
    Total bytes sent: 357
    Total bytes received: 12

Hastur

Posted 2016-07-01T06:51:21.567

Reputation: 15 043