Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does rsync ignore file timestamps and automatically overwrite on client if file is different on server?

I'm trying to set up two PCs to sync a folder tree so that each PC will have a copy of the tree with the most recent updates to each file.

I considered setting up Mercurial but realized I don't really care about versioning (especially since I'm low on disk space), and that rsync sounds like it does more of what I want - just keeping files up to date, no versions.

However, the page at http://www.linuxjournal.com/content/synchronizing-your-life says the following:

With rsync, any files that already exist at the destination will not be transferred. This speeds up the transfer time considerably. However, there is still the problem of having modifications made on both sides. By default, the rsync program only looks to see if the files are different in size and timestamp. It doesn't care which file is newer, if it is different, it gets overwritten.

You can pass the '--update' flag to rsync which will cause it to skip files on the destination if they are newer than the file on the source, but only so long as they are the same type of file. What this means is that if, for example, the source file is a regular file and the destination is a symlink, the destination file will be overwritten, regardless of timestamp. Even looking past its quirks, the --update flag does not solve the problem because all it does is skip files on the destination if they are newer, it doesn't pull those changes down to the source computer.

Is this correct?

If so, I guess it makes rsync really only useful for backing up one master ("source") machine onto one or more slaves that will get the changes from the master regardless of timestamps. Whereas the problem I'm really trying to solve is having two machines be "peers" and equally just get the most recently updated files from the other.

Or do you think I'll just have to bite the bullet and use git or Mercurial despite the extra disk space for tracking versions?

(Yes I know about Dropbox; I'm well above the 2GB free account limit and not really interested in spending $120-$240 a year when I don't need the cloud storage and something this simple has to have been done before with free & open tools.)

The PCs are both running XP but I was going to use Cygwin's rsync and any other Unixy tools necessary to get the job done.

like image 766
Chirael Avatar asked Sep 04 '10 06:09

Chirael


People also ask

Does rsync overwrite existing files?

Any files that already exist on the backup drive are skipped. If you want you can force rsync to overwrite files that already exist at the destination using the -I ( --ignore-times ) option.

Does rsync automatically skip existing files?

Note: This does not ignore existing directories, or nothing would get done. Even if there are some changes in a file in the local host, it still would not be synced if its present on the remote host.

Does rsync look at timestamps?

Note though, that rsync compares timestamps (and other metadata) to determine whether files are updated and needs copying. You may therefore want to use -u ( --update ) too.

Does rsync replace?

On Unix-like operating systems, the rsync command synchronizes files from a source to a destination, on a local machine or over a secure network connection. It is a fast, flexible, secure replacement for the command rcp.


2 Answers

If you have problems with rsync and timestamps, perhaps you need to look into the precision with which timestamps are stored on your various filesystems involved in the synchronization.

Use fstat to spot this: if you sync a file and the original has a timestamp of 2012-01-10 23:41:04.348724000 and the synced file timestamp 2012-01-10 23:41:04.000000000 then this is a sign of a difference in precision.

rsync's option --modify-window=1 can deal with this difference in precision by allowing for a small difference.

like image 138
Martijn de Milliano Avatar answered Oct 23 '22 16:10

Martijn de Milliano


rsync av --update /loc1 /loc2

so only files that are NEWER will be synced from loc1 to loc2. Logic dictates that any files that are NEWER on loc2 will be untouched. Therefore all the outdated files on loc2 will be up-to-date thanks to loc1

rsync av --update /loc2 /loc1

Now we know that all the files that loc1 had that were newer were copied to loc2. Any files that were older on loc1 (that had newer ones on loc2) remained unchanged. The second rsync command now will update loc1 with files newer on loc2

Et Voila! both locations are synchronised in this example.

like image 23
nyk Avatar answered Oct 23 '22 18:10

nyk