Check hash
This commit is contained in:
parent
22f15b91c8
commit
d821a5e357
37
backup.py
37
backup.py
@ -72,6 +72,25 @@ class Backup:
|
|||||||
# Delete orphan crypt
|
# Delete orphan crypt
|
||||||
self.__delete_file(str(orphan['id']).zfill(ZFILL))
|
self.__delete_file(str(orphan['id']).zfill(ZFILL))
|
||||||
self.bdd.delete_crypt(orphan['id'])
|
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
|
return self
|
||||||
|
|
||||||
def __save(self, path, recurse=True):
|
def __save(self, path, recurse=True):
|
||||||
@ -141,8 +160,11 @@ class Backup:
|
|||||||
if self.save_mode == BACKBLAZE:
|
if self.save_mode == BACKBLAZE:
|
||||||
self.buk.download_file_by_name(file).save(dl)
|
self.buk.download_file_by_name(file).save(dl)
|
||||||
elif self.save_mode == LOCAL:
|
elif self.save_mode == LOCAL:
|
||||||
|
try:
|
||||||
with open(os.path.join(self.save_location, file), 'rb') as infile:
|
with open(os.path.join(self.save_location, file), 'rb') as infile:
|
||||||
dl.write(infile.read())
|
dl.write(infile.read())
|
||||||
|
except FileNotFoundError:
|
||||||
|
print("Fichier", file, "introuvable")
|
||||||
return dl
|
return dl
|
||||||
|
|
||||||
def __delete_file(self, file):
|
def __delete_file(self, file):
|
||||||
@ -320,6 +342,14 @@ class DataBase:
|
|||||||
crypt_list.append({'path': path, 'crypt': None})
|
crypt_list.append({'path': path, 'crypt': None})
|
||||||
return crypt_list
|
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):
|
def __get_crypt_id(self, list_file):
|
||||||
cursor = self.conn.cursor()
|
cursor = self.conn.cursor()
|
||||||
crypt_id_list = []
|
crypt_id_list = []
|
||||||
@ -373,6 +403,13 @@ class DataBase:
|
|||||||
WHERE files.id IS NULL""")
|
WHERE files.id IS NULL""")
|
||||||
return cursor.fetchall()
|
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):
|
def delete_crypt(self, crypt_id):
|
||||||
cursor = self.conn.cursor()
|
cursor = self.conn.cursor()
|
||||||
cursor.execute("""DELETE FROM crypt WHERE id=?""", (crypt_id,))
|
cursor.execute("""DELETE FROM crypt WHERE id=?""", (crypt_id,))
|
||||||
|
18
main.py
18
main.py
@ -14,20 +14,22 @@ else:
|
|||||||
|
|
||||||
print(key)
|
print(key)
|
||||||
|
|
||||||
application_key_id = '003aa00745ec42a0000000004'
|
# application_key_id = '003aa00745ec42a0000000004'
|
||||||
application_key = 'K003RNvGfy+pazc6pD97xuUzPcDEqS0'
|
# application_key = 'K003RNvGfy+pazc6pD97xuUzPcDEqS0'
|
||||||
bucket_id = '6a1a9000075465fe7cc4021a'
|
# bucket_id = '6a1a9000075465fe7cc4021a'
|
||||||
|
|
||||||
bdd = "bdd.db"
|
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().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 = 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.clear(rootdir, recurse=False)
|
||||||
# bck.save(rootdir)
|
# bck.save(rootdir)
|
||||||
bck.update(rootdir)
|
# bck.update(rootdir)
|
||||||
|
#
|
||||||
bck.recover_file(paths=["test\\tmp597556213336506368.jpg", "share/archive.tar"], save_path="recovered")
|
# bck.recover_file(paths=["test\\tmp597556213336506368.jpg", "share/archive.tar"], save_path="recovered")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user