Debian Codegraph @

support/idmap

raw symbols SCIP
1 #!/usr/bin/env python3
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:
7 #
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/
11 #
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/
15
16 import re, fileinput, argparse
17
18 NAME_ID_RE = re.compile(r'^(\w+):[^:]+:(\d+)')
19
20 def main():
21 maps = [ ]
22 for line in fileinput.input(args.files):
23 m = NAME_ID_RE.match(line)
24 if not m:
25 continue
26 if args.to:
27 pair = (m[1], m[2])
28 else:
29 pair = (m[2], m[1])
30 maps.append(':'.join(pair))
31 print(','.join(maps))
32
33
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()
43 main()
44
45 # vim: sw=4 et