Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud-Backup #948

Merged
merged 5 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/helpermodules/setdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,8 @@ def process_system_topic(self, msg: mqtt.MQTTMessage):
enthält Topic und Payload
"""
try:
if "openWB/set/system/lastlivevaluesJson" in msg.topic:
if ("openWB/set/system/lastlivevaluesJson" in msg.topic or
"openWB/set/system/cloud_backup" in msg.topic):
self._validate_value(msg, "json")
elif ("openWB/set/system/perform_update" in msg.topic or
"openWB/set/system/wizard_done" in msg.topic or
Expand Down
33 changes: 30 additions & 3 deletions packages/helpermodules/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from helpermodules import pub
from control import data
from modules.common import req

log = logging.getLogger(__name__)

Expand All @@ -17,9 +18,9 @@ class System:
def __init__(self):
"""
"""
self.data = {}
self.data["update_in_progress"] = False
self.data["perform_update"] = False
self.data = {"cloud_backup": {"ip_address": None, "password": None, "user": None},
"update_in_progress": False,
"perform_update": False}

def perform_update(self):
""" markiert ein aktives Update, triggert das Update auf dem Master und den externen WBs.
Expand Down Expand Up @@ -72,3 +73,29 @@ def update_ip_address(self) -> None:
if new_ip != self.data["ip_address"] and new_ip != "":
self.data["ip_address"] = new_ip
pub.Pub().pub("openWB/set/system/ip_address", new_ip)

def create_backup_and_send_to_cloud(self):
cloud_backup = self.data["cloud_backup"]
if cloud_backup["ip_address"] is not None:
backup_filename = self.create_backup()
with open(self._get_parent_file()/'data'/'backup'/backup_filename, 'rb') as f:
Copy link
Contributor

@MartinRinas MartinRinas Jun 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dieser WebDav Pfad scheint mir auf einen bestimmten Anbieter zugeschnitten. Lässt sich ggf. die gesamte URI als konfigurierbarer Parameter angeben? ggf. auch mit einer Liste/DropDown bekannter Anbieter als Template. Dann wäre der Weg für OneDrive & co einfach zu beschreiten. Bring' mich gern mit ein um das Thema OneDrive abzudecken. Sei es als UI, Wiki Artikel und/oder code.

data = f.read()
req.get_http_session().put(
f'{cloud_backup["ip_address"]}/public.php/webdav/{backup_filename}',
headers={'X-Requested-With': 'XMLHttpRequest', },
data=data,
auth=(cloud_backup["user"], '' if cloud_backup["password"] is None else cloud_backup["password"]),
)
log.debug(f'Sicherung erstellt und unter {cloud_backup["ip_address"]}/public.php/webdav/'
f'{backup_filename} hochgeladen.')

def create_backup(self) -> str:
result = subprocess.run([str(self._get_parent_file() / "runs" / "backup.sh"), "1"], stdout=subprocess.PIPE)
if result.returncode == 0:
file_name = result.stdout.decode("utf-8").rstrip('\n')
return file_name
else:
raise Exception(f'Backup-Status: {result.returncode}, Meldung: {result.stdout.decode("utf-8")}')

def _get_parent_file(self) -> Path:
return Path(__file__).resolve().parents[2]
2 changes: 2 additions & 0 deletions packages/helpermodules/update_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ class UpdateConfig:
"^openWB/LegacySmartHome/Devices/[1-2]+/TemperatureSensor[0-2]$",

"^openWB/system/boot_done$",
"^openWB/system/cloud_backup$",
"^openWB/system/dataprotection_acknowledged$",
"^openWB/system/debug_level$",
"^openWB/system/lastlivevaluesJson$",
Expand Down Expand Up @@ -407,6 +408,7 @@ class UpdateConfig:
("openWB/optional/int_display/theme", dataclass_utils.asdict(CardsDisplayTheme())),
("openWB/optional/led/active", False),
("openWB/optional/rfid/active", False),
("openWB/system/cloud_backup", {"ip_address": None, "password": None, "user": None}),
("openWB/system/dataprotection_acknowledged", False),
("openWB/system/debug_level", 30),
("openWB/system/device/module_update_completed", True),
Expand Down
1 change: 1 addition & 0 deletions packages/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def handler5Min(self):
def handler_midnight(self):
try:
measurement_log.save_log("monthly")
data.data.system_data["system"].create_backup_and_send_to_cloud()
except KeyboardInterrupt:
log.critical("Ausführung durch exit_after gestoppt: "+traceback.format_exc())
except Exception:
Expand Down