Added working backup system

This commit is contained in:
Jef Roosens 2021-01-15 21:08:56 +01:00
parent bb33f7cbbc
commit 44764d30d9
7 changed files with 72 additions and 28 deletions

View file

@ -10,14 +10,9 @@ class DirectorySpec(Spec):
A spec for backing up a local directory.
"""
__SKEL = {
"name": None,
_SKEL = {
"source": None,
"destination": None,
"limit": None,
"notifier": None,
"command": "tar -czf '{destination}/{filename}' .",
"extension": "tar.gz",
}
def __init__(
@ -28,9 +23,9 @@ class DirectorySpec(Spec):
limit: int,
command: str,
extension: str,
notifier=None,
notify=None,
):
super().__init__(name, destination, limit, extension, notifier)
super().__init__(name, destination, limit, extension, notify)
self.source = source if type(source) == Path else Path(source)
@ -58,6 +53,7 @@ class DirectorySpec(Spec):
filename=filename,
),
cwd=self.source,
shell=True,
)
if self.notifier:

View file

@ -3,6 +3,8 @@ from pathlib import Path
from typing import Union, Dict
import skeleton
import os
from notifier import Notifier
import inspect
class Spec:
@ -10,11 +12,13 @@ class Spec:
Base class for all other spec types.
"""
__SKEL = {
"name": None,
_SKEL = {
"destination": None,
"limit": None,
"notifier": None,
"notify": {
"title": "Backup Notification",
"events": ["backup_sucess"],
},
"extension": "tar.gz",
}
@ -24,7 +28,7 @@ class Spec:
destination: Union[Path, str],
limit: int,
extension: str,
notifier=None,
notify=None,
):
"""
Args:
@ -48,9 +52,15 @@ class Spec:
)
self.limit = limit
self.notifier = notifier
self.notifier = Notifier(*notify) if notify else None
self.extension = extension
@classmethod
def skeleton(cls):
return skeleton.merge(
*[val._SKEL for val in reversed(inspect.getmro(cls)[:-1])]
)
def remove_backups(self):
"""
Remove all backups exceeding the limit
@ -73,14 +83,15 @@ class Spec:
raise NotImplementedError()
@classmethod
def from_dict(cls, name, obj: Dict, *defaults: Dict) -> Spec:
def from_dict(cls, name, obj: Dict, defaults: Dict) -> Spec:
# Combine defaults with skeleton, creating new skeleton
skel = cls.__SKEL
for default in defaults:
skel = skeleton.combine(defaults, skel)
skel = skeleton.merge(cls.skeleton(), defaults)
print(skel)
# Then, combine actual values with new skeleton
obj = skeleton.combine(obj, skel)
obj = skeleton.merge_with_skeleton(obj, skel)
return cls(name, **obj)
def to_dict(self):
raise NotImplementedError()