我正在测试一个脚本,以使用 rsync 智能地对两个目录进行双向同步。
由于我正在测试许多 rsync 选项不适用于测试环境,包括以下行:
personal_excludes="
--exclude='home/jesse/scripts/'
--exclude='home/jesse/.*/'"
因此,当我认为我应该-prune
结束该排除时,我真的认为这不会对我的测试环境产生任何影响。
但是,添加-prune
到这两行会导致 rsync 执行 DRY-RUN!!!?什么??
有人愿意启发我这是如何/为什么吗?我应该为 rsync 提交错误报告吗?
工作目录
jesse@Limbo ~/dev/sync-script-testing $ ls -la . *
-rwxr-x--- 1 jesse jesse 1910 Dec 29 14:04 test-script.sh
.:
total 24
drwxr-x--- 5 jesse jesse 4096 Dec 29 14:05 .
drwxrwxr-x 7 jesse jesse 4096 Dec 29 13:53 ..
drwxr-x--- 2 jesse jesse 4096 Dec 29 13:43 dir1
drwxr-x--- 2 jesse jesse 4096 Dec 29 13:37 dir2
drwxr-x--- 8 jesse jesse 4096 Dec 29 14:03 .git
-rwxr-x--- 1 jesse jesse 1910 Dec 29 14:04 test-script.sh
dir1:
total 12
drwxr-x--- 2 jesse jesse 4096 Dec 29 13:43 .
drwxr-x--- 5 jesse jesse 4096 Dec 29 14:05 ..
-rw-r----- 1 jesse jesse 0 Dec 29 13:33 file2
-rw-r----- 1 jesse jesse 0 Dec 29 13:34 file3
-rw-r----- 1 jesse jesse 0 Dec 29 13:35 file4
-rw-r----- 1 jesse jesse 32 Dec 29 14:04 .lastsync
dir2:
total 12
drwxr-x--- 2 jesse jesse 4096 Dec 29 13:37 .
drwxr-x--- 5 jesse jesse 4096 Dec 29 14:05 ..
-rw-r----- 1 jesse jesse 0 Dec 29 13:33 file1
-rw-r----- 1 jesse jesse 0 Dec 29 13:34 file3
-rw-r----- 1 jesse jesse 0 Dec 29 13:35 file4
-rw-r----- 1 jesse jesse 32 Dec 29 14:04 .lastsync
脚本内容
jesse@Limbo ~/dev/sync-script-testing $ cat test-script.sh
#!/usr/bin/bash
#rsync -av --files-from=<(cd dir1 && find ./ -newermt "$(sed 's/^Successful sync on //' sync.log)") --exclude=/sync.log ./dir1/ ./dir2/ && echo "Successful sync on $(date -R)" | tee dir2/sync.log > dir1/sync.log
#
# # Perform the Sync
# echo -e "\n[1] Uppdate HDD -> USB DO NOT Include configuration .dot file - They take too long\n"
# First do a sync of ONLY those files modified since last run WITHOUT deleting any files
rsync_general_args="
--verbose
--human-readable
--progress
--recursive
--update
--links
--perms
--times
--group
--owner
--devices
--specials
--hard-links
--xattrs
--one-file-system
--one-file-system" # specifying --one-file-system twice means something different!
personal_excludes="
--exclude='home/jesse/scripts/'
--exclude='home/jesse/.*/'"
# Build list of files to sync
# First use mktemp to create a randomly named temporary directory to keep tempfiles in. This prevents hackers from intercepting data
tmpdir=$(mktemp -d "${TMPDIR:-/tmp/}$(basename $0).XXXXXXXXXXXX")
# Find files modified since the last sync, date saved in .lastcync
# Do this in a subshell so as not to change the current directory
$(cd dir1 && find ./ -newermt "$(cat .lastsync)" ! -path "./.lastsync" >$tmpdir/rsync_files_to_sync_from_dir1)
$(cd dir2 && find ./ -newermt "$(cat .lastsync)" ! -path "./.lastsync" >$tmpdir/rsync_files_to_sync_from_dir2)
echo; echo --files-from dir1=$(cat $tmpdir/rsync_files_to_sync_from_dir1)
rsync $rsync_general_args $personal_excludes --files-from=$tmpdir/rsync_files_to_sync_from_dir1 --exclude=/.lastsync ./dir1/ ./dir2/ && date -R > ./dir1/.lastsync
echo; echo --files-from dir2=$(cat $tmpdir/rsync_files_to_sync_from_dir2)
rsync $rsync_general_args $personal_excludes --files-from=$tmpdir/rsync_files_to_sync_from_dir2 --exclude=/.lastsync ./dir2/ ./dir1/ && date -R > ./dir2/.lastsync
# Clean Up
rm -r $tmpdir
脚本输出
jesse@Limbo ~/dev/sync-script-testing $ ./test-script.sh
--files-from dir1=
sending incremental file list
sent 18 bytes received 12 bytes 60.00 bytes/sec
total size is 0 speedup is 0.00
--files-from dir2=
sending incremental file list
sent 18 bytes received 12 bytes 60.00 bytes/sec
total size is 0 speedup is 0.00
更改脚本:添加“-prune”
jesse@Limbo ~/dev/sync-script-testing $ git log -p --grep WHY??
commit d9d50bdd289616faaf3d174e6023ba16a5286b53
Author: Jesse the Wind Wanderer <[email protected]>
Date: Sat Dec 29 14:03:43 2018 +0800
adding '-prune' causes rsync to do DRY-RUN. WHY???
diff --git a/test-script.sh b/test-script.sh
index 37ee2e6..b45f4a0 100755
--- a/test-script.sh
+++ b/test-script.sh
@@ -26,8 +26,8 @@ rsync_general_args="
--one-file-system" # specifying --one-file-system twice means something different!
personal_excludes="
- --exclude='home/jesse/scripts/'
- --exclude='home/jesse/.*/'"
+ --exclude='home/jesse/scripts/' -prune
+ --exclude='home/jesse/.*/' -prune"
# Build list of files to sync
# First use mktemp to create a randomly named temporary directory to keep tempfiles in. This prevents hackers from intercepting data
脚本现在通过 DRY-RUN 运行 rsync
jesse@Limbo ~/dev/sync-script-testing $ ./test-script.sh
--files-from dir1=
sending incremental file list
./
file2
sent 123 bytes received 22 bytes 290.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
--files-from dir2=
sending incremental file list
./
file1
sent 127 bytes received 22 bytes 298.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
答案1
呵呵,现在想想都觉得自己好傻。因此,作为对我自己和其他人的教训,我将发布此内容。 -prune
是一个选项find
不是rsync
! find 的不同寻常之处在于它只使用单个“-”和长选项。
添加-prune
到 rsync 意味着每个单独的字母都被解释为单个选项 '-p' '-r' '-u' '-n' (这 = Dry-Run!!) 和 '-e'