Files
ThousandHands/core/objects/backup_object.py
2024-11-12 20:30:19 +08:00

94 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
********************************************
* @Date: 2024 09 27
* @Description: BackupObject备份对象
********************************************
"""
import os
import asyncio
from typing import List, Union
from .tree_object import TreeObject
from .blob_object import BlobObject
from utils.log import Logger
logger = Logger("BackupObject")
class BackupObject:
"""
备份对象,负责管理存储对象(tree、blob)对象、备份信息、恢复等操作
"""
backup_name: str
backup_time_lately: str
backup_time_create: str
backup_size: int
backup_version_number: int
backup_trees: List[Union[TreeObject, BlobObject]] = []
new_backup_flag: bool
def __init__(self, backup_name: str, backup_base_path: str):
self.backup_name = backup_name
# 获取备份路径的绝对路径
if os.path.isabs(backup_base_path):
self.backup_path = os.path.join(backup_base_path, backup_name)
else:
self.backup_path = os.path.join(
os.path.abspath(backup_base_path), backup_name
)
logger.debug(f"Backup path: {self.backup_path}")
if not os.path.exists(self.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_trees.append(TreeObject(backup_dir))
else:
self.backup_trees.append(BlobObject(backup_dir))
logger.info("New backup created successfully.")
logger.debug(f"Backup trees: {self.backup_trees}")
def backup(self):
asyncio.run(self.__writeBlobs())
async def __writeBlobs(self):
"""
写入所有对象到备份路径
"""
obj_save_path = os.path.join(self.backup_path, "objects")
for obj in self.backup_trees:
if isinstance(obj, TreeObject):
await obj.writeBlobs(obj_save_path) # 如果是TreeObject则调用writeBlobs方法
else:
await obj.writeBlob(obj_save_path) # 如果是BlobObject则调用writeBlob方法
def recover(self, recover_path: str):
"""
恢复到指定备份
:return
"""
pass
def getBackupTree(self):
"""
获取备份树
:return
"""
pass