我正在 Docker 容器中使用 WebDAV 服务器测试 rsync:
- 本地目录:/Users/user/files/
- “远程”安装的服务器:/Volumes/webdav/
这是初始状态:
# remote
➜ cat /Volumes/webdav/remotefile
change
➜ ls -la /Volumes/webdav/remotefile
-rwx------ 1 user staff 7 Dec 2 01:39 /Volumes/webdav/remotefile
# local
➜ cat /Users/user/files/remotefile
change
➜ ls -la /Users/user/files/remotefile
-rwx------ 1 user staff 7 Dec 2 01:39 /Users/user/files/remotefile
现在让我们更改本地文件并将其上传到“远程”服务器:
➜ files echo 'add#0' > ./remotefile
➜ files cat remotefile
add#0
➜ files \rsync -varP /Users/user/files/* /Volumes/webdav/ --delete
building file list ...
1 file to consider
remotefile
6 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 132 bytes received 42 bytes 348.00 bytes/sec
total size is 6 speedup is 0.03
文件上传成功,因为它已被更改。但如果我再次运行 rsync local => remote,它会重新上传它:
➜ files \rsync -varP /Users/user/files/* /Volumes/webdav/ --delete
building file list ...
1 file to consider
remotefile
6 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 132 bytes received 42 bytes 348.00 bytes/sec
total size is 6 speedup is 0.03
现在我测试相反的方向:
➜ files \rsync -varP /Volumes/webdav/* /Users/user/files/ --delete
building file list ...
1 file to consider
remotefile
6 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 132 bytes received 42 bytes 348.00 bytes/sec
total size is 6 speedup is 0.03
文件下载成功。再来一次……
➜ files \rsync -varP /Volumes/webdav/* /Users/user/files/ --delete
building file list ...
1 file to consider
sent 80 bytes received 20 bytes 200.00 bytes/sec
total size is 6 speedup is 0.06
...显示 0 次传输,因为文件未更改。这是预期的行为。
为什么它不适用于本地 => 远程上传并且总是重新上传文件?
谢谢。
答案1
local
您是否已确认和上的时钟同步remote
?
来自rsync 手册页:
Rsync finds files that need to be transferred using a lqquick checkrq algorithm
(by default) that looks for files that have changed in size
or in last-modified time.
如果时钟是不同步,并且您无法编辑一个,请尝试告诉 rsync 在检查时间时稍微“模糊”一点。再次从手册页同步
--modify-window
When comparing two timestamps, rsync treats the timestamps
as being equal if they differ by no more than the modify-window value.
This is normally 0 (for an exact match), but you may find it useful
to set this to a larger value in some situations.
In particular, when transferring to or from an MS Windows FAT filesystem
(which represents times with a 2-second resolution),
--modify-window=1 is useful (allowing times to differ by up to 1 second).
尝试使用该--size-only
选项运行 rsync 并查看行为是否rsync
发生变化。
另一种选择是使用--checksum
rsync 选项。这需要更多的磁盘 I/O 和周期来生成校验和,因此速度可能会更慢。想知道它是否会转移问题会很有趣。
答案2
确保将 --times (-t) 传递给 rsync。这会导致修改时间被复制过来。
否则,如果您立即再次运行 rsync,它将看到所有文件都“已更改”,需要复制(再次!)。这是因为默认情况下,rsync 会执行以下操作:
- 查看大小和修改时间来确定文件是否已更改。
- 修改时不会进行复制。
(我喜欢 rsync 默认的自毁行为。)
--archive (-a) 暗示 --times,因此如果您已经在使用 --archive,则无需使用 --times。