tags:

views:

665

answers:

3

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.

+1  A: 

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

Ray Booysen
with --existing, his new directories in the source hierarchy will never be created on the backup drive...
mjy
Yes these are the options I can't seem to get to work in the way I want them to. Even when the run aborts because destination dir does not exist, the destination dir gets created (although the tree is not copied in).
Marcus
+1  A: 

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

mjy
I do not find the first statement to be true. The directory is created regardless of the trailing slash. I like the shell optimization for the existence test, except I do want some error output if the directory does not exist, so I'll probably keep it verbose.
Marcus
+1  A: 

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

JesperE