pybackive/backive/backive_service

90 lines
2.9 KiB
Plaintext
Raw Permalink Normal View History

2019-02-22 21:26:01 +01:00
#!/usr/bin/env python3
"""
Service startup script.
"""
2020-01-06 13:26:52 +01:00
import sys
import os
import asyncio
2020-01-06 13:26:52 +01:00
import logging
import json
2019-05-03 20:36:09 +02:00
from backive.core.events import EventInterface
from backive.core.scheduler import Scheduler
from backive.core.backup import Backup
2019-05-03 21:36:44 +02:00
from backive.config.config import Config
2019-05-03 20:36:09 +02:00
2019-02-24 22:42:36 +01:00
2020-03-18 21:09:17 +01:00
logging.basicConfig(level=logging.DEBUG)
2020-01-06 13:26:52 +01:00
rootlogger = logging.getLogger()
rootlogger.setLevel(logging.DEBUG)
consoleHandler = logging.StreamHandler(sys.stdout)
fileHandler = logging.FileHandler("backive.log")
2020-01-06 13:26:52 +01:00
rootlogger.addHandler(fileHandler)
2020-03-18 21:09:17 +01:00
#rootlogger.addHandler(consoleHandler)
2020-01-06 13:26:52 +01:00
logging.info("Backive starting.")
2019-02-24 22:42:36 +01:00
class Backive:
def __init__(self):
self._scheduler = Scheduler()
2019-05-03 21:36:44 +02:00
self._config = Config()
2019-05-03 20:36:09 +02:00
self._events = None
self.initialize_scheduler()
def initialize_scheduler(self):
backups = self._config.get_backups()
for backup in backups:
self._scheduler.register_backup(backup.name, backup.get_frequency())
2019-02-24 22:42:36 +01:00
async def callback(self, data=None):
data_dict = json.loads(data)
uuid = data_dict.get("ID_FS_UUID", None)
2020-03-18 21:09:17 +01:00
logging.info("UUID: %s", uuid)
if uuid and data_dict.get("ACTION") == "add":
logging.debug(json.dumps(data_dict, indent=4))
2020-03-18 21:09:17 +01:00
backups = await self._config.get_backups_by_device(uuid)
device = await self._config.get_uuid_device(uuid)
prefs = await self._config.get_preferences()
if backups:
logging.info("Mounting device '%s'", uuid)
mount_available = await device.mount(prefs.get("mount_root"))
if mount_available:
for backup in backups:
if await self._scheduler.should_run(backup.name):
logging.info("Running backup '%s'", backup.name)
result = await backup.run()
logging.debug("Result: %s", result)
await self._scheduler.register_run(backup.name)
else:
logging.info(
"Backup '%s' next run interval has not been reached.",
backup.name
)
logging.info("Unmounting device '%s'", uuid)
await device.unmount()
else:
logging.error("Device %s could not be mounted...", uuid)
2019-02-24 22:42:36 +01:00
def serve(self):
loop = asyncio.get_event_loop()
self._events = EventInterface(self.callback, None, loop)
loop.run_forever()
2019-02-24 22:42:36 +01:00
pass
2020-01-06 13:26:52 +01:00
def __del__(self):
2021-05-20 20:24:46 +02:00
self._scheduler.save()
2020-01-06 13:26:52 +01:00
del self._events
2019-02-24 22:42:36 +01:00
if __name__ == "__main__":
2020-01-06 13:26:52 +01:00
backive = None
try:
backive = Backive()
backive.serve()
except Exception as e:
raise e
finally:
logging.info("Backive exited.")
del backive