Is there a way to tell rsync
to sync only source directories that are missing on a destination (whole directories, not just any missing file)?
Considering S
as the set of source directories, and D
the set of destination directories, I want to:
- copy the entire directory
s
inS
ifs
not inD
. - skip entirely the directory
s
(indep. of the files it contains) ifs
inD
.
Of course it's possible to list the directories on both sides, rsync
the list from the destination, do some perl
and generate a list of all directories that need to be copied, but it would be better if it were possible with just one invocation of rsync
.
For example, if source and destination are on the same server, one could do:
src=/some/where
dst=/else/where
cd /tmp
(cd $src; find . -type d) | sort > a
(cd $dst; find . -type d) | sort > b
comm -23 a b | perl -e 'L: while(<>) {chomp; $p=$_; while ($p=~s,/[^/]+$,,) { next L if $n{$p}; } $n{$_}++; s,^./,/,; print "$_/***\n"}' > tocopy
rsync -vmazn --include-from tocopy --exclude '*' $src/ $dst/
(without the -n
if for real).
PS: note that the perl "one-liner" above (more or less) strips subdirs of dirs that we decide to copy (as those subdirs are subsumed in the copy of their parent). That recipe ends up with the minimal set of dirs to copy.