pybackive/backive/backive_service

66 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Service startup script.
"""
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
logging.basicConfig(level=logging.DEBUG)
rootlogger = logging.getLogger()
rootlogger.setLevel(logging.DEBUG)
consoleHandler = logging.StreamHandler(sys.stdout)
fileHandler = logging.FileHandler("backive.log")
rootlogger.addHandler(fileHandler)
#rootlogger.addHandler(consoleHandler)
logging.info("Backive starting.")
class Backive:
def __init__(self):
self._config = Config()
self._events = None
async def callback(self, data=None):
data_dict = json.loads(data)
uuid = data_dict.get("ID_FS_UUID", None)
logging.info("UUID: %s", uuid)
logging.debug(json.dumps(data_dict, indent=4))
if uuid and data_dict.get("ACTION") == "add":
backups = await self._config.get_backups_by_device(uuid)
for backup in backups:
logging.info("Running backup '%s'", backup.name)
result = await backup.run()
logging.debug("Result: %s", str(result))
def serve(self):
loop = asyncio.get_event_loop()
self._events = EventInterface(self.callback, None, loop)
loop.run_forever()
pass
def __del__(self):
del self._events
if __name__ == "__main__":
backive = None
try:
backive = Backive()
backive.serve()
except Exception as e:
raise e
finally:
logging.info("Backive exited.")
del backive