A perl script that outputs excludes for all mount points that affect
[rsync/rsync.git] / support / mnt-excl
1 #!/usr/bin/perl -w
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:
8 #
9 # mnt-excl /dir | rsync --exclude-from=- ... /dir /dest/
10 # mnt-excl /dir/ | rsync --exclude-from=- ... /dir/ /dest/
11 #
12 # Imagine that /dir/foo is a mount point: the first invocation of
13 # mnt-excl would have output /dir/foo, while the second would have
14 # output /foo (which are the properly anchored excludes).
15 #
16 # NOTE:  This script expects /proc/mounts to exist, but could be
17 # easily adapted to read /etc/mtab or similar.
18
19 use strict;
20 use Cwd 'abs_path';
21
22 my $dir = shift || '/';
23 $dir = abs_path($dir);
24 $dir =~ s#([^/]*)$##;
25 my $trailing = $1;
26 $trailing = '' if $trailing eq '.' || !-d "$dir$trailing";
27 $trailing .= '/' if $trailing ne '';
28
29 open(IN, '/proc/mounts') or die $!;
30 while (<IN>) {
31     $_ = (split)[1];
32     next unless s#^\Q$dir$trailing\E##o && $_ ne '';
33     print "- /$trailing$_\n";
34 }
35 close IN;