From d821a5e357e7e02e22549c86744629f9ca480cd9 Mon Sep 17 00:00:00 2001 From: lionel <> Date: Sun, 14 Aug 2022 21:19:33 +0200 Subject: [PATCH] Check hash --- backup.py | 41 +++++++++++++++++++++++++++++++++++++++-- main.py | 18 ++++++++++-------- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/backup.py b/backup.py index d7c2430..90832db 100644 --- a/backup.py +++ b/backup.py @@ -72,6 +72,25 @@ class Backup: # Delete orphan crypt self.__delete_file(str(orphan['id']).zfill(ZFILL)) self.bdd.delete_crypt(orphan['id']) + nocrypts = self.bdd.get_file_no_crypt() + for nocrypt in nocrypts: + self.bdd.delete_file(nocrypt) + return self + + def check(self, path, recurse=True): + #paths = [paths] if not isinstance(paths, list) else paths + # files = self.bdd.get_files(path) + crypts = self.bdd.get_crypts(path) + # if not recurse: + # crypts = [f for f in crypts if (pathlib.Path(f['path'])).parent == pathlib.Path(paths)] + for crypt in crypts: + if crypt['id'] is not None: + encrypted_file = self.__download_file(str(crypt['id']).zfill(ZFILL)) + file_hash = get_hash(encrypted_file) + if crypt['sha1sum'] != file_hash: + print("Hash mismatch", str(crypt['id']).zfill(ZFILL)) + self.__delete_file(str(crypt['id']).zfill(ZFILL)) + self.bdd.delete_crypt(crypt['id']) return self def __save(self, path, recurse=True): @@ -141,8 +160,11 @@ class Backup: if self.save_mode == BACKBLAZE: self.buk.download_file_by_name(file).save(dl) elif self.save_mode == LOCAL: - with open(os.path.join(self.save_location, file), 'rb') as infile: - dl.write(infile.read()) + try: + with open(os.path.join(self.save_location, file), 'rb') as infile: + dl.write(infile.read()) + except FileNotFoundError: + print("Fichier", file, "introuvable") return dl def __delete_file(self, file): @@ -320,6 +342,14 @@ class DataBase: crypt_list.append({'path': path, 'crypt': None}) return crypt_list + def get_crypts(self, path): + cursor = self.conn.cursor() + cursor.execute("""SELECT DISTINCT crypt.id, crypt.sha1sum FROM crypt + LEFT JOIN files ON files.crypt_id = crypt.id + WHERE path LIKE ?""", + (path + "%", )) + return cursor.fetchall() + def __get_crypt_id(self, list_file): cursor = self.conn.cursor() crypt_id_list = [] @@ -373,6 +403,13 @@ class DataBase: WHERE files.id IS NULL""") return cursor.fetchall() + def get_file_no_crypt(self): + cursor = self.conn.cursor() + cursor.execute("""SELECT files.id, files.name, files.path FROM files + LEFT JOIN crypt ON files.crypt_id = crypt.id + WHERE crypt.id IS NULL""") + return cursor.fetchall() + def delete_crypt(self, crypt_id): cursor = self.conn.cursor() cursor.execute("""DELETE FROM crypt WHERE id=?""", (crypt_id,)) diff --git a/main.py b/main.py index 149bc10..df41b9c 100644 --- a/main.py +++ b/main.py @@ -14,20 +14,22 @@ else: print(key) -application_key_id = '003aa00745ec42a0000000004' -application_key = 'K003RNvGfy+pazc6pD97xuUzPcDEqS0' -bucket_id = '6a1a9000075465fe7cc4021a' +# application_key_id = '003aa00745ec42a0000000004' +# application_key = 'K003RNvGfy+pazc6pD97xuUzPcDEqS0' +# bucket_id = '6a1a9000075465fe7cc4021a' bdd = "bdd.db" #bck = Backup().backblaze(key, bdd=bdd, app_key_id=application_key_id, app_key=application_key, bucket_id=bucket_id) bck = Backup().local(key, bdd=bdd, save_location="crypted") -bck.save("share", recurse=False) +bck.check("/home/lionel/Images", recurse=False) +bck.clear("/home/lionel/Images", recurse=False) +bck.save("/home/lionel/Images", recurse=False) -rootdir = "test" +# rootdir = "test" # bck.clear(rootdir, recurse=False) # bck.save(rootdir) -bck.update(rootdir) - -bck.recover_file(paths=["test\\tmp597556213336506368.jpg", "share/archive.tar"], save_path="recovered") +# bck.update(rootdir) +# +# bck.recover_file(paths=["test\\tmp597556213336506368.jpg", "share/archive.tar"], save_path="recovered")