misc: refactored backup and update plugins
This commit is contained in:
parent
e9b9a1edb9
commit
02e6ccbd8e
sdcard/rootfs/root/pwnagotchi
@ -14,9 +14,17 @@ main:
|
||||
interval: 1 # every day
|
||||
files:
|
||||
- /root/brain.nn
|
||||
- /root/brain.json
|
||||
- /root/custom.yaml
|
||||
- /root/handshakes
|
||||
- /etc/ssh
|
||||
- /etc/hostname
|
||||
- /etc/hosts
|
||||
- /etc/motd
|
||||
- /var/log/pwnagotchi.log
|
||||
commands:
|
||||
- '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:
|
||||
enabled: false
|
||||
twitter:
|
||||
|
@ -4,21 +4,18 @@ __name__ = 'auto-backup'
|
||||
__license__ = 'GPL3'
|
||||
__description__ = 'This plugin backups files when internet is availaible.'
|
||||
|
||||
import os
|
||||
from pwnagotchi.utils import StatusFile
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
|
||||
OPTIONS = dict()
|
||||
LAST_UPDATE = None
|
||||
READY = False
|
||||
STATUS = StatusFile('/root/.auto-backup')
|
||||
|
||||
|
||||
def on_loaded():
|
||||
"""
|
||||
Gets called when the plugin gets loaded
|
||||
"""
|
||||
global READY
|
||||
global LAST_UPDATE
|
||||
|
||||
if 'files' not in OPTIONS or ('files' in OPTIONS and OPTIONS['files'] is None):
|
||||
logging.error("AUTO-BACKUP: No files to backup.")
|
||||
@ -32,31 +29,28 @@ def on_loaded():
|
||||
logging.error("AUTO-BACKUP: No commands given.")
|
||||
return
|
||||
|
||||
if os.path.exists('/root/.auto-backup'):
|
||||
LAST_BACKUP = datetime.fromtimestamp(os.path.getmtime('/root/.auto-backup'))
|
||||
|
||||
READY = True
|
||||
|
||||
|
||||
def on_internet_available(display, config, log):
|
||||
"""
|
||||
Called in manual mode when there's internet connectivity
|
||||
"""
|
||||
global LAST_UPDATE
|
||||
global STATUS
|
||||
|
||||
if READY:
|
||||
if LAST_BACKUP is not None:
|
||||
if (datetime.now() - LAST_BACKUP).days < OPTIONS['interval']:
|
||||
return
|
||||
if STATUS.newer_then_days(OPTIONS['interval']):
|
||||
return
|
||||
|
||||
files_to_backup = " ".join(OPTIONS['files'])
|
||||
try:
|
||||
display.set('status', 'Backing up ...')
|
||||
display.update()
|
||||
|
||||
for cmd in OPTIONS['commands']:
|
||||
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()
|
||||
with open('/root/.auto-backup', 'w') as f:
|
||||
f.write('success')
|
||||
|
||||
logging.info("AUTO-BACKUP: backup done")
|
||||
STATUS.update()
|
||||
except OSError as 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'
|
||||
__description__ = 'This plugin performs an "apt update && apt upgrade" when internet is availaible.'
|
||||
|
||||
import os
|
||||
import logging
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
from pwnagotchi.utils import StatusFile
|
||||
|
||||
OPTIONS = dict()
|
||||
LAST_UPDATE = None
|
||||
READY = False
|
||||
STATUS_FILE = '/root/.auto-update'
|
||||
STATUS = StatusFile('/root/.auto-update')
|
||||
|
||||
|
||||
def on_loaded():
|
||||
global READY
|
||||
global LAST_UPDATE
|
||||
|
||||
if 'interval' not in OPTIONS or ('interval' in OPTIONS and OPTIONS['interval'] is None):
|
||||
logging.error("AUTO-UPDATE: Interval is not set.")
|
||||
return
|
||||
|
||||
if os.path.exists(STATUS_FILE):
|
||||
LAST_UPDATE = datetime.fromtimestamp(os.path.getmtime(STATUS_FILE))
|
||||
|
||||
READY = True
|
||||
|
||||
|
||||
def on_internet_available(display, config, log):
|
||||
global LAST_UPDATE
|
||||
global STATUS
|
||||
|
||||
if READY:
|
||||
if LAST_UPDATE is not None:
|
||||
if (datetime.now() - LAST_UPDATE).days < OPTIONS['interval']:
|
||||
return
|
||||
if STATUS.newer_then_days(OPTIONS['interval']):
|
||||
return
|
||||
|
||||
try:
|
||||
display.set('status', 'Updating ...')
|
||||
display.update()
|
||||
|
||||
logging.info("AUTO-UPDATE: updating packages index ...")
|
||||
|
||||
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.")
|
||||
|
||||
LAST_UPDATE = datetime.now()
|
||||
with open(STATUS_FILE, 'w') as f:
|
||||
f.write('success')
|
||||
STATUS.update()
|
||||
except Exception as e:
|
||||
logging.exception("AUTO-UPDATE ERROR")
|
||||
|
||||
display.set('status', 'Updated!')
|
||||
display.update()
|
||||
|
@ -1,3 +1,4 @@
|
||||
from datetime import datetime
|
||||
import logging
|
||||
import glob
|
||||
import os
|
||||
@ -78,3 +79,20 @@ def blink(times=1, delay=0.3):
|
||||
led(False)
|
||||
time.sleep(delay)
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user