diff --git a/Backup.py b/Backup.py index 5dadbee..07255eb 100644 --- a/Backup.py +++ b/Backup.py @@ -1,6 +1,8 @@ import re import gzip +import sqlite3 from crypt import * +import pathlib class Backup: @@ -8,11 +10,13 @@ class Backup: compressMinSize = "50M" save_location = None - def __init__(self, key): + def __init__(self, key, bdd): self.key = key + self.bdd = DataBase(bdd) def recurse(self, path): min_size = parse_size(self.compressMinSize) + files = [] for f in os.listdir(path): uri = os.path.join(path, f) if os.path.isfile(uri): @@ -22,9 +26,11 @@ class Backup: enc = crypt(compress(uri), self.key) else: enc = crypt(uri, self.key) + files.append({'name': f, 'path': uri}) save(enc, os.path.join(self.save_location, f + ".enc")) elif os.path.isdir(uri): self.recurse(uri) + self.bdd.add(files) def compress(file): @@ -65,6 +71,9 @@ def uncrypt(file, key): def save(file, name): + if not os.path.isdir(os.path.dirname(name)): + os.mkdir(os.path.dirname(name)) + if type(file) is io.BufferedRandom or tempfile.SpooledTemporaryFile: file.seek(0) with open(name, 'wb') as save: @@ -75,6 +84,66 @@ def save(file, name): return +class DataBase: + + def __init__(self, base_file): + self.conn = sqlite3.connect(base_file) + self.__create_table() + + def __del__(self): + self.conn.commit() + self.conn.close() + + def __create_table(self): + cursor = self.conn.cursor() + + cursor.execute(""" + CREATE TABLE IF NOT EXISTS files( + id INTEGER PRIMARY KEY UNIQUE NOT NULL, + name TEXT, + path TEXT + ) + """) + + cursor.execute(""" + CREATE TABLE IF NOT EXISTS crypt( + id INTEGER PRIMARY KEY UNIQUE NOT NULL, + compressed INTEGER + ) + """) + + cursor.execute(""" + CREATE TABLE IF NOT EXISTS content( + id INTEGER PRIMARY KEY UNIQUE NOT NULL, + files_id INTEGER, + crypt_id INTEGER, + isdir INTEGER, + CONSTRAINT content_files_FK FOREIGN KEY (files_id) REFERENCES files(id), + CONSTRAINT content_crypt_FK FOREIGN KEY (crypt_id) REFERENCES crypt(id) + ) + """) + + self.conn.commit() + + def add(self, list_file, compressed=False): + isdir = True if len(list_file) > 0 else False + cursor = self.conn.cursor() + cursor.execute("""SELECT IFNULL(max(id), 0) FROM crypt""") + crypt_id = cursor.fetchone()[0] + cursor.execute("""SELECT IFNULL(max(id), 0) FROM content""") + content_id = cursor.fetchone()[0] + cursor.execute("""SELECT IFNULL(max(id), 0) FROM files""") + files_id = cursor.fetchone()[0] + for file in list_file: + cursor.execute("""INSERT INTO files VALUES(?, ?, ?)""", (files_id, file['name'], file['path'])) + files_id += 1 + + cursor.execute("""INSERT INTO crypt VALUES(?, ?)""", (crypt_id, compressed)) + cursor.execute("""INSERT INTO content VALUES(?, ?, ?, ?)""", (content_id, files_id, crypt_id, isdir)) + + return crypt_id + + def human_size(size, decimal_places=0): for unit in ['B', 'K', 'M', 'G', 'T']: if size < 1024.0: diff --git a/main.py b/main.py index 7f10151..d9e853a 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ from Crypto.Random import get_random_bytes import os -from Backup import Backup +from Backup import * if not os.path.exists("key"): key = get_random_bytes(32) # 32 bytes * 8 = 256 bits (1 byte = 8 bits) @@ -11,8 +11,17 @@ else: print("Recovered") print(key) -bck = Backup(key) -bck.save_location = "C:\\temp" +bdd = "bdd.db" +bck = Backup(key, bdd) +bck.save_location = "crypted" + rootdir = "test" bck.recurse(rootdir) + +# base = DataBase(bdd) +# file1 = {'name': "testname", 'path': "pathtest"} +# file2 = {'name': "secondname", 'path': "secondpath"} +# truc = [] +# truc.append(file1) +# base.add(truc) \ No newline at end of file