Signed-off-by: Jeroen Oudshoorn <oudshoorn.jeroen@gmail.com>
This commit is contained in:
Jeroen Oudshoorn
2023-10-14 19:59:10 +02:00
parent eab3f1c222
commit 6362ea5aed
2 changed files with 28 additions and 26 deletions

View File

@ -1,3 +1,4 @@
main.plugins.gdrivesync.enabled = false main.plugins.gdrivesync.enabled = false
main.plugins.gdrivesync.backupfiles = [''] main.plugins.gdrivesync.backupfiles = ['']
main.plugins.gdrivesync.backup_folder = "PwnagotchiBackups" main.plugins.gdrivesync.backup_folder = "PwnagotchiBackups"
main.plugin.gdrivesync.interval = 1

View File

@ -7,6 +7,8 @@ import pwnagotchi
import pydrive2 import pydrive2
from pydrive2.auth import GoogleAuth from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive from pydrive2.drive import GoogleDrive
from threading import Lock
from pwnagotchi.utils import StatusFile, parse_version as version_to_tuple
class GdriveSync(plugins.Plugin): class GdriveSync(plugins.Plugin):
@ -19,17 +21,14 @@ class GdriveSync(plugins.Plugin):
} }
def __init__(self): def __init__(self):
self.lock = Lock()
self.internet = False self.internet = False
self.ready = False self.ready = False
self.drive = None self.drive = None
self.last_upload_timestamp = time.time() self.status = StatusFile('/root/.gdrive-backup')
self.backup = True self.backup = True
self.backupfiles = [ self.backupfiles = [
'/root/brain.nn', '/root',
'/root/brain.json',
'/root/.api-report.json',
'/root/handshakes',
'/root/peers',
'/etc/pwnagotchi' '/etc/pwnagotchi'
] ]
@ -70,7 +69,7 @@ class GdriveSync(plugins.Plugin):
logging.info(f"[gDriveSync] Created folder '{self.options['backup_folder']}' with ID: {backup_folder_id}") logging.info(f"[gDriveSync] Created folder '{self.options['backup_folder']}' with ID: {backup_folder_id}")
# Continue with the rest of the code using backup_folder_id # Continue with the rest of the code using backup_folder_id
file_list = self.drive.ListFile({'q': f"'{backup_folder_id}' and trashed=false"}).GetList() file_list = self.drive.ListFile({'q': f"'{backup_folder_id}' in parents and mimeType = 'application/vnd.google-apps.folder' and trashed=false"}).GetList()
if not file_list: if not file_list:
# Handle the case where no files were found # Handle the case where no files were found
@ -97,8 +96,7 @@ class GdriveSync(plugins.Plugin):
# Optionally, you can use the downloaded files as needed # Optionally, you can use the downloaded files as needed
# For example, you can copy them to the corresponding directories # For example, you can copy them to the corresponding directories
self.backup = True self.backup = True
with open("/root/.gdrive-backup", "w").close(): self.status.update()
pass # Create an empty file
# reboot so we can start opwngrid with backup id # reboot so we can start opwngrid with backup id
pwnagotchi.reboot() pwnagotchi.reboot()
@ -123,24 +121,27 @@ class GdriveSync(plugins.Plugin):
self.internet = True self.internet = True
def on_handshake(self, agent): def on_handshake(self, agent):
if not self.ready: if self.lock.locked():
return return
try: with self.lock:
if self.internet: if not self.ready and not self.internet:
current_timestamp = time.time() return
# Check if an hour has passed since the last upload try:
if current_timestamp - self.last_upload_timestamp >= 3600: if self.status.newer_then_hours(self.options['interval']):
self.last_upload_timestamp = current_timestamp logging.debug("[update] last check happened less than %d hours ago" % self.options['interval'])
logging.info("[gdrivesync] new handshake captured, backing up to gdrive") return
if self.options['backupfiles'] is not None:
self.backupfiles = self.backupfiles + self.options['backupfiles']
self.backup_files(self.backupfiles, '/backup')
self.upload_to_gdrive('/backup', self.options['backup_folder']) logging.info("[gdrivesync] new handshake captured, backing up to gdrive")
display = agent.view() if self.options['backupfiles'] is not None:
display.update(force=True, new_data={'Backing up to gdrive ...'}) self.backupfiles = self.backupfiles + self.options['backupfiles']
except Exception as e: self.backup_files(self.backupfiles, '/backup')
logging.error(f"[gDriveSync] Error during handshake processing: {e}")
self.upload_to_gdrive('/backup', self.options['backup_folder'])
self.status.update()
display = agent.view()
display.update(force=True, new_data={'Backing up to gdrive ...'})
except Exception as e:
logging.error(f"[gDriveSync] Error during handshake processing: {e}")
def backup_files(self, paths, dest_path): def backup_files(self, paths, dest_path):
for src_path in paths: for src_path in paths: