如何RSYNC单个文件?

时间:2013-02-15 03:55:39

标签: linux file rsync

目前我只对Directories进行RSync同步:

* * * * * rsync -avz /var/www/public_html/images root@<remote-ip>:/var/www/public_html

那么我如何rsync一个单独的文件,如/var/www/public_html/.htaccess

4 个答案:

答案 0 :(得分:125)

您以与目录相同的方式执行此操作,但是您指定文件名的完整路径作为源。在您的示例中:

rsync -avz /var/www/public_html/.htaccess root@<remote-ip>:/var/www/public_html/

答案 1 :(得分:13)

基本语法

rsync options source destination

实施例

rsync -az /var/www/public_html/filename root@<remote-ip>:/var/www/public_html

Read more

答案 2 :(得分:11)

如果相对于源和目标的根目录,文件路径中的所有目录都已存在,那么Michael Place的答案很有用。

但是,如果要将文件与此源路径同步,该怎么办:

/源根/ A / B /文件

到具有以下目标路径的文件:

/目标根/ A / B /文件

目录 a b 不存在?

您需要运行如下所示的rsync命令:

rsync -r --include="/a/" --include="/a/b/" --include="/a/b/file" --exclude="*" [source] [target]

答案 3 :(得分:0)

到目前为止,有两个答案不太正确,它们将得到多个文件,而另一个则并非如此简单,这是IMO的简单答案。

以下内容仅获取一个文件,但是您必须使用mkdir创建dest目录。这可能是最快的选择:

mkdir -p ./local/path/to/file
rsync user@remote:/remote/path/to/file/ -zarv --include "filename" --exclude "*" ./local/path/to/file/

如果/ remote / path中只有一个文件实例,那么执行以下操作,rsync可以为您创建目录。这可能会花费更多时间,因为它会搜索更多目录。另外,它将为/ remote / path中不在./local中的目录创建空目录

cd ./local
rsync user@remote:/remote/path -zarv --include "*/" --include "filename" --exclude "*" .

请记住,--include和--exclude的顺序很重要。