15

Can Rsync be configured to verify the contents of the file before they are being synced. I have heard about checksum, but I came to know that checksum only does a sampling. I want to transfer a file only if it is contents are changed and not timestamp, is there a way to do it with any of the rsync modes. In my scenario, say file sample.text will be created every week and I want to sync it with a remote server only if the contents of sample.text are changed, since it is created every week, the time stamp would obviously change. But I want the transfer only on a content change.

1
  • Have you tried the man page for rsync? -c, --checksum This changes the way rsync checks if the files have been changed and are in need of a transfer. Without this option, rsync uses a "quick check" that (by default) checks if each file's size and time of last modification match between the sender and receiver. Oct 12, 2011 at 19:27

3 Answers 3

35

Yes:

$ man rsync | grep "\--checksum"
    -c, --checksum              skip based on checksum, not mod-time & size

Rsync is pretty complicated. I recommend cuddle time with the man page and experimentation with test data before using it for anything remotely important.

Most of the time, people use rsync -ac source dest.

$ man rsync | grep "\--archive"
 -a, --archive               archive mode; same as -rlptgoD (no -H)

And that -rlptgoD garbage means: recursive (r), copy symlinks as symlinks (l), preserve permissions (p), preserve times (t), preserve group (g), preserve owner (o), preserve device files (D, super-user only), preserve special files (also part of D).

The -c or --checksum is really what you are looking for (skip based on checksum, not mod-time & size). Your supposition that rsync only samples mtime and size is wrong.

1
  • 8
    + for cuddle time with the man page and experimentation with test data - Extremely good advice Dec 18, 2014 at 21:56
9

See the --checksum option on the rsync man page.

Also, the --size-only option will be a faster choice if you know for sure that a change of contents also means a change of size.

0

Example:

#!/bin/bash
echo > diff.txt
rsync -rvcn --delete /source/ /destination/ > diff.txt
second_row=$(sed -n '2p' diff.txt)
if [ "$second_row" = "" ]; then
    echo there was no change
else 
    echo there was change   
fi
2
  • 3
    You should really add some explanation as to why this code should work - you can also add comments in the code itself - in its current form, it does not provide any explanation which can help the rest of the community to understand what you did to solve/answer the question. Jul 28, 2016 at 20:45
  • If there was no change the second row of diff.txt in verbose mode of rsync is empty, sed examine this second line.
    – oldbowman
    Jul 29, 2016 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.