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

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

parent eef2608a
......@@ -355,21 +355,35 @@ 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() ;
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" ){
$client->login($config->system->remotebackup->GDriveEmail->__toString(), $config->system->remotebackup->GDriveP12key->__toString() );
$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)
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'])) {
......@@ -380,12 +394,12 @@ function backup_to_google_drive() {
// 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;
}
}
......
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