Tarball and database insert

- Tarball for multiple files smaller than tarball_size
- Delete Table content (useless)
This commit is contained in:
toto 2021-10-01 14:36:00 +02:00
parent 155788cd1a
commit 1776d7f15c

View File

@ -1,5 +1,6 @@
import re import re
import gzip import gzip
import tarfile
import sqlite3 import sqlite3
from crypt import * from crypt import *
import pathlib import pathlib
@ -7,7 +8,7 @@ import pathlib
class Backup: class Backup:
key = None key = None
compressMinSize = "50M" tarball_size = "50M"
save_location = None save_location = None
def __init__(self, key, bdd): def __init__(self, key, bdd):
@ -15,22 +16,47 @@ class Backup:
self.bdd = DataBase(bdd) self.bdd = DataBase(bdd)
def recurse(self, path): def recurse(self, path):
min_size = parse_size(self.compressMinSize) tarball_size = parse_size(self.tarball_size)
files = [] files = []
print("Start", path, ":", 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):
size = os.path.getsize(uri) size = os.path.getsize(uri)
print(f + " : ", human_size(size)) print(f + " : ", human_size(size))
if size > min_size: if size > tarball_size:
enc = crypt(compress(uri), self.key) enc = crypt(compress(uri), self.key)
crypt_name = self.bdd.add([{'name': f, 'path': uri}], compress_mode="gz")
save(enc, os.path.join(self.save_location, crypt_name))
else: else:
enc = crypt(uri, self.key)
files.append({'name': f, 'path': uri}) files.append({'name': f, 'path': uri})
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) if len(files) > 0:
print("End", path, ":", files)
crypt_name = self.bdd.add(files, compress_mode="tar.gz")
tarball = tar(files)
enc = crypt(compress(tarball), self.key)
save(enc, os.path.join(self.save_location, crypt_name))
def tar(files):
tarball = tempfile.SpooledTemporaryFile()
with tarfile.open(fileobj=tarball, mode='w') as zipfile:
for file in files:
zipfile.add(file['path'])
return tarball
def untar(tar_file, files, save_path):
if type(tar_file) is io.BufferedRandom or tempfile.SpooledTemporaryFile:
tar_file.seek(0)
zipfile = tarfile.open(fileobj=tar_file, mode='r')
else:
zipfile = tarfile.open(tar_file, 'r')
for file in files:
zipfile.extract(file['path'], path=save_path)
zipfile.close()
def compress(file): def compress(file):
@ -70,13 +96,13 @@ def uncrypt(file, key):
return decrypted_file return decrypted_file
def save(file, name): def save(file, save_path):
if not os.path.isdir(os.path.dirname(name)): if not os.path.isdir(os.path.dirname(save_path)):
os.mkdir(os.path.dirname(name)) os.mkdir(os.path.dirname(save_path))
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(save_path, 'wb') as save:
while chunk := file.read(64 * 1024): while chunk := file.read(64 * 1024):
save.write(chunk) save.write(chunk)
else: else:
@ -101,47 +127,34 @@ class DataBase:
CREATE TABLE IF NOT EXISTS files( CREATE TABLE IF NOT EXISTS files(
id INTEGER PRIMARY KEY UNIQUE NOT NULL, id INTEGER PRIMARY KEY UNIQUE NOT NULL,
name TEXT, name TEXT,
path TEXT path TEXT,
crypt_id INTEGER,
CONSTRAINT files_crypt_FK FOREIGN KEY (crypt_id) REFERENCES crypt(id)
) )
""") """)
cursor.execute(""" cursor.execute("""
CREATE TABLE IF NOT EXISTS crypt( CREATE TABLE IF NOT EXISTS crypt(
id INTEGER PRIMARY KEY UNIQUE NOT NULL, id INTEGER PRIMARY KEY UNIQUE NOT NULL,
compressed INTEGER compress_mode TEXT
)
""")
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() self.conn.commit()
def add(self, list_file, compressed=False): def add(self, list_file, compress_mode=None):
isdir = True if len(list_file) > 0 else False
cursor = self.conn.cursor() cursor = self.conn.cursor()
cursor.execute("""SELECT IFNULL(max(id), 0) FROM crypt""") cursor.execute("""SELECT IFNULL(max(id) + 1, 0) FROM crypt""")
crypt_id = cursor.fetchone()[0] crypt_id = cursor.fetchone()[0]
cursor.execute("""SELECT IFNULL(max(id), 0) FROM content""") cursor.execute("""SELECT IFNULL(max(id) + 1, 0) FROM files""")
content_id = cursor.fetchone()[0]
cursor.execute("""SELECT IFNULL(max(id), 0) FROM files""")
files_id = cursor.fetchone()[0] files_id = cursor.fetchone()[0]
for file in list_file: for file in list_file:
cursor.execute("""INSERT INTO files VALUES(?, ?, ?)""", (files_id, file['name'], file['path'])) cursor.execute("""INSERT INTO files VALUES(?, ?, ?, ?)""", (files_id, file['name'], file['path'], crypt_id))
files_id += 1 files_id += 1
cursor.execute("""INSERT INTO crypt VALUES(?, ?)""", (crypt_id, compressed)) cursor.execute("""INSERT INTO crypt VALUES(?, ?)""", (crypt_id, compress_mode))
cursor.execute("""INSERT INTO content VALUES(?, ?, ?, ?)""", (content_id, files_id, crypt_id, isdir)) self.conn.commit()
return str(crypt_id)
return crypt_id
def human_size(size, decimal_places=0): def human_size(size, decimal_places=0):