deleting remote dirs I no longer want to backup

Wayne Davison wayned at
Fri Jan 20 17:47:36 GMT 2006

On Fri, Jan 20, 2006 at 03:33:06PM +0100, Christophe LYON wrote:
> rsync -avn --exclude=dirb --delete-excluded test-rsync/ /tmp
> but in this particular case, it tries to remove many things in /tmp 
> which is obviously populated with many files ;-)

That command tells rsync to make the /tmp dir identical with the sending
dir, which is why it tries to remove everything missing from the source
dir from the destination dir.

There are several solutions, and which one is right for you depends on
how new your rsync version is.  For instance, a way that works with any
rsync version is to copy from an empty dir to get rsync to do a

  mkdir empty-directory
  rsync -av --delete --include=/dirb --exclude='*' empty-directory/ /tmp
  rmdir empty-directory

That tells rsync to only send dirb on the sending side (which is
missing) and protects everything except dirb on the receiving side.

An alternate way to do this that also updates dira requires at least
version 2.6.4:  you can use a hide filter to make rsync transfer exactly
what you want and avoid deleting anything extra (because you don't need
to use --delete-excluded).  Like this:

  rsync -av --del -f 'hide /dirb' --include='/dir[ab]' --exclude='/*' . /tmp

This tells rsync to send the parent directory of the dira and dirb dirs
with only dira and dirb included in the transfer (you could use separate
--include options, if you prefer).  It pretends that dirb does not exist
on the sending side (due to the hide filter, which must come before the
include).  On the receiving side, rsync does not protect dirb from
deletion because the include rule comes before the exclude rule.  The
exclude makes sure that nothing else gets deleted in the destination
dir.  You'll note that all the include/filter/exclude rules are anchored
to the root of the transfer so that they only apply where they should
(and not accidentally deeper in the transfer).


More information about the rsync mailing list