Files
pwnagotchi/pwnagotchi/plugins/default/auto-backup.py

66 lines
2.0 KiB
Python
Raw Normal View History

2019-10-05 11:19:53 +02:00
__author__ = '33197631+dadav@users.noreply.github.com'
__version__ = '1.0.0'
__name__ = 'auto-backup'
__license__ = 'GPL3'
__description__ = 'This plugin backups files when internet is availaible.'
from pwnagotchi.utils import StatusFile
2019-10-05 11:19:53 +02:00
import logging
import os
2019-10-05 11:19:53 +02:00
import subprocess
OPTIONS = dict()
READY = False
STATUS = StatusFile('/root/.auto-backup')
2019-10-05 11:19:53 +02:00
def on_loaded():
global READY
if 'files' not in OPTIONS or ('files' in OPTIONS and OPTIONS['files'] is None):
logging.error("AUTO-BACKUP: No files to backup.")
return
if 'interval' not in OPTIONS or ('interval' in OPTIONS and OPTIONS['interval'] is None):
logging.error("AUTO-BACKUP: Interval is not set.")
return
2019-10-05 11:32:54 +02:00
if 'commands' not in OPTIONS or ('commands' in OPTIONS and OPTIONS['commands'] is None):
logging.error("AUTO-BACKUP: No commands given.")
2019-10-05 11:19:53 +02:00
return
READY = True
2019-10-05 17:57:15 +02:00
logging.info("AUTO-BACKUP: Successfuly loaded.")
2019-10-05 11:19:53 +02:00
def on_internet_available(agent):
global STATUS
2019-10-05 11:19:53 +02:00
if READY:
if STATUS.newer_then_days(OPTIONS['interval']):
return
2019-10-05 11:19:53 +02:00
files_to_backup = " ".join(OPTIONS['files'])
try:
display = agent.view()
2019-10-05 17:57:15 +02:00
logging.info("AUTO-BACKUP: Backing up ...")
display.set('status', 'Backing up ...')
display.update()
2019-10-05 11:32:54 +02:00
for cmd in OPTIONS['commands']:
2019-10-05 17:57:15 +02:00
logging.info(f"AUTO-BACKUP: Running {cmd.format(files=files_to_backup)}")
process = subprocess.Popen(cmd.format(files=files_to_backup), shell=True, stdin=None,
stdout=open("/dev/null", "w"), stderr=None, executable="/bin/bash")
process.wait()
if process.returncode > 0:
raise OSError(f"Command failed (rc: {process.returncode})")
logging.info("AUTO-BACKUP: backup done")
STATUS.update()
2019-10-05 11:19:53 +02:00
except OSError as os_e:
logging.info(f"AUTO-BACKUP: Error: {os_e}")
display.set('status', 'Backup done!')
display.update()