Commit 580d93d9 authored by Ad Schellevis's avatar Ad Schellevis

(gdrive backup) catch and log errors and apply code style

parent eef2608a
...@@ -355,37 +355,51 @@ function make_config_revision_entry($desc = null, $override_user = null) { ...@@ -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) * backup config to google drive and return current file list (/ info)
* *
*/ */
function backup_to_google_drive() { function backup_to_google_drive()
$client = new Google\API\Drive() ; {
$cnf = OPNsense\Core\Config::getInstance(); $cnf = OPNsense\Core\Config::getInstance();
if ($cnf->isValid()) { if ($cnf->isValid()) {
$config = $cnf->object() ; $config = $cnf->object();
if ( isset($config->system->remotebackup) && isset($config->system->remotebackup->GDriveEnabled) && $config->system->remotebackup->GDriveEnabled == "on" ){ 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() ); 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) // backup source data to local strings (plain/encrypted)
$confdata = file_get_contents('/conf/config.xml') ; $confdata = file_get_contents('/conf/config.xml');
$confdata_enc = encrypt_data($confdata, $config->system->remotebackup->GDrivePassword->__toString()) ; $confdata_enc = encrypt_data($confdata, $config->system->remotebackup->GDrivePassword->__toString());
tagfile_reformat($confdata_enc, $confdata_enc, "config.xml"); tagfile_reformat($confdata_enc, $confdata_enc, "config.xml");
// read filelist (config-*.xml) // read filelist (config-*.xml)
$files = $client->listFiles($config->system->remotebackup->GDriveFolderID->__toString()); try {
$configfiles = array(); $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) { foreach ($files as $file) {
if (fnmatch("config-*.xml", $file['title'])) { if (fnmatch("config-*.xml", $file['title'])) {
$configfiles[$file['title']] = $file; $configfiles[$file['title']] = $file;
} }
} }
krsort($configfiles); krsort($configfiles);
// backup new file if changed (or if first in backup) // backup new file if changed (or if first in backup)
$target_filename = "config-".time().".xml"; $target_filename = "config-" . time() . ".xml";
if (count($configfiles) > 1) { if (count($configfiles) > 1) {
// compare last backup with current, only save new // compare last backup with current, only save new
$bck_data_enc_in = $client->download($configfiles[array_keys($configfiles)[0]]); $bck_data_enc_in = $client->download($configfiles[array_keys($configfiles)[0]]);
$bck_data_enc=""; $bck_data_enc = "";
tagfile_deformat($bck_data_enc_in, $bck_data_enc,"config.xml") ; tagfile_deformat($bck_data_enc_in, $bck_data_enc, "config.xml");
$bck_data = decrypt_data($bck_data_enc, $config->system->remotebackup->GDrivePassword->__toString()); $bck_data = decrypt_data($bck_data_enc, $config->system->remotebackup->GDrivePassword->__toString());
if ($bck_data == $confdata) { if ($bck_data == $confdata) {
$target_filename = null; $target_filename = null;
...@@ -393,7 +407,8 @@ function backup_to_google_drive() { ...@@ -393,7 +407,8 @@ function backup_to_google_drive() {
} }
if (!is_null($target_filename)) { if (!is_null($target_filename)) {
log_error("backup configuration as " . $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); krsort($configfiles);
} }
...@@ -402,16 +417,15 @@ function backup_to_google_drive() { ...@@ -402,16 +417,15 @@ function backup_to_google_drive() {
$fcount = 0; $fcount = 0;
foreach ($configfiles as $filename => $file) { foreach ($configfiles as $filename => $file) {
if ($fcount >= $config->system->remotebackup->GDriveBackupCount->__toString()) { if ($fcount >= $config->system->remotebackup->GDriveBackupCount->__toString()) {
log_error("remove " . $filename . " from Google Drive" ); log_error("remove " . $filename . " from Google Drive");
$client->delete($file); $client->delete($file);
} }
$fcount++ ; $fcount++;
} }
} }
// return filelist // return filelist
return $configfiles; return $configfiles;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment