- 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 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()

22
backive/backive_udev Normal file → Executable file
View File

@ -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)))

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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()