Is it possible to create a script to save and restore permissions?

前端 未结 13 1337
逝去的感伤
逝去的感伤 2021-01-30 04:29

I am using a linux system and need to experiment with some permissions on a set of nested files and directories. I wonder if there is not some way to save the permissions for t

13条回答
  •  一整个雨季
    2021-01-30 05:13

    I found best way (for me) do it with python!

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    import os
    import json
    import sys
    import re
    try:
        import posix1e
    except ImportError:
        print("No module named 'posix1e'")
        print("You need do: apt install python3-pylibacl")
        sys.exit()
    
    def dump_acl(dir):
        acl_dict = {}
        for root, dirs, files in os.walk(dir):
            try:
                path = root.split(os.sep)
                root_acl = str(posix1e.ACL(file=root))
                root_def_acl = str(posix1e.ACL(filedef=root))
                #print(root_acl)
                acl_dict[root] = root_acl
                acl_dict["default_" + root] = root_def_acl
                for file_name in files:
                    try:
                        if 'acl.json' in file_name:
                            continue
                        file_path = root + "/" + file_name
                        file_acl = str(posix1e.ACL(file=file_path))
                        acl_dict[file_path] = file_acl
                        #print(file_path)
                    except Exception as e:
                        print(e)
                        print(root, '/' + file_name)
                        continue
            except Exception as e:
                print(e)
                print(root, '/' + file_name)
                continue
    
        with open(dir + '/acl.json', 'bw') as f:
            f.write(json.dumps(acl_dict, ensure_ascii=False).encode('utf8'))
        return
    
    
    def recovery_acl(dir):
        with open(dir + '/acl.json', 'r') as f:
            acl_dict = json.load(f)
        try:
            for file_path, file_acl in acl_dict.items():
                if file_path.startswith('default_'):
                    file_path = file_path.replace('default_', '', 1)
                    posix1e.ACL(text = file_acl).applyto(file_path, posix1e.ACL_TYPE_DEFAULT)
                    continue
                if 'acl.json' in file_path:
                    continue
                file_acl = file_acl.replace('\n', ',', file_acl.count('\n') -1)
                file_acl = file_acl.replace('\134', u'\ ' [:-1])
                file_acl = file_acl.replace('\040', u' ')
                if 'effective' in file_acl:
                    file_acl = file_acl.replace('\t', '')
                    f_acl = re.sub('#effective:[r,w,x,-]{3}', '', file_acl)
                posix1e.ACL(text = file_acl).applyto(file_path)
        except Exception as e:
            print(e, file_path, file_acl)
        return
    
    def help_usage():
        print("Usage:")
        print("For dump acl:   ", sys.argv[0], "-d /path/to/dir")
        print("For restore acl:", sys.argv[0], "-r /path/to/dir")
        print("File with acls (acl.json) storing in the same dir")
        sys.exit()
    
    
    if len(sys.argv) == 3 and os.path.isdir(sys.argv[2]):
        if sys.argv[1] == '-d':
            dump_acl(sys.argv[2])
        elif sys.argv[1] == '-r':
            if os.path.exists(sys.argv[2] + '/acl.json'):
                recovery_acl(sys.argv[2])
            else:
                print("File not found:", sys.argv[2] + '/acl.json')
    else:
        help_usage()
    

    backup acl: dump_acl.py -d /path/to/dir

    recovery acl: dump_acl.py -r /path/to/dir

    After execution, script create acl.json in the same dir(witch backup/restore acls)

提交回复
热议问题