mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2025-01-18 17:15:13 +01:00
Setup new backup manager
ToDo: Add legacy backups to new manager to make them more module
This commit is contained in:
parent
a4256b66e8
commit
bd55cdce16
@ -2,20 +2,24 @@ import base64
|
|||||||
import hashlib
|
import hashlib
|
||||||
import pathlib
|
import pathlib
|
||||||
import shutil
|
import shutil
|
||||||
import uuid
|
|
||||||
import zlib
|
import zlib
|
||||||
|
import datetime
|
||||||
|
|
||||||
from app.classes.shared.crypto_helper import CryptoHelper
|
from app.classes.shared.crypto_helper import CryptoHelper
|
||||||
|
|
||||||
|
# Set byte constants
|
||||||
|
BYTE_FALSE = bytes.fromhex("00")
|
||||||
|
BYTE_TRUE = bytes.fromhex("01")
|
||||||
|
|
||||||
|
|
||||||
class BackupManager:
|
class BackupManager:
|
||||||
def __init__(self, server_instance):
|
def __init__(self, server_instance):
|
||||||
self.server_instance = server_instance
|
self.server_instance = server_instance
|
||||||
self.crypto_helper = CryptoHelper()
|
self.crypto_helper = CryptoHelper()
|
||||||
|
|
||||||
# Set byte constants
|
####################################################################################
|
||||||
BYTE_FALSE = bytes.fromhex("00")
|
########################## SNAPSHOT METHODS ######################################
|
||||||
BYTE_TRUE = bytes.fromhex("01")
|
####################################################################################
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def blake2_hash_bytes(bytes_to_hash: bytes) -> bytes:
|
def blake2_hash_bytes(bytes_to_hash: bytes) -> bytes:
|
||||||
@ -211,12 +215,12 @@ class BackupManager:
|
|||||||
# Compress bytes if set to true
|
# Compress bytes if set to true
|
||||||
if use_compression:
|
if use_compression:
|
||||||
file_chunk = self.compress_bytes(file_chunk)
|
file_chunk = self.compress_bytes(file_chunk)
|
||||||
compression = self.BYTE_TRUE
|
compression = BYTE_TRUE
|
||||||
else:
|
else:
|
||||||
compression = self.BYTE_FALSE
|
compression = BYTE_FALSE
|
||||||
|
|
||||||
# Placeholder for encryption
|
# Placeholder for encryption
|
||||||
encryption = self.BYTE_FALSE
|
encryption = BYTE_FALSE
|
||||||
nonce = bytes.fromhex("000000000000000000000000")
|
nonce = bytes.fromhex("000000000000000000000000")
|
||||||
|
|
||||||
# Create Chunk
|
# Create Chunk
|
||||||
@ -321,16 +325,21 @@ class BackupManager:
|
|||||||
"""
|
"""
|
||||||
return self.b64_to_bytes(input_b64).decode("utf-8")
|
return self.b64_to_bytes(input_b64).decode("utf-8")
|
||||||
|
|
||||||
def backup(self) -> uuid.UUID:
|
def backup(self, conf: dict) -> None:
|
||||||
"""
|
"""
|
||||||
Perform the backup.
|
Perform the backup.
|
||||||
Iterate over files in source dir. Apply save function. Save file information to manifest.
|
Iterate over files in source dir. Apply save function.
|
||||||
|
Save file information to manifest.
|
||||||
:return: UUID of backup.
|
:return: UUID of backup.
|
||||||
"""
|
"""
|
||||||
# Initialize backup stuff.
|
# Initialize backup stuff.\
|
||||||
backup_id = uuid.uuid4()
|
backup_id = str(
|
||||||
source_path = pathlib.Path(__file__).parent / "source_files"
|
datetime.datetime.now()
|
||||||
repo_path = pathlib.Path(__file__).parent / "backup_repository"
|
.astimezone(self.server_instance.tz)
|
||||||
|
.strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
|
)
|
||||||
|
source_path = pathlib.Path(self.server_instance.server_path)
|
||||||
|
repo_path = pathlib.Path(conf["backup_location"]) / "snapshots"
|
||||||
manifest_path = repo_path / "manifests" / f"{str(backup_id)}.manifest"
|
manifest_path = repo_path / "manifests" / f"{str(backup_id)}.manifest"
|
||||||
|
|
||||||
# Get list of files to backup.
|
# Get list of files to backup.
|
||||||
@ -373,7 +382,6 @@ class BackupManager:
|
|||||||
|
|
||||||
# Backup complete
|
# Backup complete
|
||||||
manifest_file.close()
|
manifest_file.close()
|
||||||
return backup_id
|
|
||||||
|
|
||||||
def recover(
|
def recover(
|
||||||
self,
|
self,
|
||||||
@ -505,10 +513,14 @@ class BackupManager:
|
|||||||
# Read use compression byte
|
# Read use compression byte
|
||||||
use_compression_byte = chunk_file.read(1)
|
use_compression_byte = chunk_file.read(1)
|
||||||
|
|
||||||
if use_compression_byte == self.BYTE_TRUE:
|
if use_compression_byte == BYTE_TRUE:
|
||||||
try:
|
try:
|
||||||
return self.decompress_bytes(chunk_file.read())
|
return self.decompress_bytes(chunk_file.read())
|
||||||
except zlib.error as why:
|
except zlib.error as why:
|
||||||
raise RuntimeError(f"Unable to decompress file {chunk_path}.") from why
|
raise RuntimeError(f"Unable to decompress file {chunk_path}.") from why
|
||||||
else:
|
else:
|
||||||
return chunk_file.read()
|
return chunk_file.read()
|
||||||
|
|
||||||
|
####################################################################################
|
||||||
|
########################## LEGACY BACKUP METHODS #################################
|
||||||
|
####################################################################################
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
class CryptoHelper:
|
class CryptoHelper:
|
||||||
print("hi")
|
def __init__(self):
|
||||||
|
return
|
||||||
|
@ -1185,6 +1185,10 @@ class ServerInstance:
|
|||||||
|
|
||||||
self.helper.ensure_dir_exists(backup_location)
|
self.helper.ensure_dir_exists(backup_location)
|
||||||
|
|
||||||
|
if conf["snapshot"]:
|
||||||
|
self.backup_manager.backup(conf)
|
||||||
|
else:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
backup_filename = (
|
backup_filename = (
|
||||||
f"{backup_location}/"
|
f"{backup_location}/"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user