Rsync复制整个目录内容而不是单个文件

时间:2016-04-18 20:46:14

标签: linux shell command-line rsync

我遇到了rsync命令的问题,这个命令正在做我不期望的额外工作。下面的命令应该只将单个文件rsync到目标服务器的目录。

但是,它正在将额外的文件从${OUT_DIR}/make目录复制到目标中,即使我尝试--exclude该目录中的所有其他文件。

如何使rsync仅复制目标文件?

eval rsync -azu --delete \
--include ${OUT_DIR}/make/file.mk \
--exclude ${OUT_DIR}/make \
${OUT_DIR}/make/file.mk
${DEST_USER}@${DEST_SVR}:$OUT_DIR_DEST/make

我已经尝试删除“额外”选项,但是我遇到了错误,所以尝试让命令在上面运行似乎是最好的行动方案。但是,当我试图改变它时,这就是我得到的:

eval rsync \
${OUT_DIR}/make/file.mk
${DEST_USER}@${DEST_SVR}:$OUT_DIR_DEST/make

Incompatible options specified for inc-recursive connection.
rsync error: syntax or usage error (code 1) at compat.c(268) [sender=3.0.6]

删除--delete相关选项:

eval rsync -azu
${OUT_DIR}/make/file.mk
${DEST_USER}@${DEST_SVR}:$OUT_DIR_DEST/make

opening connection using: ssh -x -i ... rsync --server -vvulogDtprze.iLs . 

$OUT_DIR_DEST/make 
note: iconv_open("UTF-8", "UTF-8") succeeded.
(Client) Protocol versions: remote=30, negotiated=30
sending incremental file list
[sender] make_file(file.mk,*,0)
[sender] flist start=1, used=1, low=0, high=0
[sender] i=1 /OUTDIR/path file.mk mode=0100664 len=91 uid=300 gid=301 flags=5
send_file_list done
file list sent
send_files starting
ERROR: buffer overflow in recv_rules [receiver]
rsync error: error allocating core memory buffers (code 22) at util.c(123) [receiver=3.0.6]
rsync: connection unexpectedly closed (9 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]
_exit_cleanup(code=12, file=io.c, line=600): entered
_exit_cleanup(code=12, file=io.c, line=600): about to call exit(12)

我所做的大多数谷歌搜索都没有为这两个问题找到任何明显的解决方案。

1 个答案:

答案 0 :(得分:1)

我要离开eval业务,因为有什么额外的扩展不清楚,但我可以告诉你rsync应该是什么样子。如果您正在使用bash或zsh,那么您可以执行set -x它将显示它在变量扩展后运行的真正的命令。

最简单的解决方案是这样的:

rsync -azu \
   /local/path/to/make/file.mk \
   user@server:/remote/path/to/make/

如果你真的想使用包含/排除规则(如果有多个包含,可能是好的),那么就这样做:

rsync -azu \
   --include=file.mk \
   --exclude='*' \
   /local/path/to/make/ \
   user@server:/remote/path/to/make/

--include模式是相对于源目录的根目录的,所以如果你说--include=/local/path/to/make/file.mk那么那个模式只匹配一个名为/local/path/to/make/local/path/to/make/file.mk的文件,这显然不是你的的意思。

--exclude规则的工作方式相同,所以基本上你告诉它排除一些不存在的东西,所以没有任何东西被排除在外。 --exclude='*'表示排除所有内容(尚未明确包含),因此您不会收到类似的错误。

您可以使用--relative选项更改这些规则。这可能会使你的原始命令工作,但我会在这里创建远程系统上的文件:/remote/path/to/make/local/path/to/make/file.mk

无论如何,包含规则需要在排除规则之前:rsync使用匹配任何给定文件或目录的第一个规则。

您发布的错误消息表明远程计算机内存不足:

ERROR: buffer overflow in recv_rules [receiver]
rsync error: error allocating core memory buffers (code 22) at util.c(123) [receiver=3.0.6]

3.0中的内存使用量大大提高,但你似乎有3.0.6,所以大概不是你可以修复的东西。尝试新版本(我有3.1.2)可能不是一个糟糕的计划。

如果该文件夹下有很多文件(除了一个除外),那么你可以通过避免递归选项来节省内存:

rsync -ptgo -zu \
   /local/path/to/make/file.mk \
   user@server:/remote/path/to/make/

我所做的全部内容已被-a替换为等效的-rlptgoD,然后删除了不必要的选项,尤其是-r

如果所有其他方法都失败了,您可以尝试在该计算机上添加交换文件。

相关问题