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 nee开发者_JS百科d 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.
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.
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.
After testing, I believe the answer to this is yes.
As for why I didn't just test this in the first place before asking, I'd misunderstood the way rsync worked and thought that you always needed to set up an rsync daemon on the server.
However if you use ssh as the transfer mechanism then there doesn't need to be an rsync daemon running on the server, just an ssh daemon, which is a lot more common.
In my testing, a newer, changed file on my local machine was overwritten by the older file on the server (even though I had used the --update option).
My conclusion is that rsync is better for master-slave copying/updating, rather than true bidirectional, peer-based synchronization.
I will have to look into Mercurial or possibly Microsoft SyncToy (since both machines are running XP); I will probably go with the latter since it's a simple home network.
Have you tried executing rsync --update twice. I might be wrong but I think the following pair of commands would achieve a bi-directional sync.
rsync -avz --stats --update geezer/ merlin
rsync -avz --stats --update merlin/ geezer
here it is just syncing 2 directories geezer and merlin, but I think it should behave the same if the source and dir were full paths including machine names.
精彩评论