mirror of
https://github.com/jayofelony/pwnagotchi.git
synced 2025-07-01 18:37:27 -04:00
misc: refactored backup and update plugins
This commit is contained in:
@ -14,9 +14,17 @@ main:
|
|||||||
interval: 1 # every day
|
interval: 1 # every day
|
||||||
files:
|
files:
|
||||||
- /root/brain.nn
|
- /root/brain.nn
|
||||||
|
- /root/brain.json
|
||||||
|
- /root/custom.yaml
|
||||||
|
- /root/handshakes
|
||||||
|
- /etc/ssh
|
||||||
|
- /etc/hostname
|
||||||
|
- /etc/hosts
|
||||||
|
- /etc/motd
|
||||||
|
- /var/log/pwnagotchi.log
|
||||||
commands:
|
commands:
|
||||||
- 'tar czf /tmp/backup.tar.gz {files}'
|
- 'tar czf /tmp/backup.tar.gz {files}'
|
||||||
- 'scp /tmp/backup.tar.gz 10.0.0.1:/backups/backup-$(date).tar.gz'
|
- 'scp /tmp/backup.tar.gz pwnagotchi@10.0.0.1:/home/pwnagotchi/backups/backup-$(date).tar.gz'
|
||||||
gps:
|
gps:
|
||||||
enabled: false
|
enabled: false
|
||||||
twitter:
|
twitter:
|
||||||
|
@ -4,21 +4,18 @@ __name__ = 'auto-backup'
|
|||||||
__license__ = 'GPL3'
|
__license__ = 'GPL3'
|
||||||
__description__ = 'This plugin backups files when internet is availaible.'
|
__description__ = 'This plugin backups files when internet is availaible.'
|
||||||
|
|
||||||
import os
|
from pwnagotchi.utils import StatusFile
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
OPTIONS = dict()
|
OPTIONS = dict()
|
||||||
LAST_UPDATE = None
|
|
||||||
READY = False
|
READY = False
|
||||||
|
STATUS = StatusFile('/root/.auto-backup')
|
||||||
|
|
||||||
|
|
||||||
def on_loaded():
|
def on_loaded():
|
||||||
"""
|
|
||||||
Gets called when the plugin gets loaded
|
|
||||||
"""
|
|
||||||
global READY
|
global READY
|
||||||
global LAST_UPDATE
|
|
||||||
|
|
||||||
if 'files' not in OPTIONS or ('files' in OPTIONS and OPTIONS['files'] is None):
|
if 'files' not in OPTIONS or ('files' in OPTIONS and OPTIONS['files'] is None):
|
||||||
logging.error("AUTO-BACKUP: No files to backup.")
|
logging.error("AUTO-BACKUP: No files to backup.")
|
||||||
@ -32,31 +29,28 @@ def on_loaded():
|
|||||||
logging.error("AUTO-BACKUP: No commands given.")
|
logging.error("AUTO-BACKUP: No commands given.")
|
||||||
return
|
return
|
||||||
|
|
||||||
if os.path.exists('/root/.auto-backup'):
|
|
||||||
LAST_BACKUP = datetime.fromtimestamp(os.path.getmtime('/root/.auto-backup'))
|
|
||||||
|
|
||||||
READY = True
|
READY = True
|
||||||
|
|
||||||
|
|
||||||
def on_internet_available(display, config, log):
|
def on_internet_available(display, config, log):
|
||||||
"""
|
global STATUS
|
||||||
Called in manual mode when there's internet connectivity
|
|
||||||
"""
|
|
||||||
global LAST_UPDATE
|
|
||||||
|
|
||||||
if READY:
|
if READY:
|
||||||
if LAST_BACKUP is not None:
|
if STATUS.newer_then_days(OPTIONS['interval']):
|
||||||
if (datetime.now() - LAST_BACKUP).days < OPTIONS['interval']:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
files_to_backup = " ".join(OPTIONS['files'])
|
files_to_backup = " ".join(OPTIONS['files'])
|
||||||
try:
|
try:
|
||||||
|
display.set('status', 'Backing up ...')
|
||||||
|
display.update()
|
||||||
|
|
||||||
for cmd in OPTIONS['commands']:
|
for cmd in OPTIONS['commands']:
|
||||||
subprocess.call(cmd.format(files=files_to_backup).split(), stdout=open(os.devnull, 'wb'))
|
subprocess.call(cmd.format(files=files_to_backup).split(), stdout=open(os.devnull, 'wb'))
|
||||||
logging.info("AUTO-BACKUP: Successfuly ran backup commands.")
|
|
||||||
LAST_BACKUP = datetime.now()
|
logging.info("AUTO-BACKUP: backup done")
|
||||||
with open('/root/.auto-backup', 'w') as f:
|
STATUS.update()
|
||||||
f.write('success')
|
|
||||||
except OSError as os_e:
|
except OSError as os_e:
|
||||||
logging.info(f"AUTO-BACKUP: Error: {os_e}")
|
logging.info(f"AUTO-BACKUP: Error: {os_e}")
|
||||||
|
|
||||||
|
display.set('status', 'Backup done!')
|
||||||
|
display.update()
|
||||||
|
@ -4,40 +4,36 @@ __name__ = 'auto-update'
|
|||||||
__license__ = 'GPL3'
|
__license__ = 'GPL3'
|
||||||
__description__ = 'This plugin performs an "apt update && apt upgrade" when internet is availaible.'
|
__description__ = 'This plugin performs an "apt update && apt upgrade" when internet is availaible.'
|
||||||
|
|
||||||
import os
|
|
||||||
import logging
|
import logging
|
||||||
import subprocess
|
import subprocess
|
||||||
from datetime import datetime
|
from pwnagotchi.utils import StatusFile
|
||||||
|
|
||||||
OPTIONS = dict()
|
OPTIONS = dict()
|
||||||
LAST_UPDATE = None
|
|
||||||
READY = False
|
READY = False
|
||||||
STATUS_FILE = '/root/.auto-update'
|
STATUS = StatusFile('/root/.auto-update')
|
||||||
|
|
||||||
|
|
||||||
def on_loaded():
|
def on_loaded():
|
||||||
global READY
|
global READY
|
||||||
global LAST_UPDATE
|
|
||||||
|
|
||||||
if 'interval' not in OPTIONS or ('interval' in OPTIONS and OPTIONS['interval'] is None):
|
if 'interval' not in OPTIONS or ('interval' in OPTIONS and OPTIONS['interval'] is None):
|
||||||
logging.error("AUTO-UPDATE: Interval is not set.")
|
logging.error("AUTO-UPDATE: Interval is not set.")
|
||||||
return
|
return
|
||||||
|
|
||||||
if os.path.exists(STATUS_FILE):
|
|
||||||
LAST_UPDATE = datetime.fromtimestamp(os.path.getmtime(STATUS_FILE))
|
|
||||||
|
|
||||||
READY = True
|
READY = True
|
||||||
|
|
||||||
|
|
||||||
def on_internet_available(display, config, log):
|
def on_internet_available(display, config, log):
|
||||||
global LAST_UPDATE
|
global STATUS
|
||||||
|
|
||||||
if READY:
|
if READY:
|
||||||
if LAST_UPDATE is not None:
|
if STATUS.newer_then_days(OPTIONS['interval']):
|
||||||
if (datetime.now() - LAST_UPDATE).days < OPTIONS['interval']:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
display.set('status', 'Updating ...')
|
||||||
|
display.update()
|
||||||
|
|
||||||
logging.info("AUTO-UPDATE: updating packages index ...")
|
logging.info("AUTO-UPDATE: updating packages index ...")
|
||||||
|
|
||||||
update = subprocess.Popen('apt update -y', shell=True, stdin=None,
|
update = subprocess.Popen('apt update -y', shell=True, stdin=None,
|
||||||
@ -52,8 +48,9 @@ def on_internet_available(display, config, log):
|
|||||||
|
|
||||||
logging.info("AUTO-UPDATE: complete.")
|
logging.info("AUTO-UPDATE: complete.")
|
||||||
|
|
||||||
LAST_UPDATE = datetime.now()
|
STATUS.update()
|
||||||
with open(STATUS_FILE, 'w') as f:
|
|
||||||
f.write('success')
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception("AUTO-UPDATE ERROR")
|
logging.exception("AUTO-UPDATE ERROR")
|
||||||
|
|
||||||
|
display.set('status', 'Updated!')
|
||||||
|
display.update()
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
from datetime import datetime
|
||||||
import logging
|
import logging
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
@ -78,3 +79,20 @@ def blink(times=1, delay=0.3):
|
|||||||
led(False)
|
led(False)
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
led(True)
|
led(True)
|
||||||
|
|
||||||
|
|
||||||
|
class StatusFile(object):
|
||||||
|
def __init__(self, path):
|
||||||
|
self._path = path
|
||||||
|
self._updated = None
|
||||||
|
|
||||||
|
if os.path.exists(path):
|
||||||
|
self._updated = datetime.fromtimestamp(os.path.getmtime(path))
|
||||||
|
|
||||||
|
def newer_then_days(self, days):
|
||||||
|
return self._updated is not None and (datetime.now() - self._updated).days < days
|
||||||
|
|
||||||
|
def update(self, data=None):
|
||||||
|
self._updated = datetime.now()
|
||||||
|
with open(self._path, 'w') as fp:
|
||||||
|
fp.write(str(self._updated) if data is None else data)
|
||||||
|
Reference in New Issue
Block a user