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

发布于 2024-07-11 18:04:33 字数 322 浏览 6 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我不在是我 2024-07-18 18:04:33

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 ...

何以畏孤独 2024-07-18 18:04:33

这两个标志看起来像您正在寻找的:

--existing, --ignore-non-existing

从手册页:

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

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).

£冰雨忧蓝° 2024-07-18 18:04:33

不,据我从联机帮助页中看到,似乎没有任何这样的选项。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文