bash 我如何告诉 rsync 仅在目标目录存在时运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/411092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:36:26  来源:igfitidea点击:

How do I tell rsync to run only if the destination directory exists?

bashrsync

提问by Marcus

I have this bash script running my backup to an external hard drive... only if that drive is mounted (OS X):

我有这个 bash 脚本将我的备份运行到外部硬盘驱动器......仅当该驱动器已安装(OS X):

DIR=/Volumes/External; 
if [ -e $DIR ]; 
then rsync -av ~/dir_to_backup $DIR; 
else echo "$DIR does not exist"; 
fi

This works, but I sense I am misreading the rsync man page. Is there a builtin rsync option to abort the run if the top level destination directory does not exist? Without testing for the existence of /Volumes/External, a directory will be created by that name if it isn't already mounted.

这有效,但我觉得我误读了 rsync 手册页。如果顶级目标目录不存在,是否有内置的 rsync 选项来中止运行?如果不测试 /Volumes/External 的存在,则如果尚未安装目录,则会以该名称创建目录。

回答by Ray Booysen

These two flags look like what you're looking for:

这两个标志看起来像你要找的:

--existing, --ignore-non-existing

From the man page:

从手册页:

--existing, --ignore-non-existing
This tells rsync to skip creating files (including directories) that do not exist yet on the destination. If this option is combined with the --ignore-existing option, no files will be updated (which can be useful if all you want to do is delete extraneous files).

--existing, --ignore-non-existing
这告诉 rsync 跳过创建目标上尚不存在的文件(包括目录)。如果此选项与 --ignore-existing 选项结合使用,则不会更新任何文件(如果您只想删除无关文件,这会很有用)。

回答by mjy

AFAIK no, but you can simulate the behavour with a trailing slash:

AFAIK 不,但您可以使用尾部斜杠模拟行为:

rsync -av dir_to_backup /Volumes/External/;

rsync -av dir_to_backup /Volumes/External/;

It will exit with an error if the directory does not exist (which may or may not be desired).

如果目录不存在(可能需要也可能不需要),它将退出并显示错误。

Also, you can always optimize away the if:

此外,您始终可以优化以下条件:

test -e $DIR && rsync -av ...

test -e $DIR && rsync -av ...

回答by JesperE

No, there does not seem to be any such option, as far as I can see from the manpage.

不,就我从联机帮助页中看到的而言,似乎没有任何此类选项。