1 Debian patch touch this file
| •1 | #!/usr/bin/python3 |
| 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 | # ssh host mnt-excl /dir | rsync --exclude-from=- ... host:/dir /dest/ |
| 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. |
| 19 | # |
| 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 |
| 25 | # dirs. For instance: |
| 26 | # |
| 27 | # awk '{print $2}' /proc/mounts | grep -v '^/$' | \ |
| 28 | # rsync -avf 'merge,/- -' /dir host:/dest/ |
| 29 | |
| 30 | import os, argparse |
| 31 | |
| 32 | MNT_FILE = '/proc/mounts'; |
| 33 | |
| 34 | def main(): |
| 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): |
| 40 | trailing = '' |
| 41 | elif trailing != '': |
| 42 | trailing += '/' |
| 43 | want_path = os.path.join(parent_dir, trailing) |
| 44 | wp_len = len(want_path) |
| 45 | |
| 46 | with open(MNT_FILE) as fh: |
| 47 | for line in 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:]}") |
| 51 | |
| 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() |
| 57 | main() |
| 58 | |
| 59 | # vim: sw=4 et |