问题原文
原帖链接 · jmituzas · CC BY-SA 4.0
I am working on an rsync script for directory replication. I have it syncing only new and modified files or directories but I don't like the fact that it's copying renamed files or directories as a new file or directory, keeping the files not in sync. I have also set a bandwidth limit of 1MB since this will run during business work hours.
Here's my script:
rsync -zvru --bwlimit=1024 /mymounts/test1/ /mymounts/test2
How can I keep the files and directories in sync if someone renames something and still only copy new or modified files?
Here are the files in question:
ls "/mymounts/test1/some stuff"
new directory newfile1.txt newfile3.txt renamedFile.txt
ls "/mymounts/test2/some stuff"
new directory newfile1.txt newfile2.txt newfile3.txt renamedFile.txt
Or would there be a way to even move the renamed files to another directory say: /mymounts/VerControl?
社区高分解答
原帖链接 · dparoli · CC BY-SA 3.0
You can handle moved and renamed files with rsync if the filesystems on the source and target directory have support for hard links. The idea is to let rsync reconstruct hard links before real transfer. You can find a brilliant explanation here.
We ended up with a simple solution that create an hidden tree of hard links inside the source/target directory, the basic script could be like this:
# Name of hidden directory
Shadow=".rsync_shadow"
# do real sync
rsync -ahHv --stats --no-inc-recursive --delete --delete-after "$Source"/ "$Target"
# update/create hidden dir of hard links in source
rsync -a --delete --link-dest="$Source" --exclude="/$Shadow" "$Source"/ "$Source/$Shadow"
# update/create hidden dir of hard links in target
rsync -a --delete --link-dest="$Target" --exclude="/$Shadow" "$Target"/ "$Target/$Shadow"
I have an example script on GitHub. But I advise you to do a large amount of testing before use this method on production.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
