This commit is contained in:
2024-10-03 18:23:21 +08:00
commit 8626664653
45 changed files with 777 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
"""
********************************************
* @Date: 2024 09 27
* @Description: BackupObject备份对象
********************************************
"""
import os
from typing import List, Union
from .tree_object import TreeObject
from .blob_object import BlobObject
class BackupObject(ObjectBase):
"""
备份对象,负责管理存储对象(tree、blob)对象、备份信息、恢复等操作
"""
backup_name: str
backup_time_lately: str
backup_time_create: str
backup_size: int
backup_version_number: int
backup_tree: List[Union[TreeObject, BlobObject]]
new_backup_flag: bool
def __init__(self, backup_name: str, backup_base_path: str):
self.backup_name = backup_name
backup_path = os.path.join(backup_base_path, backup_name)
if not os.path.exists(backup_path):
self.new_backup_flag = True
else:
self.new_backup_flag = False
pass #TODO 读取备份信息
def createNewBackup(self, backup_dirs: List[str]):
"""
创建新的备份
:return
"""
for backup_dir in backup_dirs:
if os.path.isdir(backup_dir):
self.backup_tree.append(TreeObject(backup_dir))
else:
self.backup_tree.append(BlobObject(backup_dir))
def backup(self):
pass
def recover(self, recover_path: str):
"""
恢复到指定备份
:return
"""
pass
def getBackupTree(self):
"""
获取备份树
:return
"""
pass

View File

@@ -0,0 +1,70 @@
"""
********************************************
* @Date: 2024 09 27
* @Description: BlobObject存储对象
********************************************
"""
import hashlib
import os
import zlib
class BlobObject:
object_id: str
__buff_size: int = 8192
def __init__(self, file_path: str) -> None:
self.file_path = file_path
self.object_id = self.contentSha1()
def writeBlob(self, base_path) -> None:
Folder = base_path + "/" + self.__getFolderName()
if not os.path.exists(Folder):
os.makedirs(Folder)
self.__compressFile(
self.file_path,
base_path + "/" + self.__getFolderName() + "/" + self.__getFileName(),
)
def __compressFile(self, file_path, save_path) -> None:
compresser = zlib.compressobj(9)
write_file = open(save_path, "wb")
with open(file_path, "rb") as f:
while True:
data = f.read(self.__buff_size)
if not data:
break
compressedData = compresser.compress(data)
write_file.write(compressedData)
write_file.write(compresser.flush())
write_file.close()
def __getFolderName(self) -> str:
return self.object_id[:2]
def __getFileName(self) -> str:
return self.object_id[2:]
def getBlobAbsPath(self) -> str:
"""
获取blob的绝对路径,此相对路径是基于存储路径的,不是相对当前文件的路径
:return 相对路径,格式为xx/xxxx...
"""
return self.__getFolderName() + "/" + self.__getFileName()
def contentSha1(self) -> str:
"""
计算文件内容的sha1值
:return sha1的hex值
"""
with open(self.file_path, "rb") as f:
sha1 = hashlib.sha1()
while True:
data = f.read(self.__buff_size)
if not data:
break
sha1.update(data)
return sha1.hexdigest()

View File

@@ -0,0 +1,31 @@
"""
********************************************
* @Date: 2024 09 27
* @Description: BackObject树对象,标记多个存储存储对象和树对象,为备份对象提供目标
********************************************
"""
from typing import Dict, List
from .blob_object import BlobObject
import os
class TreeObject:
__children: List[BlobObject] = [] # 备份树节点列表节点为BlobObject备份存储对象
__file_map: Dict[str, str] = {} # 备份文件路径到blob对象的映射
def __init__(self, tree_path: str):
self.tree_path = tree_path
self.__loadChildren()
def __loadChildren(self):
for root, dirs, files in os.walk(self.tree_path):
for name in files:
blob_path = os.path.join(root, name)
blob = BlobObject(blob_path)
self.__children.append(blob)
self.__file_map[blob_path] = blob.object_id
def writeBlobs(self, base_path: str):
for child in self.__children:
child.writeBlob(base_path)