hash + set crypt attr
This commit is contained in:
parent
ac6435d17f
commit
4c79d283b0
55
backup.py
55
backup.py
@ -7,8 +7,9 @@ import mgzip as gzip
|
|||||||
import tarfile
|
import tarfile
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import pathlib
|
import pathlib
|
||||||
from b2sdk.v2 import B2Api
|
import hashlib
|
||||||
|
|
||||||
|
from b2sdk.v2 import B2Api
|
||||||
from crypt import encrypt_file, decrypt_file
|
from crypt import encrypt_file, decrypt_file
|
||||||
|
|
||||||
ZFILL = 5
|
ZFILL = 5
|
||||||
@ -56,17 +57,17 @@ class Backup:
|
|||||||
m_date = datetime.fromtimestamp(os.path.getmtime(uri)).strftime("%Y-%m-%d %H:%M:%S.%f")
|
m_date = datetime.fromtimestamp(os.path.getmtime(uri)).strftime("%Y-%m-%d %H:%M:%S.%f")
|
||||||
c_date = datetime.fromtimestamp(os.path.getctime(uri)).strftime("%Y-%m-%d %H:%M:%S.%f")
|
c_date = datetime.fromtimestamp(os.path.getctime(uri)).strftime("%Y-%m-%d %H:%M:%S.%f")
|
||||||
if size > tarball_size:
|
if size > tarball_size:
|
||||||
crypt_name = self.bdd.add([{'name': f,
|
crypt_id = self.bdd.add([{'name': f,
|
||||||
'path': pathlib.Path(uri).as_posix(),
|
'path': pathlib.Path(uri).as_posix(),
|
||||||
'size': size,
|
'size': size,
|
||||||
'm_date': m_date,
|
'm_date': m_date,
|
||||||
'c_date': c_date}],
|
'c_date': c_date}])
|
||||||
compress_mode="gz")
|
if crypt_id is not None:
|
||||||
if crypt_name is not None:
|
print("Proceed", uri, ' ==> ', crypt_id)
|
||||||
print("Proceed", uri)
|
|
||||||
enc = crypt(compress(uri), self.key)
|
enc = crypt(compress(uri), self.key)
|
||||||
|
self.bdd.set_crypt_attr(crypt_id, compress_mode="gz", sha1sum=hashlib.sha1(enc.read()).hexdigest())
|
||||||
print(" Size : ", get_size(enc))
|
print(" Size : ", get_size(enc))
|
||||||
upload_file(enc, self.save_mode, file_name=os.path.join(self.save_location, crypt_name), bucket=self.buk)
|
upload_file(enc, self.save_mode, file_name=os.path.join(self.save_location, str(crypt_id).zfill(ZFILL)), bucket=self.buk)
|
||||||
else:
|
else:
|
||||||
files.append({'name': f,
|
files.append({'name': f,
|
||||||
'path': pathlib.Path(uri).as_posix(),
|
'path': pathlib.Path(uri).as_posix(),
|
||||||
@ -76,19 +77,20 @@ class Backup:
|
|||||||
elif os.path.isdir(uri) and recurse:
|
elif os.path.isdir(uri) and recurse:
|
||||||
self.__save(uri, recurse=recurse)
|
self.__save(uri, recurse=recurse)
|
||||||
if len(files) > 0:
|
if len(files) > 0:
|
||||||
crypt_name = self.bdd.add(files, compress_mode="tar.gz")
|
crypt_id = self.bdd.add(files)
|
||||||
if crypt_name is not None:
|
if crypt_id is not None:
|
||||||
print("Proceed", path, ":", [file['name'] for file in files])
|
print("Proceed", path, ":", [file['name'] for file in files], ' ==> ', crypt_id)
|
||||||
tarball = tar([file['path'] for file in files])
|
tarball = tar([file['path'] for file in files])
|
||||||
enc = crypt(compress(tarball), self.key)
|
enc = crypt(compress(tarball), self.key)
|
||||||
|
self.bdd.set_crypt_attr(crypt_id, compress_mode="tar.gz", sha1sum=hashlib.sha1(enc.read()).hexdigest())
|
||||||
print(" Size : ", get_size(enc))
|
print(" Size : ", get_size(enc))
|
||||||
upload_file(enc, self.save_mode, file_name=os.path.join(self.save_location, crypt_name), bucket=self.buk)
|
upload_file(enc, self.save_mode, file_name=os.path.join(self.save_location, str(crypt_id).zfill(ZFILL)), bucket=self.buk)
|
||||||
|
|
||||||
def recover_file(self, paths, parents=False, save_path=os.getcwd()):
|
def recover_file(self, paths, parents=False, save_path=os.getcwd()):
|
||||||
files = self.bdd.get_crypt_name(paths)
|
files = self.bdd.get_crypt_name(paths)
|
||||||
for file in files:
|
for file in files:
|
||||||
if file['crypt'] is not None:
|
if file['crypt_id'] is not None:
|
||||||
encrypted_file = download_file(self.save_mode, os.path.join(self.save_location, file['crypt']), bucket=self.buk)
|
encrypted_file = download_file(self.save_mode, os.path.join(self.save_location, str(file['crypt_id']).zfill(ZFILL)), bucket=self.buk)
|
||||||
|
|
||||||
if parents:
|
if parents:
|
||||||
save_path = os.path.join(save_path, file['path'])
|
save_path = os.path.join(save_path, file['path'])
|
||||||
@ -236,7 +238,8 @@ class DataBase:
|
|||||||
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,
|
||||||
compress_mode TEXT
|
compress_mode TEXT,
|
||||||
|
sha1sum TEXT
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@ -258,7 +261,7 @@ class DataBase:
|
|||||||
try:
|
try:
|
||||||
crypt_list.append({'name': os.path.basename(path),
|
crypt_list.append({'name': os.path.basename(path),
|
||||||
'path': path,
|
'path': path,
|
||||||
'crypt': str(retval['crypt_id']).zfill(ZFILL),
|
'crypt_id': retval['crypt_id'],
|
||||||
'compress_mode': retval['compress_mode']})
|
'compress_mode': retval['compress_mode']})
|
||||||
except TypeError:
|
except TypeError:
|
||||||
crypt_list.append({'path': path, 'crypt': None})
|
crypt_list.append({'path': path, 'crypt': None})
|
||||||
@ -275,6 +278,8 @@ class DataBase:
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
|
if len(list(set(crypt_id_list))) == 1:
|
||||||
|
return crypt_id_list[0]
|
||||||
id = most_frequent(crypt_id_list)
|
id = most_frequent(crypt_id_list)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
cursor.execute("""SELECT IFNULL(max(id) + 1, 0) as crypt_id FROM crypt""")
|
cursor.execute("""SELECT IFNULL(max(id) + 1, 0) as crypt_id FROM crypt""")
|
||||||
@ -283,14 +288,17 @@ class DataBase:
|
|||||||
'name': ', '.join([f"'{file['name']}'" for file in list_file]),
|
'name': ', '.join([f"'{file['name']}'" for file in list_file]),
|
||||||
'path': ', '.join([f"'{file['path']}'" for file in list_file])}
|
'path': ', '.join([f"'{file['path']}'" for file in list_file])}
|
||||||
cursor.execute("""SELECT 1 FROM files
|
cursor.execute("""SELECT 1 FROM files
|
||||||
WHERE crypt_id={id}
|
WHERE crypt_id={id}
|
||||||
AND name NOT IN ({name})
|
AND name NOT IN ({name})
|
||||||
AND path NOT IN ({path})""".format(**params))
|
AND path NOT IN ({path})""".format(**params))
|
||||||
neighbour = cursor.fetchall()
|
neighbour = cursor.fetchall()
|
||||||
if len(neighbour) > 0:
|
if len(neighbour) > 0:
|
||||||
cursor.execute("""SELECT IFNULL(max(id) + 1, 0) as crypt_id FROM crypt""")
|
cursor.execute("""SELECT IFNULL(max(id) + 1, 0) as crypt_id FROM crypt""")
|
||||||
return cursor.fetchone()['crypt_id']
|
return cursor.fetchone()['crypt_id']
|
||||||
else:
|
else:
|
||||||
|
cursor.execute("""UPDATE files SET crypt_id={id}
|
||||||
|
WHERE name IN ({name})
|
||||||
|
AND path IN ({path})""".format(**params))
|
||||||
return id
|
return id
|
||||||
|
|
||||||
def exist(self, file):
|
def exist(self, file):
|
||||||
@ -312,7 +320,12 @@ class DataBase:
|
|||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def add(self, list_file, compress_mode=None):
|
def set_crypt_attr(self, crypt_id, compress_mode=None, sha1sum=None):
|
||||||
|
cursor = self.conn.cursor()
|
||||||
|
cursor.execute("""UPDATE crypt SET compress_mode='{mode}', sha1sum='{sum}' WHERE id='{id}'""".format(id=crypt_id, mode=compress_mode, sum=sha1sum))
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
|
def add(self, list_file):
|
||||||
cursor = self.conn.cursor()
|
cursor = self.conn.cursor()
|
||||||
crypt_id = self.__get_crypt_id(list_file)
|
crypt_id = self.__get_crypt_id(list_file)
|
||||||
cursor.execute("""SELECT IFNULL(max(id) + 1, 0) as files_id FROM files""")
|
cursor.execute("""SELECT IFNULL(max(id) + 1, 0) as files_id FROM files""")
|
||||||
@ -330,9 +343,9 @@ class DataBase:
|
|||||||
file_id += 1
|
file_id += 1
|
||||||
proceed = True
|
proceed = True
|
||||||
if proceed:
|
if proceed:
|
||||||
cursor.execute("""INSERT OR IGNORE INTO crypt VALUES(?, ?)""", (crypt_id, compress_mode))
|
cursor.execute("""INSERT OR IGNORE INTO crypt (id) VALUES(?)""", (crypt_id,))
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
return str(crypt_id).zfill(ZFILL)
|
return crypt_id
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user