-
-
Notifications
You must be signed in to change notification settings - Fork 319
Expand file tree
/
Copy pathbackups.php
More file actions
195 lines (166 loc) · 5.62 KB
/
backups.php
File metadata and controls
195 lines (166 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!--
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();
function filesize_compat($file)
{
if(PHP_INT_SIZE === 4) # workaround for 32-bit architectures
return trim(shell_exec("stat -c%s " . escapeshellarg($file)));
else
return filesize($file);
}
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;
$cache_file = '/var/www/ncp-web/backup-info-cache.cfg';
if (file_exists($cache_file)) {
$cache_str = file_get_contents($cache_file)
or exit("error opening ${cache_file}");
$cache = json_decode($cache_str, true);
if (!is_array($cache)) {
$cache = [];
}
} else {
$cache = [];
}
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_compat($bkp)/1024/1024) . " MiB";
$has_data = '';
$ret = null;
if (array_key_exists($bkp, $cache)) {
$ret = $cache[$bkp];
$cache_new[$bkp] = $ret;
}
if ($ret === null)
{
exec("sudo /home/www/ncp-backup-launcher.sh bkp " . escapeshellarg($bkp) . " \"$compressed\"", $output, $ret);
$cache_new[$bkp] = $ret;
}
if ($ret == 0)
$has_data = '✓';
echo <<<HTML
<tr id="$bkp">
<td class="long-field" title="$bkp">$date </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() . '"/>';
}
}
$cache_str = json_encode($cache_new)
or exit('internal error');
file_put_contents($cache_file, $cache_str)
or exit("error writing ${cache_file}");
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
-->