2 # This helper script makes it easy to use a passwd or group file to map values
3 # in a LOCAL transfer. For instance, if you mount a backup that does not have
4 # the same passwd setup as the local machine, you can do a copy to/from the
5 # backup area as follows and get the differing ID values mapped just like a
6 # remote transfer to/from the backed-up machine would do:
8 # rsync -av --usermap=`idmap --to /mnt/backup/etc/passwd` \
9 # --groupmap=`idmap --to /mnt/backup/etc/group` \
10 # /some/src/ /mnt/backup/some/dest/
12 # rsync -av --usermap=`idmap --from /mnt/backup/etc/passwd` \
13 # --groupmap=`idmap --from /mnt/backup/etc/group` \
14 # /mnt/backup/some/src/ /some/dest/
16 import re, fileinput, argparse
18 NAME_ID_RE = re.compile(r'^(\w+):[^:]+:(\d+)')
22 for line in fileinput.input(args.files):
23 m = NAME_ID_RE.match(line)
30 maps.append(':'.join(pair))
34 if __name__ == '__main__':
35 parser = argparse.ArgumentParser(description="Output usermap or groupmap args for rsync.", add_help=False)
36 action = parser.add_argument_group()
37 action = parser.add_mutually_exclusive_group(required=True)
38 action.add_argument("--from", action="store_true", help="Output the map for use on the sending side.")
39 action.add_argument("--to", action="store_true", help="Output the map for use on the receiving side.")
40 parser.add_argument("--help", "-h", action="help", help="Output this help message and exit.")
41 parser.add_argument("files", metavar="FILE", default='-', nargs='*', help="The file(s) that hold the name & id pairs. Defaults to stdin.")
42 args = parser.parse_args()