2020-04-16 19:19:21 +00:00
|
|
|
import asyncio
|
2020-04-16 17:37:16 +00:00
|
|
|
from .. import utils
|
|
|
|
|
|
|
|
|
|
|
|
class Backup():
|
|
|
|
name:str
|
|
|
|
periode:int
|
|
|
|
blocked:set
|
|
|
|
|
2020-04-16 19:19:21 +00:00
|
|
|
to_backup:list
|
|
|
|
|
|
|
|
def __init__(self, name:str, periode:int=None, blocked:list=[], to_backup:list=[]):
|
2020-04-16 17:37:16 +00:00
|
|
|
# Check args
|
|
|
|
utils.valid_name_check(name)
|
|
|
|
if periode is not None and not isinstance(periode, int):
|
|
|
|
raise TypeError("Periode have to be an integer or null.")
|
|
|
|
if periode is not None and periode < 0:
|
|
|
|
raise ValueError("periode can't be negetive.")
|
|
|
|
blocked = set(blocked)
|
|
|
|
for i in blocked:
|
|
|
|
utils.valid_name_check(i)
|
2020-04-16 19:19:21 +00:00
|
|
|
to_backup = set(to_backup)
|
|
|
|
for i in to_backup:
|
|
|
|
if not isinstance(i, str):
|
|
|
|
raise TypeError("to_backup have to be a string.")
|
|
|
|
if ":" in i:
|
|
|
|
raise ValueError(": isn't allowed a char.")
|
2020-04-16 17:37:16 +00:00
|
|
|
|
|
|
|
# Set values
|
|
|
|
self.name = name
|
|
|
|
self.periode = periode
|
|
|
|
self.blocks = blocked
|
2020-04-16 19:19:21 +00:00
|
|
|
self.to_backup = to_backup
|
2020-04-16 17:37:16 +00:00
|
|
|
|
|
|
|
def get_next_scedule(self, latest, zero):
|
|
|
|
if self.periode is not None:
|
|
|
|
tmp = (latest - zero) // self.periode
|
|
|
|
return zero + self.periode * (tmp + 1)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("No implemented types.")
|
|
|
|
|
|
|
|
def dump_config(self):
|
|
|
|
result = {}
|
|
|
|
if self.blocked:
|
|
|
|
result["blocked"] = ",".join(self.blocked)
|
|
|
|
if self.periode is not None:
|
|
|
|
result["periode"] = str(self.periode)
|
2020-04-16 19:19:21 +00:00
|
|
|
if self.to_backup:
|
|
|
|
result["to_backup"] = ":".join(self.to_backup)
|
2020-04-16 17:37:16 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load_backup(name:str, config):
|
|
|
|
# Load informations
|
|
|
|
config = dict(config.items())
|
|
|
|
|
|
|
|
periode = None
|
|
|
|
if "periode" in config:
|
|
|
|
periode = int(config["periode"])
|
|
|
|
del config["periode"]
|
|
|
|
|
|
|
|
blocked = []
|
|
|
|
if "blocked" in config:
|
|
|
|
blocked = config["blocked"].split(",")
|
|
|
|
del config["blocked"]
|
2020-04-16 19:19:21 +00:00
|
|
|
|
|
|
|
to_backup = []
|
|
|
|
if "to_backup" in config:
|
|
|
|
to_backup = config["to_backup"].split(":")
|
|
|
|
del config["to_backup"]
|
2020-04-16 17:37:16 +00:00
|
|
|
|
|
|
|
# Generate backup
|
|
|
|
utils.check_empty_data_dict(config)
|
|
|
|
|
2020-04-16 19:19:21 +00:00
|
|
|
return Backup(name=name, periode=periode, blocked=blocked, to_backup=to_backup)
|
|
|
|
|
|
|
|
async def run_backup(self, subvolumes:list):
|
|
|
|
print("Subvolumes: %s" % repr(subvolumes))
|