Tarball and database insert
- Tarball for multiple files smaller than tarball_size - Delete Table content (useless)
This commit is contained in:
parent
155788cd1a
commit
1776d7f15c
83
Backup.py
83
Backup.py
@ -1,5 +1,6 @@
|
||||
import re
|
||||
import gzip
|
||||
import tarfile
|
||||
import sqlite3
|
||||
from crypt import *
|
||||
import pathlib
|
||||
@ -7,7 +8,7 @@ import pathlib
|
||||
|
||||
class Backup:
|
||||
key = None
|
||||
compressMinSize = "50M"
|
||||
tarball_size = "50M"
|
||||
save_location = None
|
||||
|
||||
def __init__(self, key, bdd):
|
||||
@ -15,22 +16,47 @@ class Backup:
|
||||
self.bdd = DataBase(bdd)
|
||||
|
||||
def recurse(self, path):
|
||||
min_size = parse_size(self.compressMinSize)
|
||||
tarball_size = parse_size(self.tarball_size)
|
||||
files = []
|
||||
print("Start", path, ":", files)
|
||||
for f in os.listdir(path):
|
||||
uri = os.path.join(path, f)
|
||||
if os.path.isfile(uri):
|
||||
size = os.path.getsize(uri)
|
||||
print(f + " : ", human_size(size))
|
||||
if size > min_size:
|
||||
if size > tarball_size:
|
||||
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:
|
||||
enc = crypt(uri, self.key)
|
||||
files.append({'name': f, 'path': uri})
|
||||
save(enc, os.path.join(self.save_location, f + ".enc"))
|
||||
files.append({'name': f, 'path': uri})
|
||||
elif os.path.isdir(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):
|
||||
@ -70,13 +96,13 @@ def uncrypt(file, key):
|
||||
return decrypted_file
|
||||
|
||||
|
||||
def save(file, name):
|
||||
if not os.path.isdir(os.path.dirname(name)):
|
||||
os.mkdir(os.path.dirname(name))
|
||||
def save(file, save_path):
|
||||
if not os.path.isdir(os.path.dirname(save_path)):
|
||||
os.mkdir(os.path.dirname(save_path))
|
||||
|
||||
if type(file) is io.BufferedRandom or tempfile.SpooledTemporaryFile:
|
||||
file.seek(0)
|
||||
with open(name, 'wb') as save:
|
||||
with open(save_path, 'wb') as save:
|
||||
while chunk := file.read(64 * 1024):
|
||||
save.write(chunk)
|
||||
else:
|
||||
@ -101,47 +127,34 @@ class DataBase:
|
||||
CREATE TABLE IF NOT EXISTS files(
|
||||
id INTEGER PRIMARY KEY UNIQUE NOT NULL,
|
||||
name TEXT,
|
||||
path TEXT
|
||||
path TEXT,
|
||||
crypt_id INTEGER,
|
||||
CONSTRAINT files_crypt_FK FOREIGN KEY (crypt_id) REFERENCES crypt(id)
|
||||
)
|
||||
""")
|
||||
|
||||
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)
|
||||
compress_mode TEXT
|
||||
)
|
||||
""")
|
||||
|
||||
self.conn.commit()
|
||||
|
||||
def add(self, list_file, compressed=False):
|
||||
isdir = True if len(list_file) > 0 else False
|
||||
def add(self, list_file, compress_mode=None):
|
||||
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]
|
||||
cursor.execute("""SELECT IFNULL(max(id), 0) FROM content""")
|
||||
content_id = cursor.fetchone()[0]
|
||||
cursor.execute("""SELECT IFNULL(max(id), 0) FROM files""")
|
||||
cursor.execute("""SELECT IFNULL(max(id) + 1, 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']))
|
||||
cursor.execute("""INSERT INTO files VALUES(?, ?, ?, ?)""", (files_id, file['name'], file['path'], crypt_id))
|
||||
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
|
||||
cursor.execute("""INSERT INTO crypt VALUES(?, ?)""", (crypt_id, compress_mode))
|
||||
self.conn.commit()
|
||||
return str(crypt_id)
|
||||
|
||||
|
||||
def human_size(size, decimal_places=0):
|
||||
|
Loading…
x
Reference in New Issue
Block a user