【发布时间】:2013-01-31 00:47:49
【问题描述】:
目前我只对Directories 进行 RSync,如下所示:
* * * * * rsync -avz /var/www/public_html/images root@<remote-ip>:/var/www/public_html
那么我如何rsync 一个文件,例如/var/www/public_html/.htaccess?
【问题讨论】:
目前我只对Directories 进行 RSync,如下所示:
* * * * * rsync -avz /var/www/public_html/images root@<remote-ip>:/var/www/public_html
那么我如何rsync 一个文件,例如/var/www/public_html/.htaccess?
【问题讨论】:
您的操作方式与处理目录的方式相同,但您将文件名的完整路径指定为源。在您的示例中:
rsync -avz --progress /var/www/public_html/.htaccess root@<remote-ip>:/var/www/public_html/
正如 cmets 中提到的:由于 -a 包含 recurse,一个小错误就可以启动完整的目录树传输,因此更简单的方法可能是使用 @987654323 @,或将其替换为-lptgoD。
【讨论】:
基本语法
rsync options source destination
例子
rsync -az /var/www/public_html/filename root@<remote-ip>:/var/www/public_html
【讨论】:
如果相对于源和目标的根目录,文件路径中的所有目录都已经存在,那么 Michael Place 的答案非常有效。
但是如果你想用这个源路径同步文件怎么办:
/source-root/a/b/file
到具有以下目标路径的文件:
/target-root/a/b/file
并且目录 a 和 b 不存在?
您需要运行如下的 rsync 命令:
rsync -r --include="/a/" --include="/a/b/" --include="/a/b/file" --exclude="*" [source] [target]
【讨论】:
迄今为止,其中两个答案并不完全正确,他们会得到多个文件,而另一个则不是那么简单,这是 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 的顺序很重要。
【讨论】: