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,20 +121,23 @@ 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
with self.lock:
if not self.ready and not self.internet:
return return
try: try:
if self.internet: if self.status.newer_then_hours(self.options['interval']):
current_timestamp = time.time() logging.debug("[update] last check happened less than %d hours ago" % self.options['interval'])
# Check if an hour has passed since the last upload return
if current_timestamp - self.last_upload_timestamp >= 3600:
self.last_upload_timestamp = current_timestamp
logging.info("[gdrivesync] new handshake captured, backing up to gdrive") logging.info("[gdrivesync] new handshake captured, backing up to gdrive")
if self.options['backupfiles'] is not None: if self.options['backupfiles'] is not None:
self.backupfiles = self.backupfiles + self.options['backupfiles'] self.backupfiles = self.backupfiles + self.options['backupfiles']
self.backup_files(self.backupfiles, '/backup') self.backup_files(self.backupfiles, '/backup')
self.upload_to_gdrive('/backup', self.options['backup_folder']) self.upload_to_gdrive('/backup', self.options['backup_folder'])
self.status.update()
display = agent.view() display = agent.view()
display.update(force=True, new_data={'Backing up to gdrive ...'}) display.update(force=True, new_data={'Backing up to gdrive ...'})
except Exception as e: except Exception as e: