Save and Recover encrypted DB
This commit is contained in:
parent
d821a5e357
commit
b1cc817777
@ -70,10 +70,12 @@ class Backup:
|
|||||||
orphans = self.bdd.get_orphan_crypt()
|
orphans = self.bdd.get_orphan_crypt()
|
||||||
for orphan in orphans:
|
for orphan in orphans:
|
||||||
# Delete orphan crypt
|
# Delete orphan crypt
|
||||||
|
print("Clear deleted crypt :", orphan['id'])
|
||||||
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()
|
nocrypts = self.bdd.get_file_no_crypt()
|
||||||
for nocrypt in nocrypts:
|
for nocrypt in nocrypts:
|
||||||
|
print("Clear deleted file :", nocrypt)
|
||||||
self.bdd.delete_file(nocrypt)
|
self.bdd.delete_file(nocrypt)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@ -364,7 +366,7 @@ class DataBase:
|
|||||||
id = most_common(crypt_id_list)
|
id = most_common(crypt_id_list)
|
||||||
else:
|
else:
|
||||||
# if not already/find in bdd
|
# if not already/find in bdd
|
||||||
cursor.execute("""SELECT IFNULL(max(id) + 1, 0) as crypt_id FROM crypt""")
|
cursor.execute("""SELECT IFNULL(max(id) + 1, 1) as crypt_id FROM crypt""")
|
||||||
return cursor.fetchone()['crypt_id']
|
return cursor.fetchone()['crypt_id']
|
||||||
params = {'id': id,
|
params = {'id': id,
|
||||||
'name': ', '.join([f"'{file['name']}'" for file in list_file]),
|
'name': ', '.join([f"'{file['name']}'" for file in list_file]),
|
||||||
|
24
main.py
24
main.py
@ -2,14 +2,16 @@ from Crypto.Random import get_random_bytes
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
import backup
|
import backup
|
||||||
from backup import Backup
|
from backup import *
|
||||||
|
|
||||||
if not os.path.exists("key"):
|
key_file = "key"
|
||||||
|
|
||||||
|
if not os.path.exists(key_file):
|
||||||
key = get_random_bytes(32) # 32 bytes * 8 = 256 bits (1 byte = 8 bits)
|
key = get_random_bytes(32) # 32 bytes * 8 = 256 bits (1 byte = 8 bits)
|
||||||
open("key", 'wb').write(key)
|
open(key_file, 'wb').write(key)
|
||||||
print("Saved")
|
print("Saved")
|
||||||
else:
|
else:
|
||||||
key = open("key", 'rb').read()
|
key = open(key_file, 'rb').read()
|
||||||
print("Recovered")
|
print("Recovered")
|
||||||
|
|
||||||
print(key)
|
print(key)
|
||||||
@ -18,15 +20,25 @@ print(key)
|
|||||||
# application_key = 'K003RNvGfy+pazc6pD97xuUzPcDEqS0'
|
# application_key = 'K003RNvGfy+pazc6pD97xuUzPcDEqS0'
|
||||||
# bucket_id = '6a1a9000075465fe7cc4021a'
|
# bucket_id = '6a1a9000075465fe7cc4021a'
|
||||||
|
|
||||||
bdd = "bdd.db"
|
|
||||||
|
bdd_location = os.path.abspath("bdd.db")
|
||||||
|
exported_bdd = os.path.join("crypted", "0".zfill(ZFILL))
|
||||||
|
if os.path.exists(exported_bdd):
|
||||||
|
print("Reload BDD from", exported_bdd)
|
||||||
|
backup.save(backup.uncrypt(exported_bdd, key), bdd_location)
|
||||||
|
|
||||||
#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_location, save_location="crypted")
|
||||||
|
|
||||||
bck.check("/home/lionel/Images", recurse=False)
|
bck.check("/home/lionel/Images", recurse=False)
|
||||||
bck.clear("/home/lionel/Images", recurse=False)
|
bck.clear("/home/lionel/Images", recurse=False)
|
||||||
bck.save("/home/lionel/Images", recurse=False)
|
bck.save("/home/lionel/Images", recurse=False)
|
||||||
|
|
||||||
|
exported_bdd = os.path.join("crypted", "0".zfill(ZFILL))
|
||||||
|
print("Save BDD on", exported_bdd)
|
||||||
|
backup.save(backup.crypt(bdd_location, key), exported_bdd)
|
||||||
|
os.remove(bdd_location)
|
||||||
|
|
||||||
# rootdir = "test"
|
# rootdir = "test"
|
||||||
# bck.clear(rootdir, recurse=False)
|
# bck.clear(rootdir, recurse=False)
|
||||||
# bck.save(rootdir)
|
# bck.save(rootdir)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user