|
|
@ -24,28 +24,28 @@ class Cloner: |
|
|
|
elif entry.is_file(): |
|
|
|
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' |
|
|
|
|
|
|
|
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() |
|
|
|
targetFileContent = targetFile.read() |
|
|
|
realHash = hashlib.md5(targetFileContent).hexdigest() |
|
|
|
if realHash != cloneHashFileContent: |
|
|
|
os.system('cp {backup} {real}'.format( |
|
|
|
backup=cloneFileName, real=fileName)) |
|
|
|
backup=cloneFileName, real=targetFileName)) |
|
|
|
logging.warning( |
|
|
|
'{file} failed check, restoring...'.format(file=fileName)) |
|
|
|
'{file} failed check, restoring...'.format(file=targetFileName)) |
|
|
|
else: |
|
|
|
logging.info('{file} is fine'.format(file=fileName)) |
|
|
|
logging.info('{file} is fine'.format(file=targetFileName)) |
|
|
|
else: |
|
|
|
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( |
|
|
|
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() |
|
|
|
cloneHashFile.write(hashlib.md5(targetFileContent).hexdigest()) |
|
|
|
|
|
|
|