2 # This script takes a command-line arg of a source directory
3 # that will be passed to rsync, and generates a set of excludes
4 # that will exclude all mount points from the list. This is
5 # useful if you have "bind" mounts since the --one-file-system
6 # option won't notice the transition to a different spot on
7 # the same disk. For example:
9 # mnt-excl /dir | rsync --exclude-from=- ... /dir /dest/
10 # mnt-excl /dir/ | rsync --exclude-from=- ... /dir/ /dest/
11 # ssh host mnt-excl /dir | rsync --exclude-from=- ... host:/dir /dest/
13 # Imagine that /dir/foo is a mount point: the first invocation of
14 # mnt-excl would have output /dir/foo, while the second would have
15 # output /foo (which are the properly anchored excludes).
17 # NOTE: This script expects /proc/mounts to exist, but could be
18 # easily adapted to read /etc/mtab or similar.
20 # ADDENDUM: The addition of the --filter option (which has support for
21 # absolute-anchored excludes) can make this script unneeded in some
22 # scenarios. If you don't need delete protection on the receiving side
23 # (or if the destination path is identical to the source path), then you
24 # can exclude some absolute paths from the transfer based on the mount
27 # awk '{print $2}' /proc/mounts | grep -v '^/$' | \
28 # rsync -avf 'merge,/- -' /dir host:/dest/
34 my $file = '/proc/mounts';
35 my $dir = shift || '/';
36 my $trailing_slash = $dir =~ m{./$} ?
'/' : '';
37 $dir = abs_path
($dir) . $trailing_slash;
38 $dir =~ s{([^/]*)$}{};
40 $trailing = '' if $trailing eq '.' || !-d
"$dir$trailing";
41 $trailing .= '/' if $trailing ne '';
43 open(IN
, $file) or die "Unable to open $file: $!\n";
46 next unless s{^\Q$dir$trailing\E}{}o && $_ ne '';
47 print "- /$trailing$_\n";