我找到了这个问题的多个答案,所以想问问实际使用它的人,而不是只想通过填写随机的半无用信息来制作最大的博客。
场景:我
rsync -av --progress /dir/a /dir/b
和它做它的事。
我将新文件添加到 /dir/a 并再次运行相同的命令,它知道它做了什么并且只复制新文件。
我将新文件添加到 /dir/a 并重命名 /dir/b 中的一些文件,也可能删除一些文件。
如果我rsync -av --progress /dir/a /dir/b再次运行,会复制什么?只是新文件,因为它知道它以前复制了什么,或者也被重命名/删除的文件,因为它们不再存在。
作为奖励,如果以前复制的文件被再次复制,是有办法防止这一点,所以,只有新加入/ DIR / A被复制?
目前我很乐意手动检查事物,但是随着数据变得越来越大,我将需要更多的自动化来执行此任务。
mur*_*uru 23
我将新文件添加到 /dir/a 并再次运行相同的命令,它知道它做了什么并且只复制新文件。
不,它不知道它在之前的运行中做了什么。它将接收端的数据与要发送的数据进行比较。如果数据足够小,这不会很明显,但是当您有足够大的目录时,很容易感觉到在复制实际开始之前进行比较所花费的时间。
默认检查是文件修改时间和大小。来自man 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.
This option changes this to compare a 128-bit checksum for each
file that has a matching size. Generating the checksums means
that both sides will expend a lot of disk I/O reading all the
data in the files in the transfer (and this is prior to any
reading that will be done to transfer changed files), so this
can slow things down significantly.
Run Code Online (Sandbox Code Playgroud)
和:
-u, --update
This forces rsync to skip any files which exist on the
destination and have a modified time that is newer than the
source file. (If an existing destination file has a
modification time equal to the source file’s, it will be updated
if the sizes are different.)
Run Code Online (Sandbox Code Playgroud)
请注意,您使用的选项并不暗示这些。-a是:
-a, --archive archive mode; same as -rlptgoD (no -H)
-r, --recursive recurse into directories
-l, --links copy symlinks as symlinks
-p, --perms preserve permissions
-o, --owner preserve owner (super-user only)
-g, --group preserve group
--devices preserve device files (super-user only)
--specials preserve special files
-D same as --devices --specials
-t, --times preserve times
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,rsync -av它没有内存,所以它也会复制重命名/删除的文件,因为它们存在于源中但不再存在于目标中。
使用选项-n'dry run',在运行rsync命令行之前检查会发生什么。
注意源目录后面的斜杠的特殊含义,看看两者之间的区别
rsync -av --progress dir/a/ dir/b
Run Code Online (Sandbox Code Playgroud)
和
rsync -av --progress dir/a dir/b
Run Code Online (Sandbox Code Playgroud)
这在手册中有描述man rsync。
您的特殊情况(将文件添加到源目录 'a' 并从目标目录 'b' 中删除文件)将添加添加的文件和之前复制的文件,因为它仍在源目录中。这将在有和没有选项的情况下发生,如果您想将其保留在源目录中-u,我不知道任何可以rsync轻松解决该问题的选项。
但是您可以将其从源目录中删除或将文件名放入文件中excluded并使用该选项--exclude-from=excluded(对于许多文件)或仅--exclude=PATTERN用于一个或几个文件。
$ rsync -avn --progress dir/a/ dir/b
sending incremental file list
./
file-1
file-2
sent 103 bytes received 25 bytes 256.00 bytes/sec
total size is 13 speedup is 0.10 (DRY RUN)
$ rsync -av --progress dir/a/ dir/b
sending incremental file list
./
file-1
6 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=1/3)
file-2
7 100% 6.84kB/s 0:00:00 (xfr#2, to-chk=0/3)
sent 196 bytes received 57 bytes 506.00 bytes/sec
total size is 13 speedup is 0.05
$ echo textx-3>./dir/a/file-3
$ rsync -avn --progress dir/a/ dir/b
sending incremental file list
./
file-3
sent 121 bytes received 22 bytes 286.00 bytes/sec
total size is 21 speedup is 0.15 (DRY RUN)
$ rm dir/b/file-1
rm: ta bort normal fil 'dir/b/file-1'? y
$ rsync -avn --progress dir/a/ dir/b
sending incremental file list
./
file-1
file-3
sent 124 bytes received 25 bytes 298.00 bytes/sec
total size is 21 speedup is 0.14 (DRY RUN)
$ rsync -avun --progress dir/a/ dir/b
sending incremental file list
./
file-1
file-3
sent 124 bytes received 25 bytes 298.00 bytes/sec
total size is 21 speedup is 0.14 (DRY RUN)
$ rsync -avun --exclude=file-1 --progress dir/a/ dir/b
sending incremental file list
./
file-3
sent 104 bytes received 22 bytes 252.00 bytes/sec
total size is 15 speedup is 0.12 (DRY RUN)
Run Code Online (Sandbox Code Playgroud)
unison您可能想要测试该工具unison,它是一个同步工具。它提供了一种视觉方法来识别特殊情况并决定做什么。有一个 GUI 版本 ( unison-gtk)。