如何告诉 rsync 仅在目标目录存在时运行?
我有这个 bash 脚本将我的备份运行到外部硬盘驱动器...仅当该驱动器已安装时(OS X):
DIR=/Volumes/External;
if [ -e $DIR ];
then rsync -av ~/dir_to_backup $DIR;
else echo "$DIR does not exist";
fi
这有效,但我感觉我误读了 rsync 手册页。 如果顶级目标目录不存在,是否有内置 rsync 选项可以中止运行? 在不测试 /Volumes/External 是否存在的情况下,如果尚未安装目录,则会使用该名称创建一个目录。
I have this bash script running my backup to an external hard drive... only if that drive is mounted (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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AFAIK 不,但您可以使用尾部斜杠来模拟行为:
rsync -av dir_to_backup /Volumes/External/;
如果目录不存在(可能或可能不存在),它将退出并显示错误所需)。
此外,您始终可以优化 if:
test -e $DIR && rsync -av ...
AFAIK no, but you can simulate the behavour with a trailing slash:
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 ...
这两个标志看起来像您正在寻找的:
从手册页:
These two flags look like what you're looking for:
From the man page:
不,据我从联机帮助页中看到,似乎没有任何这样的选项。
No, there does not seem to be any such option, as far as I can see from the manpage.