updated matrix program
This commit is contained in:
parent
2e09efb8d9
commit
583ee40431
52
matrix.py
52
matrix.py
@ -15,7 +15,7 @@ backup_file = ""
|
|||||||
rep = ""
|
rep = ""
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Parse arguments')
|
parser = argparse.ArgumentParser(description='Parse arguments')
|
||||||
parser.add_argument('mode', metavar='mode', type=str, choices=['backup','restore'], help='Mode')
|
parser.add_argument('mode', metavar='mode', type=str, choices=['backup', 'restore'], help='Mode')
|
||||||
parser.add_argument('backup_file', metavar='backup_file', type=str, help='Backup file')
|
parser.add_argument('backup_file', metavar='backup_file', type=str, help='Backup file')
|
||||||
parser.add_argument('directory', metavar='directory', type=str, help='Directory')
|
parser.add_argument('directory', metavar='directory', type=str, help='Directory')
|
||||||
|
|
||||||
@ -23,42 +23,50 @@ args = parser.parse_args()
|
|||||||
|
|
||||||
mode = args.mode
|
mode = args.mode
|
||||||
backup_file = args.backup_file
|
backup_file = args.backup_file
|
||||||
directory = args.directory.rstrip("/")
|
directory = args.directory
|
||||||
|
|
||||||
def backup():
|
def backup():
|
||||||
fd = open(backup_file,"w+")
|
i = 0
|
||||||
result=[]
|
with open(backup_file, "w+") as file_desc:
|
||||||
for rd,dr,fs in os.walk(directory):
|
result = []
|
||||||
for d in dr:
|
for root_dir, dirs, files in os.walk(directory):
|
||||||
res = os.path.join(rd, d)
|
for d in dirs:
|
||||||
|
res = os.path.join(root_dir, d)
|
||||||
result.append(res)
|
result.append(res)
|
||||||
for f in fs:
|
for f in files:
|
||||||
res = os.path.join(rd, f)
|
res = os.path.join(root_dir, f)
|
||||||
result.append(res)
|
result.append(res)
|
||||||
for f_name_tmp in result:
|
for f_name in result:
|
||||||
try:
|
try:
|
||||||
f_name = f_name_tmp.replace(directory,'').lstrip("/")
|
i += 1
|
||||||
f_perm = str(oct(os.stat(f_name_tmp).st_mode))
|
f_uid = os.stat(f_name).st_uid
|
||||||
line = "%s|%s\n" % (f_name,f_perm)
|
f_gid = os.stat(f_name).st_gid
|
||||||
fd.write(line)
|
f_perm = str(oct(os.stat(f_name).st_mode))
|
||||||
|
line = "{}|{}|{}|{}\n".format(f_name, f_uid, f_gid, f_perm)
|
||||||
|
file_desc.write(line)
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
fd.close()
|
print("Backup {} files permissions, storing to {}".format(i, backup_file))
|
||||||
|
|
||||||
def restore():
|
def restore():
|
||||||
fd = open(backup_file,"r+")
|
i = 0
|
||||||
for f in fd:
|
with open(backup_file, "r+") as file_desc:
|
||||||
|
for f in file_desc:
|
||||||
line = f.strip("\n")
|
line = f.strip("\n")
|
||||||
f_name_tmp = line.split('|')[0]
|
f_name = line.split('|')[0]
|
||||||
f_name = "%s/%s" % (directory,f_name_tmp)
|
f_uid = line.split('|')[1]
|
||||||
f_perm = line.split('|')[1]
|
f_gid = line.split('|')[2]
|
||||||
|
f_perm = line.split('|')[3]
|
||||||
try:
|
try:
|
||||||
os.chmod(f_name,int(f_perm,8))
|
os.chmod(f_name, int(f_perm, 8))
|
||||||
|
os.chown(f_name, int(f_uid), int(f_gid))
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
print("Restored {} files permissions".format(i))
|
||||||
|
|
||||||
if mode == "backup":
|
if mode == "backup":
|
||||||
backup()
|
backup()
|
||||||
elif mode == "restore":
|
elif mode == "restore":
|
||||||
restore()
|
restore()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user