Skip to content

Commit

Permalink
ncp-web: add backups panel
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoparker committed May 1, 2019
1 parent 01cd421 commit f34354c
Show file tree
Hide file tree
Showing 15 changed files with 746 additions and 25 deletions.
3 changes: 2 additions & 1 deletion bin/ncp/BACKUPS/nc-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ tar $compress_arg -cf "$destfile" \
exit 1
}
rm "$dbbackup"
chmod 600 "$destfile"
chmod 640 "$destfile"
chown :www-data "$destfile"
echo "backup $destfile generated"
EOF
Expand Down
10 changes: 7 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@

[v1.11.4](https://github.com/nextcloud/nextcloudpi/commit/62a7f45) (2019-04-28) letsencrypt: switch to apt version
[v1.12.0](https://github.com/nextcloud/nextcloudpi/commit/703ff6f) (2019-04-29) ncp-web: add backups panel

[v1.11.3 ](https://github.com/nextcloud/nextcloudpi/commit/71d8f52) (2019-04-09) nc-restore: check btrfs command
[v1.11.5](https://github.com/nextcloud/nextcloudpi/commit/01cd421) (2019-04-29) letsencrypt: force renewal by default

[v1.11.2, master](https://github.com/nextcloud/nextcloudpi/commit/3754609) (2019-04-06) armbian: fix uu
[v1.11.4 ](https://github.com/nextcloud/nextcloudpi/commit/b3c7d13) (2019-04-28) letsencrypt: switch to apt version

[v1.11.3 ](https://github.com/nextcloud/nextcloudpi/commit/02efd61) (2019-04-09) nc-restore: check btrfs command

[v1.11.2 ](https://github.com/nextcloud/nextcloudpi/commit/3754609) (2019-04-06) armbian: fix uu

[v1.11.1 ](https://github.com/nextcloud/nextcloudpi/commit/a712935) (2019-04-05) nc-backup: fix space calculation

Expand Down
159 changes: 159 additions & 0 deletions ncp-web/backups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<!--
NextCloudPi Web Backups Panel

Copyleft 2019 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
GPL licensed (see end of file) * Use at your own risk!

More at https://nextcloudpi.com
-->
<?php

$bkp_cfg = file_get_contents('/usr/local/etc/ncp-config.d/nc-backup.cfg') or exit('backup config not found');
$bkp_auto_cfg = file_get_contents('/usr/local/etc/ncp-config.d/nc-backup-auto.cfg') or exit('backup config not found');

$bkp_json = json_decode($bkp_cfg , true) or exit('invalid format');
$bkp_auto_json = json_decode($bkp_auto_cfg, true) or exit('invalid format');

$bkp_dir = $bkp_json['params'][0]['value'];
$bkp_auto_dir = $bkp_auto_json['params'][1]['value'];

$bkps = array();
$bkps_auto = array();

if (file_exists($bkp_dir))
{
$bkps = array_diff(scandir($bkp_dir), array('.', '..'));
$bkps = preg_filter('/^/', $bkp_dir. '/', $bkps);
}

if (file_exists($bkp_auto_dir))
{
$bkps_auto = array_diff(scandir($bkp_auto_dir), array('.', '..'));
$bkps_auto = preg_filter('/^/', $bkp_auto_dir . '/', $bkps_auto);
}

$bkps = array_unique(array_merge($bkps, $bkps_auto));

if (!empty($bkps))
{
echo <<<HTML
<div id="backups-table">
<table class="dashtable backuptable">
<th>Date</th><th>Size</th><th>Compressed</th><th>Data</th><th></th>
HTML;
foreach ($bkps as $bkp)
{
$extension = pathinfo($bkp, PATHINFO_EXTENSION);
if ($extension === "tar" || $extension === "gz")
{
$compressed = "";
if ($extension === "gz")
$compressed = '';

$date = date("Y M d @ H:i", filemtime($bkp));
$size = round(filesize($bkp)/1024/1024) . " MiB";

$has_data = '';
exec("sudo /home/www/ncp-backup-launcher.sh bkp " . escapeshellarg($bkp) . " \"$compressed\"", $output, $ret);
if ($ret == 0)
$has_data = '';

echo <<<HTML
<tr id="$bkp">
<td class="long-field" title="$bkp">$date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td class="val-field">$size</td>
<td class="ok-field align-center">$compressed</td>
<td class="ok-field align-center">$has_data</td>
<td>
<img class="hidden-btn default-btn download-bkp" title="download" src="../img/download.svg">
<img class="hidden-btn default-btn delete-bkp" title="delete" src="../img/delete.svg">
<img class="hidden-btn default-btn restore-bkp" title="restore" src="../img/defaults.svg">
</td>
</tr>
HTML;
echo '<input type="hidden" name="csrf-token" value="' . getCSRFToken() . '"/>';
}
}
echo <<<HTML
</table>
</div>
HTML;
} else {
echo "<div>No backups found.</div>";
}
?>

</br></br>
<h2 class="text-title">Restore from file</h2>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<div class="restore-upload-btn-wrapper">
<input type="file" name="backup" id="restore-upload" accept=".tar,.tar.gz"/>
<input id="restore-upload-btn" type="submit" value="Restore"/>
</div>
</form>
</br></br>

<h2 class="text-title"><?php echo $l->__("Snapshots"); ?></h2>

<?php

include( '/var/www/nextcloud/config/config.php' );

$snap_dir = realpath($CONFIG['datadirectory'] . '/../ncp-snapshots');
$snaps = array();
if (file_exists($snap_dir))
{
$snaps = array_diff(scandir($snap_dir), array('.', '..'));
$snaps = preg_filter('/^/', $snap_dir . '/', $snaps);
}

if (!empty($snaps))
{
echo <<<HTML
<div id="snapshots-table">
<table class="dashtable backuptable">
HTML;
foreach ($snaps as $snap)
{
exec("sudo /home/www/ncp-backup-launcher.sh chksnp " . escapeshellarg($snap), $out, $ret);
if ($ret == 0)
{
$snap_name = basename($snap);
echo <<<HTML
<tr id="$snap">
<td class="text-align-left" title="$snap">$snap_name</td>
<td>
<img class="hidden-btn default-btn delete-snap" title="delete" src="../img/delete.svg">
<img class="hidden-btn default-btn restore-snap" title="restore" src="../img/defaults.svg">
</td>
</tr>
HTML;
}
}
echo <<<HTML
</table>
</div>
HTML;
} else {
echo "<div>No snapshots found.</div>";
}
?>

<!--
License

This script is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This script is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this script; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
-->
56 changes: 52 additions & 4 deletions ncp-web/css/ncp.css
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ select {
display: none;
}

#loading-info-gif {
.loading-section-gif {
display: flex;
justify-content: center;
align-items: center;
Expand Down Expand Up @@ -1150,6 +1150,9 @@ select {
.icon-search {
background-image: url('../img/search.svg');
}
.icon-backups {
background-image: url('../img/defaults-white.svg');
}
.icon-config {
background-image: url('../img/settings-white.svg');
}
Expand Down Expand Up @@ -1223,6 +1226,18 @@ a#versionlink:hover {
max-width: 210px;
}

#confirmation-dialog {
position:fixed;
top:0;
bottom:0;
height:100%;
width:100%;
background-color:rgba(0, 0, 0, 0.5);
z-index:9000;
text-align:center;
cursor:pointer;
}

.dialog {
display:block;
background: white;
Expand All @@ -1249,7 +1264,7 @@ a#versionlink:hover {
opacity:0.75
}

#close-wizard {
.close-dialog-x {
position: absolute;
top: 5px;
right: 5px;
Expand Down Expand Up @@ -1319,6 +1334,23 @@ a#versionlink:hover {
border-bottom: 1px solid #ebebeb;
}

.backuptable td {
text-align: right;
}
.text-align-left {
text-align: left !important;
padding-left: 1em;
}
.backuptable th {
text-align: center;
}
#backups-content div {
text-align: center;
}
.align-center {
text-align: center !important;
}

#dashboard-suggestions {
margin-bottom: 1em;
}
Expand Down Expand Up @@ -1353,7 +1385,23 @@ a#versionlink:hover {
-ms-filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=50)';
opacity: 0.5;
}

.hidden-btn {
cursor: pointer;
-ms-filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=0)';
opacity: 0;
}

.backuptable tr:hover img {
-ms-filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=50)';
opacity: 0.5;
}
.pwd-btn:hover, .default-btn:hover {
-ms-filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=100)';
opacity: 1;
-ms-filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=100)' !important;
opacity: 1 !important;
}
.restore-upload-btn-wrapper {
display: flex;
flex-direction: row;
align-items: center;
}
75 changes: 75 additions & 0 deletions ncp-web/download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
///
// NextCloudPi Web Panel backend
//
// Copyleft 2019 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
// GPL licensed (see end of file) * Use at your own risk!
//
// More at https://nextcloudpi.com
///

include ('csrf.php');
session_start();

// CSRF check
$token = isset($_REQUEST['token']) ? $_REQUEST['token'] : '';
if ( empty($token) || !validateCSRFToken($token) )
exit('Unauthorized download');

if (!isset($_REQUEST["bkp"]))
die();

$file = $_REQUEST["bkp"];

if (!file_exists($file))
die('File not found');

if (!is_readable($file))
die('NCP does not have read permissions on this file');

$size = filesize($file);

$extension = pathinfo($file, PATHINFO_EXTENSION);
if ($extension === "tar" )
$mime_type = 'application/x-tar';
else if( $extension === "gz")
$mime_type = 'application/x-gzip';
else
die();

ob_start();
ob_clean();
header('Content-Description: File Transfer');
header('Content-Type: ' . $mime_type);
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
header('Content-Length: ' . $size);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');

$chunksize = 8 * (1024 * 1024);
if($size > $chunksize)
{
$handle = fopen($file, 'rb') or die("Error opening file");

while (!feof($handle))
{
$buffer = fread($handle, $chunksize);
echo $buffer;

ob_flush();
flush();
}

fclose($handle);
}
else
readfile($file);

ob_flush();
flush();

exit();

?>
2 changes: 0 additions & 2 deletions ncp-web/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ function print_config_forms( $l /* translations l10n object */ )
$cfg_dir = '/usr/local/etc/ncp-config.d/';
$d_iterator = new RecursiveDirectoryIterator($bin_dir);
$iterator = new RecursiveIteratorIterator($d_iterator);
$objects = new RegexIterator($iterator, '/^.+\.sh$/i', RecursiveRegexIterator::GET_MATCH);

$ret = "";
$sections = array_diff(scandir($bin_dir), array('.', '..', 'l10n'));
Expand Down Expand Up @@ -167,7 +166,6 @@ function print_sidebar( $l /* translations l10n object */, $ticks /* wether to c
$cfg_dir = '/usr/local/etc/ncp-config.d/';
$d_iterator = new RecursiveDirectoryIterator($bin_dir);
$iterator = new RecursiveIteratorIterator($d_iterator);
$objects = new RegexIterator($iterator, '/^.+\.sh$/i', RecursiveRegexIterator::GET_MATCH);

$ret = "";
$sections = array_diff(scandir($bin_dir), array('.', '..', 'l10n'));
Expand Down
Loading

0 comments on commit f34354c

Please sign in to comment.