Implement DataBase class
This commit is contained in:
parent
5c2d2a22a6
commit
155788cd1a
71
Backup.py
71
Backup.py
@ -1,6 +1,8 @@
|
|||||||
import re
|
import re
|
||||||
import gzip
|
import gzip
|
||||||
|
import sqlite3
|
||||||
from crypt import *
|
from crypt import *
|
||||||
|
import pathlib
|
||||||
|
|
||||||
|
|
||||||
class Backup:
|
class Backup:
|
||||||
@ -8,11 +10,13 @@ class Backup:
|
|||||||
compressMinSize = "50M"
|
compressMinSize = "50M"
|
||||||
save_location = None
|
save_location = None
|
||||||
|
|
||||||
def __init__(self, key):
|
def __init__(self, key, bdd):
|
||||||
self.key = key
|
self.key = key
|
||||||
|
self.bdd = DataBase(bdd)
|
||||||
|
|
||||||
def recurse(self, path):
|
def recurse(self, path):
|
||||||
min_size = parse_size(self.compressMinSize)
|
min_size = parse_size(self.compressMinSize)
|
||||||
|
files = []
|
||||||
for f in os.listdir(path):
|
for f in os.listdir(path):
|
||||||
uri = os.path.join(path, f)
|
uri = os.path.join(path, f)
|
||||||
if os.path.isfile(uri):
|
if os.path.isfile(uri):
|
||||||
@ -22,9 +26,11 @@ class Backup:
|
|||||||
enc = crypt(compress(uri), self.key)
|
enc = crypt(compress(uri), self.key)
|
||||||
else:
|
else:
|
||||||
enc = crypt(uri, self.key)
|
enc = crypt(uri, self.key)
|
||||||
|
files.append({'name': f, 'path': uri})
|
||||||
save(enc, os.path.join(self.save_location, f + ".enc"))
|
save(enc, os.path.join(self.save_location, f + ".enc"))
|
||||||
elif os.path.isdir(uri):
|
elif os.path.isdir(uri):
|
||||||
self.recurse(uri)
|
self.recurse(uri)
|
||||||
|
self.bdd.add(files)
|
||||||
|
|
||||||
|
|
||||||
def compress(file):
|
def compress(file):
|
||||||
@ -65,6 +71,9 @@ def uncrypt(file, key):
|
|||||||
|
|
||||||
|
|
||||||
def save(file, name):
|
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:
|
if type(file) is io.BufferedRandom or tempfile.SpooledTemporaryFile:
|
||||||
file.seek(0)
|
file.seek(0)
|
||||||
with open(name, 'wb') as save:
|
with open(name, 'wb') as save:
|
||||||
@ -75,6 +84,66 @@ def save(file, name):
|
|||||||
return
|
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):
|
def human_size(size, decimal_places=0):
|
||||||
for unit in ['B', 'K', 'M', 'G', 'T']:
|
for unit in ['B', 'K', 'M', 'G', 'T']:
|
||||||
if size < 1024.0:
|
if size < 1024.0:
|
||||||
|
15
main.py
15
main.py
@ -1,6 +1,6 @@
|
|||||||
from Crypto.Random import get_random_bytes
|
from Crypto.Random import get_random_bytes
|
||||||
import os
|
import os
|
||||||
from Backup import Backup
|
from Backup import *
|
||||||
|
|
||||||
if not os.path.exists("key"):
|
if not os.path.exists("key"):
|
||||||
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)
|
||||||
@ -11,8 +11,17 @@ else:
|
|||||||
print("Recovered")
|
print("Recovered")
|
||||||
|
|
||||||
print(key)
|
print(key)
|
||||||
bck = Backup(key)
|
bdd = "bdd.db"
|
||||||
bck.save_location = "C:\\temp"
|
bck = Backup(key, bdd)
|
||||||
|
bck.save_location = "crypted"
|
||||||
|
|
||||||
|
|
||||||
rootdir = "test"
|
rootdir = "test"
|
||||||
bck.recurse(rootdir)
|
bck.recurse(rootdir)
|
||||||
|
|
||||||
|
# base = DataBase(bdd)
|
||||||
|
# file1 = {'name': "testname", 'path': "pathtest"}
|
||||||
|
# file2 = {'name': "secondname", 'path': "secondpath"}
|
||||||
|
# truc = []
|
||||||
|
# truc.append(file1)
|
||||||
|
# base.add(truc)
|
Loading…
x
Reference in New Issue
Block a user