From 580d93d98f2b0f305b8b4d705a59f7dd17dd08fd Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Fri, 31 Jul 2015 09:09:58 +0000 Subject: [PATCH] (gdrive backup) catch and log errors and apply code style --- src/etc/inc/config.lib.inc | 56 ++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/src/etc/inc/config.lib.inc b/src/etc/inc/config.lib.inc index 6bedacb5a..81f11ca94 100644 --- a/src/etc/inc/config.lib.inc +++ b/src/etc/inc/config.lib.inc @@ -355,37 +355,51 @@ function make_config_revision_entry($desc = null, $override_user = null) { * backup config to google drive and return current file list (/ info) * */ -function backup_to_google_drive() { - $client = new Google\API\Drive() ; - $cnf = OPNsense\Core\Config::getInstance(); - if ($cnf->isValid()) { - $config = $cnf->object() ; - if ( isset($config->system->remotebackup) && isset($config->system->remotebackup->GDriveEnabled) && $config->system->remotebackup->GDriveEnabled == "on" ){ - $client->login($config->system->remotebackup->GDriveEmail->__toString(), $config->system->remotebackup->GDriveP12key->__toString() ); +function backup_to_google_drive() +{ + $cnf = OPNsense\Core\Config::getInstance(); + if ($cnf->isValid()) { + $config = $cnf->object(); + if (isset($config->system->remotebackup) && isset($config->system->remotebackup->GDriveEnabled) && $config->system->remotebackup->GDriveEnabled == "on") { + try { + $client = new Google\API\Drive(); + $client->login($config->system->remotebackup->GDriveEmail->__toString(), + $config->system->remotebackup->GDriveP12key->__toString()); + } catch (Exception $e) { + log_error("error connecting to Google Drive"); + return array(); + } + // backup source data to local strings (plain/encrypted) - $confdata = file_get_contents('/conf/config.xml') ; - $confdata_enc = encrypt_data($confdata, $config->system->remotebackup->GDrivePassword->__toString()) ; + $confdata = file_get_contents('/conf/config.xml'); + $confdata_enc = encrypt_data($confdata, $config->system->remotebackup->GDrivePassword->__toString()); tagfile_reformat($confdata_enc, $confdata_enc, "config.xml"); // read filelist (config-*.xml) - $files = $client->listFiles($config->system->remotebackup->GDriveFolderID->__toString()); - $configfiles = array(); + try { + $files = $client->listFiles($config->system->remotebackup->GDriveFolderID->__toString()); + } catch (Exception $e) { + log_error("error while fetching filelist from Google Drive"); + return array(); + } + + $configfiles = array(); foreach ($files as $file) { - if (fnmatch("config-*.xml", $file['title'])) { - $configfiles[$file['title']] = $file; - } + if (fnmatch("config-*.xml", $file['title'])) { + $configfiles[$file['title']] = $file; + } } krsort($configfiles); // backup new file if changed (or if first in backup) - $target_filename = "config-".time().".xml"; + $target_filename = "config-" . time() . ".xml"; if (count($configfiles) > 1) { // compare last backup with current, only save new $bck_data_enc_in = $client->download($configfiles[array_keys($configfiles)[0]]); - $bck_data_enc=""; - tagfile_deformat($bck_data_enc_in, $bck_data_enc,"config.xml") ; + $bck_data_enc = ""; + tagfile_deformat($bck_data_enc_in, $bck_data_enc, "config.xml"); $bck_data = decrypt_data($bck_data_enc, $config->system->remotebackup->GDrivePassword->__toString()); if ($bck_data == $confdata) { $target_filename = null; @@ -393,7 +407,8 @@ function backup_to_google_drive() { } if (!is_null($target_filename)) { log_error("backup configuration as " . $target_filename); - $configfiles[$target_filename] = $client->upload($config->system->remotebackup->GDriveFolderID->__toString(),$target_filename, $confdata_enc ); + $configfiles[$target_filename] = $client->upload($config->system->remotebackup->GDriveFolderID->__toString(), + $target_filename, $confdata_enc); krsort($configfiles); } @@ -402,16 +417,15 @@ function backup_to_google_drive() { $fcount = 0; foreach ($configfiles as $filename => $file) { if ($fcount >= $config->system->remotebackup->GDriveBackupCount->__toString()) { - log_error("remove " . $filename . " from Google Drive" ); + log_error("remove " . $filename . " from Google Drive"); $client->delete($file); } - $fcount++ ; + $fcount++; } } // return filelist return $configfiles; - } }