backup, move google drive backup and settings into backup framework, add basic field types in diag_backup.php to render backup forms and remove crontab dependency since remote_backup doesn't really do anything when none of the providers are enabled. for https://github.com/opnsense/core/pull/2251

This commit is contained in:
Ad Schellevis 2018-03-23 19:25:05 +01:00
parent 63cfe0a96c
commit d5cdddedea
3 changed files with 182 additions and 150 deletions

View File

@ -1183,9 +1183,7 @@ function system_cron_configure($verbose = false, $defer = false)
$autocron[] = generate_cron_job('/usr/local/etc/rc.backup_captiveportal', '0', '*/' . $config['system']['captiveportalbackup']);
}
if (!empty($config['system']['remotebackup']['GDriveEnabled'])) {
$autocron[] = generate_cron_job('/usr/local/opnsense/scripts/remote_backup.php', 0, 1);
}
$autocron[] = generate_cron_job('/usr/local/opnsense/scripts/remote_backup.php', 0, 1);
/* bogons fetch always set in default config.xml */
switch ($config['system']['bogons']['interval']) {

View File

@ -49,43 +49,72 @@ class Gdrive extends Base implements IBackupProvider
$fields[] = array(
"name" => "GDriveEnabled",
"type" => "checkbox",
"label" => gettext("Enable")
"label" => gettext("Enable"),
"value" => null
);
$fields[] = array(
"name" => "GDriveEmail",
"type" => "text",
"label" => gettext("Email Address")
"label" => gettext("Email Address"),
"help" => gettext("Client-ID in the Google cloud console"),
"value" => null
);
$fields[] = array(
"name" => "GDriveP12file",
"name" => "GDriveP12key",
"type" => "file",
"label" => gettext("P12 key (not loaded)")
"label" => gettext("P12 key"),
"help" => sprintf(gettext("You need a private key in p12 format to use Google Drive, ".
"instructions on how to aquire one can be found here %s "),
"<a href='https://cloud.google.com/storage/docs/authentication#generating-a-private-key'
target='_blank'>
https://cloud.google.com/storage/docs/authentication#generating-a-private-key</a>"),
"value" => null
);
$fields[] = array(
"name" => "GDriveFolderID",
"type" => "text",
"label" => gettext("Folder ID")
"label" => gettext("Folder ID"),
"value" => null
);
$fields[] = array(
"name" => "GDrivePrefixHostname",
"type" => "text",
"label" => gettext("Prefix hostname to backupfile")
"type" => "checkbox",
"label" => gettext("Prefix hostname to backupfile"),
"help" => gettext("Normally the config xml will be written as config-stamp.xml, with this option set " .
"the filename will use the systems host and domain name."),
"value" => null
);
$fields[] = array(
"name" => "GDriveBackupCount",
"type" => "text",
"label" => gettext("Backup Count")
"label" => gettext("Backup Count"),
"value" => 60
);
$fields[] = array(
"name" => "GDrivePassword",
"type" => "password",
"label" => gettext("Password")
"label" => gettext("Password"),
"value" => null
);
$fields[] = array(
"name" => "GDrivePasswordConfirm",
"type" => "password",
"label" => gettext("Confirm")
"label" => gettext("Confirm"),
"value" => null
);
$cnf = Config::getInstance();
if ($cnf->isValid()) {
$config = $cnf->object();
foreach ($fields as &$field) {
$fieldname = $field['name'];
if (isset($config->system->remotebackup->$fieldname)) {
$field['value'] = (string)$config->system->remotebackup->$fieldname;
} elseif ($fieldname == "GDrivePasswordConfirm" &&
isset($config->system->remotebackup->GDrivePassword)) {
$field['value'] = (string)$config->system->remotebackup->GDrivePassword;
}
}
}
return $fields;
}
@ -102,11 +131,42 @@ class Gdrive extends Base implements IBackupProvider
/**
* validate and set configuration
* @param array $conf configuration array
* @return array of validation errors
* @return array of validation errors when not saved
*/
public function setConfiguration($conf)
{
// TODO: Implement setConfiguration() method.
$input_errors = array();
if ($conf['GDrivePasswordConfirm'] != $conf['GDrivePassword']) {
$input_errors[] = gettext("The supplied 'Password' and 'Confirm' field values must match.");
}
if (count($input_errors) == 0) {
$config = Config::getInstance()->object();
if (!isset($config->system->remotebackup)) {
$config->system->remotebackup = array();
}
foreach ($this->getConfigurationFields() as $field) {
$fieldname = $field['name'];
if ($field['type'] == 'file') {
if (!empty($conf[$field['name']])) {
$config->system->remotebackup->$fieldname = base64_encode($conf[$field['name']]);
}
} elseif ($field['name'] == 'GDrivePasswordConfirm') {
null; // skip password confirm field
} elseif (!empty($conf[$field['name']])) {
$config->system->remotebackup->$fieldname = $conf[$field['name']];
} else {
unset($config->system->remotebackup->$fieldname);
}
}
// remove private key when disabled
if (empty($config->system->remotebackup->GDriveEnabled) &&
isset($config->system->remotebackup->GDriveP12key)) {
unset($config->system->remotebackup->GDriveP12key);
}
Config::getInstance()->save();
}
return $input_errors;
}
/**

View File

@ -113,25 +113,28 @@ $backupFactory = new OPNsense\Backup\BackupFactory();
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$pconfig = array();
$pconfig['GDriveEnabled'] = isset($config['system']['remotebackup']['GDriveEnabled']) ? $config['system']['remotebackup']['GDriveEnabled'] : null;
$pconfig['GDrivePrefixHostname'] = isset($config['system']['remotebackup']['GDrivePrefixHostname']) ? $config['system']['remotebackup']['GDrivePrefixHostname'] : null;
$pconfig['GDriveEmail'] = isset($config['system']['remotebackup']['GDriveEmail']) ? $config['system']['remotebackup']['GDriveEmail'] : null;
$pconfig['GDriveP12key'] = isset($config['system']['remotebackup']['GDriveP12key']) ? $config['system']['remotebackup']['GDriveP12key'] : null;
$pconfig['GDriveFolderID'] = isset($config['system']['remotebackup']['GDriveFolderID']) ? $config['system']['remotebackup']['GDriveFolderID'] : null;
$pconfig['GDriveBackupCount'] = isset($config['system']['remotebackup']['GDriveBackupCount']) ? $config['system']['remotebackup']['GDriveBackupCount'] : null;
$pconfig['GDrivePassword'] = isset($config['system']['remotebackup']['GDrivePassword']) ? $config['system']['remotebackup']['GDrivePassword'] : null;
// collect all settings from backup providers
foreach ($backupFactory->listProviders() as $providerId => $provider) {
foreach ($provider['handle']->getConfigurationFields() as $field) {
$fieldId = $providerId . "_" .$field['name'];
$pconfig[$fieldId] = $field['value'];
}
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input_errors = array();
$pconfig = $_POST;
if (!empty($_POST['restore'])) {
$mode = "restore";
} elseif (!empty($_POST['download'])) {
$mode = "download";
} elseif (!empty($_POST['setup_gdrive'])) {
$mode = "setup_gdrive";
} else {
$mode = false;
$mode = null;
foreach (array_keys($backupFactory->listProviders()) as $providerName) {
if (!empty($pconfig["setup_{$providerName}"])) {
$mode = "setup_{$providerName}";
}
}
if (!empty($mode)) {
if (!empty($pconfig['restore'])) {
$mode = "restore";
} elseif (!empty($pconfig['download'])) {
$mode = "download";
}
}
if ($mode == "download") {
@ -250,58 +253,44 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$savemsg .= ' ' . gettext("The system is rebooting now. This may take one minute.");
}
}
} elseif ( $mode == "setup_gdrive" ){
if (!isset($config['system']['remotebackup'])) {
$config['system']['remotebackup'] = array() ;
}
$config['system']['remotebackup']['GDriveEnabled'] = $_POST['GDriveEnabled'];
$config['system']['remotebackup']['GDrivePrefixHostname'] = $_POST['GDrivePrefixHostname'];
$config['system']['remotebackup']['GDriveEmail'] = $_POST['GDriveEmail'] ;
$config['system']['remotebackup']['GDriveFolderID'] = $_POST['GDriveFolderID'];
$config['system']['remotebackup']['GDrivePassword'] = $_POST['GDrivePassword'];
if (is_numeric($_POST['GDriveBackupCount'])) {
$config['system']['remotebackup']['GDriveBackupCount'] = $_POST['GDriveBackupCount'];
} else {
$config['system']['remotebackup']['GDriveBackupCount'] = 60;
}
if ( $_POST['GDrivePasswordConfirm'] != $_POST['GDrivePassword'] ) {
// log error, but continue
$input_errors[] = gettext("The supplied 'Password' and 'Confirm' field values must match.");
}
if (count($input_errors) == 0) {
if (is_uploaded_file($_FILES['GDriveP12file']['tmp_name'])) {
$data = file_get_contents($_FILES['GDriveP12file']['tmp_name']);
$config['system']['remotebackup']['GDriveP12key'] = base64_encode($data);
} elseif ($config['system']['remotebackup']['GDriveEnabled'] != "on") {
unset($config['system']['remotebackup']['GDriveP12key']);
}
$savemsg = gettext("Google Drive backup settings have been saved.");
write_config();
system_cron_configure();
try {
$provider = $backupFactory->getProvider("GDrive");
$filesInBackup = $provider['handle']->backup();
} catch (Exception $e) {
$filesInBackup = array();
}
if (empty($config['system']['remotebackup']['GDriveEnabled'])) {
/* unused */
} elseif (count($filesInBackup) == 0) {
$input_errors[] = gettext("Google Drive communication failure");
} elseif (!empty($mode)){
// setup backup provider, collect provider settings and save/validate
$providerId = substr($mode, 6);
$provider = $backupFactory->getProvider($providerId);
$providerSet = array();
foreach ($provider['handle']->getConfigurationFields() as $field) {
$fieldId = $providerId . "_" .$field['name'];
if ($field['type'] == 'file') {
// extract file to sent to setConfiguration()
if (is_uploaded_file($_FILES[$fieldId]['tmp_name'])) {
$providerSet[$field['name']] = file_get_contents($_FILES[$fieldId]['tmp_name']);
} else {
$providerSet[$field['name']] = null;
}
} else {
$input_messages = gettext("Backup successful, current file list:") . "<br>";
foreach ($filesInBackup as $filename) {
$input_messages .= "<br>" . $filename;
$providerSet[$field['name']] = $pconfig[$fieldId];
}
}
$input_errors = $provider['handle']->setConfiguration($providerSet);
if (count($input_errors) == 0) {
if ($provider['handle']->isEnabled()) {
try {
$filesInBackup = $provider['handle']->backup();
} catch (Exception $e) {
$filesInBackup = array();
}
if (count($filesInBackup) == 0) {
$input_errors[] = gettext("communication failure");
} else {
$input_messages = gettext("Backup successful, current file list:") . "<br>";
foreach ($filesInBackup as $filename) {
$input_messages .= "<br>" . $filename;
}
}
}
system_cron_configure();
}
}
}
@ -437,80 +426,65 @@ $( document ).ready(function() {
</tbody>
</table>
</div>
<div class="content-box tab-content table-responsive">
<?php
foreach ($backupFactory->listProviders() as $providerId => $provider):?>
<div class="content-box tab-content table-responsive __mb">
<table class="table table-striped opnsense_standard_table_form">
<thead style="display: none;">
<tr>
<th class="col-sm-1"></th>
<th class="col-sm-3"></th>
</tr>
</thead>
<tbody>
<tr>
<th colspan="2" style="vertical-align:top" class="listtopic">
<?=gettext("Google Drive"); ?>
</th>
</tr>
<tr>
<td><?=gettext("Enable"); ?> </td>
<td>
<input name="GDriveEnabled" type="checkbox" <?=!empty($pconfig['GDriveEnabled']) ? "checked" : "";?> >
</td>
</tr>
<tr>
<td><?=gettext("Email Address"); ?> </td>
<td>
<input name="GDriveEmail" value="<?=$pconfig['GDriveEmail'];?>" type="text">
</td>
</tr>
<tr>
<td><?=gettext("P12 key"); ?> <?=!empty($pconfig['GDriveP12key']) ? gettext("(replace)") : gettext("(not loaded)"); ?> </td>
<td>
<input name="GDriveP12file" type="file">
</td>
</tr>
<tr>
<td><?=gettext("Folder ID"); ?> </td>
<td>
<input name="GDriveFolderID" value="<?=$pconfig['GDriveFolderID'];?>" type="text">
</td>
</tr>
<tr>
<td><?=gettext("Prefix hostname to backupfile"); ?> </td>
<td>
<input name="GDrivePrefixHostname" type="checkbox" <?=!empty($pconfig['GDrivePrefixHostname']) ? "checked" : "";?> >
</td>
</tr>
<tr>
<td><?=gettext("Backup Count"); ?> </td>
<td>
<input name="GDriveBackupCount" value="<?=$pconfig['GDriveBackupCount'];?>" type="text">
</td>
</tr>
<tr>
<td colspan=2><?=gettext("Password protect your data"); ?> :</td>
</tr>
<tr>
<td><?=gettext("Password :"); ?></td>
<td>
<input name="GDrivePassword" type="password" value="<?=$pconfig['GDrivePassword'];?>" />
</td>
</tr>
<tr>
<td><?=gettext("Confirm :"); ?></td>
<td>
<input name="GDrivePasswordConfirm" type="password" value="<?=$pconfig['GDrivePassword'];?>" />
</td>
</tr>
<tr>
<td>
<input name="setup_gdrive" class="btn btn-primary" id="Gdrive" value="<?=gettext("Setup/Test Google Drive");?>" type="submit">
</td>
<td></td>
</tr>
</tbody>
<thead>
<tr>
<th colspan="2"><?=$provider['handle']->getName();?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($provider['handle']->getConfigurationFields() as $field):
$fieldId = $providerId . "_" .$field['name'];?>
<tr>
<td style="width:22%;">
<a id="help_for_<?=$fieldId;?>" href="#" class="showhelp">
<i class="fa fa-info-circle <?=empty($field['help']) ? "text-muted" : "";?>"></i></a> <?=$field['label'];?>
</td>
<td>
<?php
if ($field['type'] == 'checkbox'):?>
<input name="<?=$fieldId;?>" type="checkbox" <?=!empty($pconfig[$fieldId]) ? "checked" : "";?> >
<?php
elseif ($field['type'] == 'text'):?>
<input name="<?=$fieldId;?>" value="<?=$pconfig[$fieldId];?>" type="text">
<?php
elseif ($field['type'] == 'file'):?>
<input name="<?=$fieldId;?>" type="file">
<?php
elseif ($field['type'] == 'password'):?>
<input name="<?=$fieldId;?>" type="password" value="<?=$field['value'];?>" />
<?php
endif;?>
<div class="hidden" data-for="help_for_<?=$fieldId;?>">
<?=!empty($field['help']) ? $field['help'] : "";?>
</div>
</td>
</tr>
<?php
endforeach;?>
</tbody>
<tfoot>
<tr>
<td colspan="2">
<input name="setup_<?=$providerId;?>" class="btn btn-primary"
value="<?=sprintf(gettext("Setup/Test %s"), $provider['handle']->getName());?>"
type="submit">
</td>
</tr>
</tfoot>
</table>
</div>
<?php
endforeach;?>
</section>
</form>
</div>