|
@ -24,28 +24,28 @@ class Cloner: |
|
|
elif entry.is_file(): |
|
|
elif entry.is_file(): |
|
|
self.handleFile(entry.path) |
|
|
self.handleFile(entry.path) |
|
|
|
|
|
|
|
|
def handleFile(self, fileName: str): |
|
|
|
|
|
cloneFileName = fileName.replace(self.targetDir, self.backupDir) |
|
|
|
|
|
|
|
|
def handleFile(self, targetFileName: str): |
|
|
|
|
|
cloneFileName = targetFileName.replace(self.targetDir, self.backupDir) |
|
|
cloneHashFileName = cloneFileName + '.md5' |
|
|
cloneHashFileName = cloneFileName + '.md5' |
|
|
|
|
|
|
|
|
if os.path.exists(cloneFileName): |
|
|
if os.path.exists(cloneFileName): |
|
|
with open(cloneHashFileName, 'r') as cloneHashFile, open(fileName, 'rb') as targetFile: |
|
|
|
|
|
|
|
|
with open(cloneHashFileName, 'r') as cloneHashFile, open(targetFileName, 'rb') as targetFile: |
|
|
cloneHashFileContent = cloneHashFile.read() |
|
|
cloneHashFileContent = cloneHashFile.read() |
|
|
targetFileContent = targetFile.read() |
|
|
targetFileContent = targetFile.read() |
|
|
realHash = hashlib.md5(targetFileContent).hexdigest() |
|
|
realHash = hashlib.md5(targetFileContent).hexdigest() |
|
|
if realHash != cloneHashFileContent: |
|
|
if realHash != cloneHashFileContent: |
|
|
os.system('cp {backup} {real}'.format( |
|
|
os.system('cp {backup} {real}'.format( |
|
|
backup=cloneFileName, real=fileName)) |
|
|
|
|
|
|
|
|
backup=cloneFileName, real=targetFileName)) |
|
|
logging.warning( |
|
|
logging.warning( |
|
|
'{file} failed check, restoring...'.format(file=fileName)) |
|
|
|
|
|
|
|
|
'{file} failed check, restoring...'.format(file=targetFileName)) |
|
|
else: |
|
|
else: |
|
|
logging.info('{file} is fine'.format(file=fileName)) |
|
|
|
|
|
|
|
|
logging.info('{file} is fine'.format(file=targetFileName)) |
|
|
else: |
|
|
else: |
|
|
logging.warning( |
|
|
logging.warning( |
|
|
'{file} is a new file, added to backup dir'.format(file=fileName)) |
|
|
|
|
|
|
|
|
'{file} is a new file, added to backup dir'.format(file=targetFileName)) |
|
|
os.system('cp {real} {clone}'.format( |
|
|
os.system('cp {real} {clone}'.format( |
|
|
real=fileName, clone=cloneFileName)) |
|
|
|
|
|
with open(cloneHashFileName, 'w') as cloneHashFile, open(fileName, 'rb') as targetFile: |
|
|
|
|
|
|
|
|
real=targetFileName, clone=cloneFileName)) |
|
|
|
|
|
with open(cloneHashFileName, 'w') as cloneHashFile, open(targetFileName, 'rb') as targetFile: |
|
|
targetFileContent = targetFile.read() |
|
|
targetFileContent = targetFile.read() |
|
|
cloneHashFile.write(hashlib.md5(targetFileContent).hexdigest()) |
|
|
cloneHashFile.write(hashlib.md5(targetFileContent).hexdigest()) |
|
|
|
|
|
|
|
|