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

时间:2020-11-25 01:06:22

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是否存在,则如果尚未安装该目录,则将使用该名称创建目录。

3 个解决方案

#1


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:

此外,您始终可以优化if:

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

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

#2


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选项结合使用,则不会更新任何文件(如果您只想删除无关文件,这将非常有用)。

#3


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

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

#1


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:

此外,您始终可以优化if:

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

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

#2


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选项结合使用,则不会更新任何文件(如果您只想删除无关文件,这将非常有用)。

#3


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

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