Skip to content

Commit

Permalink
Merge pull request #23 from emoncms/multienv
Browse files Browse the repository at this point in the history
Multi environment support for backup module
  • Loading branch information
TrystanLea authored Apr 12, 2019
2 parents e811075 + f54052b commit b6104da
Show file tree
Hide file tree
Showing 15 changed files with 408 additions and 354 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,32 @@

function backup_controller()
{
global $route, $session, $path, $redis;
global $route, $session, $path, $redis, $homedir;
$result = false;

// This module is only to be ran by the admin user
if (!$session['write'] && !$session['admin']) {
$route->format = "text";
return "<br><div class='alert alert-error'><b>Error:</b> backup module requires admin access</div>";
}
if (file_exists("$homedir/backup/config.cfg")) {
$parsed_ini = parse_ini_file("$homedir/backup/config.cfg", true);
} else if (file_exists("$homedir/modules/backup/config.cfg")) {
$parsed_ini = parse_ini_file("$homedir/modules/backup/config.cfg", true);
} else {
return "<br><div class='alert alert-error'><b>Error:</b> missing backup config.cfg</div>";
}

$export_flag = "/tmp/emoncms-flag-export";
$export_script = "/home/pi/backup/emoncms-export.sh";
$export_logfile = "/home/pi/data/emoncms-export.log";
$export_script = $parsed_ini['backup_script_location']."/emoncms-export.sh";
$export_logfile = $parsed_ini['backup_location']."/emoncms-export.log";

$import_flag = "/tmp/emoncms-flag-import";
$import_script = "/home/pi/backup/emoncms-import.sh";
$import_logfile = "/home/pi/data/emoncms-import.log";

// This module is only to be ran by the admin user
if (!$session['write'] && !$session['admin']) return array('content'=>false);
$import_script = $parsed_ini['backup_script_location']."/emoncms-import.sh";
$import_logfile = $parsed_ini['backup_location']."/emoncms-import.log";

if ($route->format == 'html' && $route->action == "") {
$result = view("Modules/backup/backup_view.php",array());
$result = view("Modules/backup/backup_view.php",array("parsed_ini"=>$parsed_ini));
}

if ($route->action == 'start') {
Expand Down Expand Up @@ -58,7 +68,7 @@ function backup_controller()
header("Content-Disposition: attachment; filename=$backup_filename");
header("Pragma: no-cache");
header("Expires: 0");
readfile("/home/pi/data/$backup_filename");
readfile($parsed_ini['backup_location']."/".$backup_filename);
exit;
}

Expand All @@ -67,7 +77,7 @@ function backup_controller()
// ini_set('upload_max_filesize', '200M');
// ini_set('post_max_size', '200M');
$uploadOk = 1;
$target_path = "/home/pi/data/uploads/";
$target_path = $parsed_ini['backup_location']."/uploads/";
$target_path = $target_path . basename( $_FILES['file']['name']);

$imageFileType = pathinfo($target_path,PATHINFO_EXTENSION);
Expand All @@ -82,9 +92,9 @@ function backup_controller()
if ((move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) && ($uploadOk == 1)) {

$redis->rpush("service-runner","$import_script $import_flag>$import_logfile");
header('Location: '.$path.'backup');
header('Location: '.$path.'backup#import');
} else {
$result = "Sorry, there was an error uploading the file";
return "<br><div class='alert alert-error'><b>Error:</b> Import archive not selected</div>";
}
}

Expand Down
26 changes: 26 additions & 0 deletions backup-module/backup_menu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
$domain = "messages";
bindtextdomain($domain, "Modules/admin/locale");
bind_textdomain_codeset($domain, 'UTF-8');

$menu_left[] = array(
'id'=>"backup_menu",
'name'=>dgettext($domain, "Backup"),
'path'=>"backup" ,
'session'=>"write",
'order' => 0,
'icon'=>'icon-circle-arrow-down icon-white',
'hideinactive'=>1
);

$menu_dropdown_config[] = array(
'id'=>"backup_menu_extras",
'name'=>dgettext($domain, "Backup"),
'path'=>"backup" ,
'session'=>"write",
'order' => 55,
'icon'=>'icon-circle-arrow-down'
);



155 changes: 155 additions & 0 deletions backup-module/backup_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
global $path, $homedir;
@exec('ps ax | grep service-runner.py | grep -v grep', $servicerunnerproc);
?>

<style>
pre {
width:80%;
height:400px;

margin:0px;
padding:0px;
font-size:16px;
color:#fff;
background-color:#300a24;
overflow: scroll;
overflow-x: hidden;

font-size:16px;
}
#export-log {
padding-left:20px;
padding-top:20px;
}
#import-log {
padding-left:20px;
padding-top:20px;
}
</style>
<link rel="stylesheet" href="<?php echo $path; ?>Lib/misc/sidebar.css">

<div id="wrapper">
<div class="sidenav">
<div class="sidenav-inner">
<ul class="sidenav-menu">
<li><a href="#export">Export</a></li>
<li><a href="#import">Import</a></li>
</ul>
</div>
</div>

<div style="height:20px"></div>

<?php
if (empty($servicerunnerproc)) {
echo "<div class='alert alert-error'><b>Warning:</b> service-runner is not running and is required. To install service-runner see <a href='https://github.com/emoncms/emoncms/blob/master/scripts/services/install-service-runner-update.md'>service-runner installation</a></div>";
}
?>
<div id="view-export">
<h3>Export</h3>
<p>Export a compressed archive containing:</p>
<ul>
<li>Emoncms MYSQL database</li>
<li>PHPFina data files</li>
<li>PHPTimeSeries data files</li>
<li>EmonHub Config</li>
</ul>
<p>These files contain all Emoncms data including:</p>
<ul>
<li>Input processes</li>
<li>Feed data</li>
<li>Dashboards</li>
<li>EmonHub Config</li>
</ul>
<p>The compressed archive can be used to migrate data to another emonPi / emonBase.</p>
<button id="emonpi-backup" class="btn btn-info"><?php echo _('Create backup'); ?></button>
<br><br>
<pre id="export-log-bound"><div id="export-log"></div></pre>
<?php
$backup_filename="emoncms-backup-".date("Y-m-d").".tar.gz";
if (file_exists($parsed_ini['backup_location']."/".$backup_filename) && !file_exists("/tmp/backuplock")) {
echo '<br><br><b>Right Click > Download:</b><br><a href="'.$path.'backup/download">'.$backup_filename.'</a>';
}
?>
<br><br>
<p>Once export is complete refresh page to see download link.</p>
<p><i>Note: Export can take a long some time, please be patient.</i></p>
</div>

<div id="view-import">
<h3>Import</h3>
<p>Import an emoncms backup archive.</p>
<span style="color:red;font-weight:bold;">CAUTION ALL EMONCMS ACCOUNT DATA WILL BE OVERWRITTEN BY THE IMPORTED DATA</span><br><br>
<p><i>Note: Before import update to latest version of Emoncms & emonHub.</i></p>
<form action="<?php echo $path; ?>backup/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br><br>
<input class="btn btn-danger" type="submit" name="submit" value="Import Backup">
</form>
<br><br>
<p><i>Note: If browser upload fails for large backup files <a href="http://github.com/emoncms/backup">follow manual import instructions.</a></i></p>
<pre id="import-log-bound"><div id="import-log"></div></pre>
<br>
<p><i>Refresh page if log window does not update.</i></p>
<p><i>After import is complete logout then login using the new imported account login details.</i></p>
</div>
</div>
<script type="text/javascript" src="<?php echo $path; ?>Lib/misc/sidebar.js"></script>

<script>
init_sidebar({menu_element:"#backup_menu"});
var path = "<?php echo $path; ?>";

$("#view-import").hide();
if (location.hash=="#export") { $("#view-import").hide(); $("#view-export").show(); }
if (location.hash=="#import") { $("#view-import").show(); $("#view-export").hide(); }

$(window).on('hashchange', function() {
if (location.hash=="#export") { $("#view-import").hide(); $("#view-export").show(); }
if (location.hash=="#import") { $("#view-import").show(); $("#view-export").hide(); }
});

export_log_update();
import_log_update();
var export_updater = false;
var import_updater = false;
export_updater = setInterval(export_log_update,1000);
import_updater = setInterval(import_log_update,1000);

$("#emonpi-backup").click(function() {
$.ajax({ url: path+"backup/start", async: true, dataType: "text", success: function(result) {
$("#export-log").html(result);
clearInterval(export_updater);
export_updater = setInterval(export_log_update,1000);
}
});
});

function export_log_update() {
$.ajax({ url: path+"backup/exportlog", async: true, dataType: "text", success: function(result)
{
$("#export-log").html(result);
document.getElementById("export-log-bound").scrollTop = document.getElementById("export-log-bound").scrollHeight

if (result.indexOf("=== Emoncms export complete! ===")!=-1) {
clearInterval(export_updater);
}
}
});
}

function import_log_update() {
$.ajax({ url: path+"backup/importlog", async: true, dataType: "text", success: function(result)
{
if (result=="backup module requires admin access") location.replace("/");
$("#import-log").html(result);
document.getElementById("import-log-bound").scrollTop = document.getElementById("import-log-bound").scrollHeight


if (result.indexOf("=== Emoncms import complete! ===")!=-1) {
clearInterval(import_updater);
}
}
});
}
</script>
File renamed without changes.
5 changes: 0 additions & 5 deletions backup/backup_menu.php

This file was deleted.

134 changes: 0 additions & 134 deletions backup/backup_view.php

This file was deleted.

Loading

0 comments on commit b6104da

Please sign in to comment.