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.'
|
|
|
|
|
2019-10-05 14:22:20 +02:00
|
|
|
from pwnagotchi.utils import StatusFile
|
2019-10-05 11:19:53 +02:00
|
|
|
import logging
|
2019-10-05 14:22:20 +02:00
|
|
|
import os
|
2019-10-05 11:19:53 +02:00
|
|
|
import subprocess
|
|
|
|
|
|
|
|
OPTIONS = dict()
|
|
|
|
READY = False
|
2019-10-05 14:22:20 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
2019-10-08 14:54:03 +02:00
|
|
|
def on_internet_available(agent):
|
2019-10-05 14:22:20 +02:00
|
|
|
global STATUS
|
2019-10-05 11:19:53 +02:00
|
|
|
|
|
|
|
if READY:
|
2019-10-05 14:22:20 +02:00
|
|
|
if STATUS.newer_then_days(OPTIONS['interval']):
|
|
|
|
return
|
2019-10-05 11:19:53 +02:00
|
|
|
|
|
|
|
files_to_backup = " ".join(OPTIONS['files'])
|
|
|
|
try:
|
2019-10-08 14:54:03 +02:00
|
|
|
display = agent.view()
|
|
|
|
|
2019-10-05 17:57:15 +02:00
|
|
|
logging.info("AUTO-BACKUP: Backing up ...")
|
2019-10-05 14:22:20 +02:00
|
|
|
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})")
|
2019-10-05 14:22:20 +02:00
|
|
|
|
|
|
|
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}")
|
|
|
|
|
2019-10-05 14:22:20 +02:00
|
|
|
display.set('status', 'Backup done!')
|
|
|
|
display.update()
|