Improve the discussion of the absolute-filter alternative.
[rsync/rsync.git] / support / mnt-excl
CommitLineData
01e293f1 1#!/usr/bin/perl
4c599711
WD
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/
026deaf7 11# ssh host mnt-excl /dir | rsync --exclude-from=- ... host:/dir /dest/
4c599711
WD
12#
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).
16#
17# NOTE: This script expects /proc/mounts to exist, but could be
18# easily adapted to read /etc/mtab or similar.
62586267
WD
19#
20# ADDENDUM: The addition of the --filter option (which has support for
3feece59
WD
21# absolute-anchored excludes) can make this screen 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
25# dirs. For instance:
62586267 26#
3feece59 27# awk '{print $2}' /proc/mounts | rsync -avf 'merge,/- -' /dir host:/dest/
4c599711
WD
28
29use strict;
01e293f1 30use warnings;
4c599711
WD
31use Cwd 'abs_path';
32
3d54c6ec 33my $file = '/proc/mounts';
4c599711
WD
34my $dir = shift || '/';
35$dir = abs_path($dir);
36$dir =~ s#([^/]*)$##;
37my $trailing = $1;
38$trailing = '' if $trailing eq '.' || !-d "$dir$trailing";
39$trailing .= '/' if $trailing ne '';
40
3d54c6ec 41open(IN, $file) or die "Unable to open $file: $!\n";
4c599711
WD
42while (<IN>) {
43 $_ = (split)[1];
44 next unless s#^\Q$dir$trailing\E##o && $_ ne '';
45 print "- /$trailing$_\n";
46}
47close IN;