第一次尝试(无效)
除了文件之外,还需要包括目录:
rsync -zavC --delete --include '*/' --include='*.php' --include='*.js' \
--exclude="*" /media/datacod/Test/ /home/lucas/Desktop/rsync/
第二次尝试
rsync -avzC --filter='-rs_*/.svn*' --include="*/" --include='*.js' \
--include='*.php' --exclude="*" --delete dir1/ dir2/
测试数据
我编写了此脚本来创建一些示例数据以对其进行测试。这是脚本setup_svn_sample.bash
:
#!/bin/bash
# setup .svn dirs
mkdir -p dir{1,2}/dir{1,2,3,4}/.svn
# fake data under .svn
mkdir -p dir1/dir{1,2,3,4}/.svn/origdir
mkdir -p dir2/dir{1,2,3,4}/.svn/keepdir
# files to not sync
touch dir1/dir{1,2,3,4}/file{1,2}
# files to sync
touch dir1/dir{1,2,3,4}/file1.js
touch dir1/dir{1,2,3,4}/file1.php
运行它会生成以下目录:
源目录
$ tree -a dir1
dir1
|-- dir1
| |-- file1
| |-- file1.js
| |-- file1.php
| |-- file2
| `-- .svn
| `-- origdir
|-- dir2
| |-- file1
| |-- file1.js
| |-- file1.php
| |-- file2
| `-- .svn
| `-- origdir
|-- dir3
| |-- file1
| |-- file1.js
| |-- file1.php
| |-- file2
| `-- .svn
| `-- origdir
`-- dir4
|-- file1
|-- file1.js
|-- file1.php
|-- file2
`-- .svn
`-- origdir
目标目录
$ tree -a dir2
dir2
|-- dir1
| `-- .svn
| `-- keepdir
|-- dir2
| `-- .svn
| `-- keepdir
|-- dir3
| `-- .svn
| `-- keepdir
`-- dir4
`-- .svn
`-- keepdir
运行上面的rsync
命令:
rsync -avzC --filter='-rs_*/.svn*' --include="*/" --include='*.js' \
--include='*.php' --exclude="*" --delete dir1/ dir2/
sending incremental file list
dir1/file1.js
dir1/file1.php
dir2/file1.js
dir2/file1.php
dir3/file1.js
dir3/file1.php
dir4/file1.js
dir4/file1.php
sent 480 bytes received 168 bytes 1296.00 bytes/sec
total size is 0 speedup is 0.00
产生的dir2延迟:
$ tree -a dir2
dir2
|-- dir1
| |-- file1.js
| |-- file1.php
| `-- .svn
| `-- keepdir
|-- dir2
| |-- file1.js
| |-- file1.php
| `-- .svn
| `-- keepdir
|-- dir3
| |-- file1.js
| |-- file1.php
| `-- .svn
| `-- keepdir
`-- dir4
|-- file1.js
|-- file1.php
`-- .svn
`-- keepdir
为什么行得通?
该脚本的关键是利用的过滤器功能rsync
。过滤器使您可以在命令的各个位置从匹配集中删除文件。因此,在本例中,我们正在过滤与模式匹配的所有文件*/.svn*
。修饰符-rs_
告诉过滤器我们要在源端和目标端都进行过滤。
摘自rsync手册页的FILTER NOTES部分
使用s表示该规则适用于发送方。当规则影响发送方时,它将阻止文件传输。除非--delete-excluded
指定了默认值,否则规则会影响双方
,在这种情况下,默认规则仅成为发送方。另请参阅隐藏(H)和显示(S)规则,它们是指定发送方包含/排除的替代方法。
一个[R 是用于指示该规则适用于接收侧。当规则影响接收方时,它将阻止文件被删除。有关更多信息,请参见s修饰符。另请参阅保护(P)和风险(R)规则,它们是指定接收方包含/排除的替代方法。
有关更多详细信息,请参见man rsync。
解决这个问题的技巧(使用提示--dry-run
)
在描述如何执行此操作时,我想我会提到--dry-run
切换到rsync
。这对于在没有rsync
实际发生的情况下看到将发生的情况非常有用。
例如
使用以下命令将进行测试运行,并向我们展示背后的决策逻辑rsync
:
rsync --dry-run -avvzC --filter='-rs_*/.svn*' --include="*/" \
--include='*.js' --include='*.php' --exclude="*" --delete dir1/ dir2/
sending incremental file list
[sender] showing directory dir3 because of pattern */
[sender] showing directory dir2 because of pattern */
[sender] showing directory dir4 because of pattern */
[sender] showing directory dir1 because of pattern */
[sender] hiding file dir1/file1 because of pattern *
[sender] showing file dir1/file1.js because of pattern *.js
[sender] hiding file dir1/file2 because of pattern *
[sender] showing file dir1/file1.php because of pattern *.php
[sender] hiding directory dir1/.svn because of pattern */.svn*
[sender] hiding file dir2/file1 because of pattern *
[sender] showing file dir2/file1.js because of pattern *.js
[sender] hiding file dir2/file2 because of pattern *
[sender] showing file dir2/file1.php because of pattern *.php
[sender] hiding directory dir2/.svn because of pattern */.svn*
[sender] hiding file dir3/file1 because of pattern *
[sender] showing file dir3/file1.js because of pattern *.js
[sender] hiding file dir3/file2 because of pattern *
[sender] showing file dir3/file1.php because of pattern *.php
[sender] hiding directory dir3/.svn because of pattern */.svn*
[sender] hiding file dir4/file1 because of pattern *
[sender] showing file dir4/file1.js because of pattern *.js
[sender] hiding file dir4/file2 because of pattern *
[sender] showing file dir4/file1.php because of pattern *.php
[sender] hiding directory dir4/.svn because of pattern */.svn*
delta-transmission disabled for local transfer or --whole-file
[generator] risking directory dir3 because of pattern */
[generator] risking directory dir2 because of pattern */
[generator] risking directory dir4 because of pattern */
[generator] risking directory dir1 because of pattern */
[generator] protecting directory dir1/.svn because of pattern */.svn*
dir1/file1.js
dir1/file1.php
[generator] protecting directory dir2/.svn because of pattern */.svn*
dir2/file1.js
dir2/file1.php
[generator] protecting directory dir3/.svn because of pattern */.svn*
dir3/file1.js
dir3/file1.php
[generator] protecting directory dir4/.svn because of pattern */.svn*
dir4/file1.js
dir4/file1.php
total: matches=0 hash_hits=0 false_alarms=0 data=0
sent 231 bytes received 55 bytes 572.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
在上面的输出中,您可以看到./svn
目录受到我们的过滤规则的保护。对调试rsync
。
参考文献