Signed-off-by: Jeroen Oudshoorn <oudshoorn.jeroen@gmail.com>
This commit is contained in:
Jeroen Oudshoorn
2023-10-13 15:20:54 +02:00
parent ae4f1340ad
commit 9b5fc8a9b3

View File

@ -50,73 +50,69 @@ class GdriveSync(plugins.Plugin):
if not os.path.exists("/root/.gdrive-backup"): if not os.path.exists("/root/.gdrive-backup"):
self.backup = False self.backup = False
try: gauth = GoogleAuth(settings_file="/root/settings.yaml")
gauth = GoogleAuth(settings_file="/root/settings.yaml") gauth.LoadCredentialsFile("/root/credentials.json")
gauth.LoadCredentialsFile("/root/credentials.json") if gauth.credentials is None:
if gauth.credentials is None: # Authenticate if they're not there
# Authenticate if they're not there gauth.LocalWebserverAuth()
gauth.LocalWebserverAuth() elif gauth.access_token_expired:
elif gauth.access_token_expired: # Refresh them if expired
# Refresh them if expired gauth.Refresh()
gauth.Refresh() gauth.SaveCredentialsFile("/root/credentials.json")
gauth.SaveCredentialsFile("/root/credentials.json") gauth.Authorize()
gauth.Authorize()
# Create GoogleDrive instance # Create GoogleDrive instance
self.drive = GoogleDrive(gauth) self.drive = GoogleDrive(gauth)
# if backup file does not exist, we will check for backup folder on gdrive. # if backup file does not exist, we will check for backup folder on gdrive.
if not self.backup: if not self.backup:
# Use self.options['backup_folder'] as the folder ID where backups are stored # Use self.options['backup_folder'] as the folder ID where backups are stored
backup_folder_id = self.get_folder_id_by_name(self.drive, self.options['backup_folder']) backup_folder_id = self.get_folder_id_by_name(self.drive, self.options['backup_folder'])
if not backup_folder_id: if not backup_folder_id:
# If the folder doesn't exist, create it # If the folder doesn't exist, create it
folder = self.drive.CreateFile( folder = self.drive.CreateFile(
{'title': self.options['backup_folder'], 'mimeType': 'application/vnd.google-apps.folder'}) {'title': self.options['backup_folder'], 'mimeType': 'application/vnd.google-apps.folder'})
folder.Upload() folder.Upload()
backup_folder_id = folder['id'] backup_folder_id = folder['id']
print(f"Created folder '{self.options['backup_folder']}' with ID: {backup_folder_id}") print(f"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}' 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
logging.warning(f"[gDriveSync] No files found in the folder with ID {backup_folder_id}") logging.warning(f"[gDriveSync] No files found in the folder with ID {backup_folder_id}")
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.backup = True
# Specify the local backup path
local_backup_path = '/'
# Create the local backup directory if it doesn't exist
os.makedirs(local_backup_path, exist_ok=True)
# Download each file in the folder
for file in file_list:
local_file_path = os.path.join(local_backup_path, file['title'])
file.GetContentFile(local_file_path)
logging.info(f"[gDriveSync] Downloaded {file['title']} from Google Drive")
# Optionally, you can use the downloaded files as needed
# For example, you can copy them to the corresponding directories
self.backup = True self.backup = True
with open("/root/.gdrive-backup", "w").close():
pass # Create an empty file
# reboot so we can start opwngrid with backup id
pwnagotchi.reboot()
# all set, gdriveSync is ready to run # Specify the local backup path
self.ready = True local_backup_path = '/'
logging.info("[gdrivesync] loaded")
except Exception as e: # Create the local backup directory if it doesn't exist
logging.error(f"Error: {e}") os.makedirs(local_backup_path, exist_ok=True)
self.ready = False
# Download each file in the folder
for file in file_list:
local_file_path = os.path.join(local_backup_path, file['title'])
file.GetContentFile(local_file_path)
logging.info(f"[gDriveSync] Downloaded {file['title']} from Google Drive")
# Optionally, you can use the downloaded files as needed
# For example, you can copy them to the corresponding directories
self.backup = True
with open("/root/.gdrive-backup", "w").close():
pass # Create an empty file
# reboot so we can start opwngrid with backup id
pwnagotchi.reboot()
# all set, gdriveSync is ready to run
self.ready = True
logging.info("[gdrivesync] loaded")
def on_unload(self, ui): def on_unload(self, ui):
logging.info("[gdrivesync] unloaded") logging.info("[gdrivesync] unloaded")