diff --git a/backive/backive_service b/backive/backive_service old mode 100644 new mode 100755 index d71e5d4..ff0b177 --- a/backive/backive_service +++ b/backive/backive_service @@ -7,16 +7,16 @@ import sys import os import asyncio import logging +import json from backive.core.events import EventInterface +from backive.core.scheduler import Scheduler from backive.config.config import Config rootlogger = logging.getLogger() rootlogger.setLevel(logging.DEBUG) consoleHandler = logging.StreamHandler(sys.stdout) -fileHandler = logging.FileHandler("/var/log/backive/backive.log") -if not os.path.exists("/var/log/backive"): - os.makedirs("/var/log/backive") +fileHandler = logging.FileHandler("backive.log") rootlogger.addHandler(consoleHandler) rootlogger.addHandler(fileHandler) @@ -29,7 +29,14 @@ class Backive: self._events = None async def callback(self, data=None): - print("Callback: {}".format(str(data))) + data_dict = json.loads(data) + uuid = data_dict.get("ID_FS_UUID", None) + print("UUID: %s" % uuid) + if uuid: + backups = self._config.get_backups_by_device(uuid) + for backup in backups: + backup.run() + def serve(self): loop = asyncio.get_event_loop() diff --git a/backive/backive_udev b/backive/backive_udev old mode 100644 new mode 100755 index 12204d7..d1e3732 --- a/backive/backive_udev +++ b/backive/backive_udev @@ -4,12 +4,20 @@ Callable script for udev rules. """ import socket -import sys +import os +import json -unix_socket = "/tmp/backive/backive.sock" -sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) -sock.connect(unix_socket) -if len(sys.argv) > 1: - sock.send(sys.argv[1].encode()) -sock.close() +with open("/tmp/backive/udev.log", "a") as stream: + stream.write("udev run...") + for k, v in os.environ.items(): + stream.write("{k}: {v}\n".format(k=k, v=v)) + + unix_socket = "/tmp/backive/backive.sock" + try: + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.connect(unix_socket) + sock.send(json.dumps(dict(os.environ)).encode()) + sock.close() + except Exception as e: + stream.write("Exception occurred: {}".format(str(e))) diff --git a/backive/config/schema.yml b/backive/config/schema.yml index 2ae12fa..7624a46 100644 --- a/backive/config/schema.yml +++ b/backive/config/schema.yml @@ -45,13 +45,11 @@ definitions: type: string # target device name frequency: - type: string - # in numbers? days? weeks? - # or just strings like + # JUST DAYS! + type: integer + # other possibilities in the future # weekly, biweekly, monthly, yearly # 7d, 2w, 1m, 1y (this needs parser) - # - # No frequency means no reminder and the backup is always started when the device is available, but not more than once a day! script: type: string # MVP just executes a script, nothing else @@ -73,6 +71,7 @@ definitions: - to - target_device - script + - frequency additionalProperties: false preferences_section: type: object diff --git a/backive/core/backup.py b/backive/core/backup.py index 2d02fba..72c9a3c 100644 --- a/backive/core/backup.py +++ b/backive/core/backup.py @@ -1,4 +1,5 @@ import os +import logging class Backup: @@ -8,13 +9,13 @@ class Backup: @classmethod def instance(cls, name, config=None): + logging.debug("Backup instance created (%s)", name) return Backup(name, config) def get_frequency(self): return self.config.get("frequency", None) def run(self): - if self.config.get("scripts", None): pass diff --git a/backive/core/device.py b/backive/core/device.py index 91de95b..c37c356 100644 --- a/backive/core/device.py +++ b/backive/core/device.py @@ -1,4 +1,5 @@ import os +import logging import backive.config.config as cfg @@ -11,6 +12,7 @@ class Device: @classmethod def instance(cls, name, config=None): + logging.debug("Device instance created (%s)", name) return Device(name, config) @classmethod diff --git a/backive/core/scheduler.py b/backive/core/scheduler.py index 7271ed5..ee1aba9 100644 --- a/backive/core/scheduler.py +++ b/backive/core/scheduler.py @@ -57,7 +57,21 @@ class Scheduler(): self.save() def should_run(self, name): - pass + runs = self.__data.get("runs", dict()) + if name not in runs: + return True + if name in runs: + backups = self.__data.get("backups", dict()) + if name in backups: + frequency = backups[name] + last_ts = runs[name][-1] + now = datetime.now() + last = datetime.fromisoformat(last_ts) + diff = now - last + days = diff.days + if days > frequency and days >= 1: + return True + return False def get_overtimed(self): return list()