- Service now has better logging (still not satisfied)
- udev tool now works sending the udev environment as json to the
service
- schema update that frequency is always days
- some additions or corrections at scheduler, backup and device classes
This commit is contained in:
Marcel Otte 2020-01-06 23:01:25 +01:00
parent 52f52a61c7
commit ec610ab3b3
6 changed files with 49 additions and 18 deletions

15
backive/backive_service Normal file → Executable file
View File

@ -7,16 +7,16 @@ import sys
import os import os
import asyncio import asyncio
import logging import logging
import json
from backive.core.events import EventInterface from backive.core.events import EventInterface
from backive.core.scheduler import Scheduler
from backive.config.config import Config from backive.config.config import Config
rootlogger = logging.getLogger() rootlogger = logging.getLogger()
rootlogger.setLevel(logging.DEBUG) rootlogger.setLevel(logging.DEBUG)
consoleHandler = logging.StreamHandler(sys.stdout) consoleHandler = logging.StreamHandler(sys.stdout)
fileHandler = logging.FileHandler("/var/log/backive/backive.log") fileHandler = logging.FileHandler("backive.log")
if not os.path.exists("/var/log/backive"):
os.makedirs("/var/log/backive")
rootlogger.addHandler(consoleHandler) rootlogger.addHandler(consoleHandler)
rootlogger.addHandler(fileHandler) rootlogger.addHandler(fileHandler)
@ -29,7 +29,14 @@ class Backive:
self._events = None self._events = None
async def callback(self, data=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): def serve(self):
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()

16
backive/backive_udev Normal file → Executable file
View File

@ -4,12 +4,20 @@
Callable script for udev rules. Callable script for udev rules.
""" """
import socket import socket
import sys import os
import json
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" unix_socket = "/tmp/backive/backive.sock"
try:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(unix_socket) sock.connect(unix_socket)
if len(sys.argv) > 1: sock.send(json.dumps(dict(os.environ)).encode())
sock.send(sys.argv[1].encode())
sock.close() sock.close()
except Exception as e:
stream.write("Exception occurred: {}".format(str(e)))

View File

@ -45,13 +45,11 @@ definitions:
type: string type: string
# target device name # target device name
frequency: frequency:
type: string # JUST DAYS!
# in numbers? days? weeks? type: integer
# or just strings like # other possibilities in the future
# weekly, biweekly, monthly, yearly # weekly, biweekly, monthly, yearly
# 7d, 2w, 1m, 1y (this needs parser) # 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: script:
type: string type: string
# MVP just executes a script, nothing else # MVP just executes a script, nothing else
@ -73,6 +71,7 @@ definitions:
- to - to
- target_device - target_device
- script - script
- frequency
additionalProperties: false additionalProperties: false
preferences_section: preferences_section:
type: object type: object

View File

@ -1,4 +1,5 @@
import os import os
import logging
class Backup: class Backup:
@ -8,13 +9,13 @@ class Backup:
@classmethod @classmethod
def instance(cls, name, config=None): def instance(cls, name, config=None):
logging.debug("Backup instance created (%s)", name)
return Backup(name, config) return Backup(name, config)
def get_frequency(self): def get_frequency(self):
return self.config.get("frequency", None) return self.config.get("frequency", None)
def run(self): def run(self):
if self.config.get("scripts", None): if self.config.get("scripts", None):
pass pass

View File

@ -1,4 +1,5 @@
import os import os
import logging
import backive.config.config as cfg import backive.config.config as cfg
@ -11,6 +12,7 @@ class Device:
@classmethod @classmethod
def instance(cls, name, config=None): def instance(cls, name, config=None):
logging.debug("Device instance created (%s)", name)
return Device(name, config) return Device(name, config)
@classmethod @classmethod

View File

@ -57,7 +57,21 @@ class Scheduler():
self.save() self.save()
def should_run(self, name): 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): def get_overtimed(self):
return list() return list()