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/
32 MNT_FILE = '/proc/mounts';
35 trailing_slash = '/' if args.path.endswith(('/', '/.')) and args.path != '/' else ''
36 args.path = os.path.realpath(args.path) + trailing_slash
37 parent_dir = os.path.dirname(args.path)
38 trailing = os.path.basename(args.path)
39 if not os.path.isdir(args.path):
43 want_path = os.path.join(parent_dir, trailing)
44 wp_len = len(want_path)
46 with open(MNT_FILE) as fh:
48 mnt_path = line.split()[1]
49 if mnt_path.startswith(want_path) and mnt_path != want_path:
50 print(f"- /{trailing}{mnt_path[wp_len:]}")
52 if __name__ == '__main__':
53 parser = argparse.ArgumentParser(description="Output mount points as rsync excludes.", add_help=False)
54 parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
55 parser.add_argument('path', metavar='PATH', nargs='?', default='/', help="Limit output to those within the PATH hierarchy.")
56 args = parser.parse_args()