5 |
use BackupPC::CGI::Lib qw(:all); |
use BackupPC::CGI::Lib qw(:all); |
6 |
use BackupPC::Attrib qw(:all); |
use BackupPC::Attrib qw(:all); |
7 |
use DBI; |
use DBI; |
8 |
|
use DateTime; |
9 |
use vars qw(%In $MyURL); |
use vars qw(%In $MyURL); |
10 |
|
use Time::HiRes qw/time/; |
11 |
|
|
12 |
my $on_page = 100; |
my $on_page = 100; |
13 |
my $pager_pages = 10; |
my $pager_pages = 10; |
14 |
|
|
15 |
|
my $dsn = $Conf{SearchDSN}; |
16 |
|
my $db_user = $Conf{SearchUser} || ''; |
17 |
|
|
18 |
sub getUnits() { |
sub getUnits() { |
19 |
my @ret = (); |
my @ret; |
20 |
my $tmp; |
|
21 |
my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", |
my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); |
22 |
"", "", { RaiseError => 1, AutoCommit => 1 } ); |
my $sth = $dbh->prepare(qq{ SELECT id, share FROM shares} ); |
23 |
my $st = |
$sth->execute(); |
24 |
$dbh->prepare( |
push @ret, { 'id' => '', 'share' => '-'}; # dummy any |
25 |
" SELECT shares.ID AS ID, shares.share AS name FROM shares;"); |
|
26 |
$st->execute(); |
while ( my $row = $sth->fetchrow_hashref() ) { |
27 |
push (@ret, { 'ID' => '', 'name' => '-'}); |
push @ret, $row; |
28 |
while ( $tmp = $st->fetchrow_hashref() ) { |
} |
29 |
push( @ret, { 'ID' => $tmp->{'ID'}, 'name' => $tmp->{'name'} } ); |
$dbh->disconnect(); |
30 |
} |
return @ret; |
31 |
$dbh->disconnect(); |
} |
32 |
return @ret; |
|
33 |
|
sub epoch_to_iso { |
34 |
|
my $t = shift || return; |
35 |
|
$t += 60 * 60 * +2; # FIXME add TZ |
36 |
|
my $dt = DateTime->from_epoch( epoch => $t ) || return; |
37 |
|
print STDERR "BUG: $t != " . $dt->epoch . "\n" unless ($t == $dt->epoch); |
38 |
|
return $dt->ymd . ' ' . $dt->hms; |
39 |
} |
} |
40 |
|
|
41 |
sub getWhere($) { |
sub getWhere($) { |
42 |
my ($param) = @_; |
my ($param) = @_; |
43 |
my @conditions; |
my @conditions; |
44 |
|
|
45 |
sub mk_iso_date($$) { |
sub mk_epoch_date($$) { |
46 |
my ($name,$suffix) = @_; |
my ($name,$suffix) = @_; |
47 |
|
|
48 |
my $yyyy = $param->{ $name . '_year_' . $suffix} || return; |
my $yyyy = $param->{ $name . '_year_' . $suffix} || return; |
50 |
( $suffix eq 'from' ? 1 : 12); |
( $suffix eq 'from' ? 1 : 12); |
51 |
my $dd .= $param->{ $name . '_day_' . $suffix} || |
my $dd .= $param->{ $name . '_day_' . $suffix} || |
52 |
( $suffix eq 'from' ? 1 : 31); |
( $suffix eq 'from' ? 1 : 31); |
53 |
return sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd); |
my $dt = new DateTime( |
54 |
} |
year => $yyyy, |
55 |
|
month => $mm, |
56 |
|
day => $dd |
57 |
|
); |
58 |
|
return $dt->epoch || 'NULL'; |
59 |
|
} |
60 |
|
|
61 |
|
my $backup_from = mk_epoch_date('search_backup', 'from'); |
62 |
|
push @conditions, qq{ backups.date >= $backup_from } if ($backup_from); |
63 |
|
my $backup_to = mk_epoch_date('search_backup', 'to'); |
64 |
|
push @conditions, qq{ backups.date <= $backup_to } if ($backup_to); |
65 |
|
|
66 |
|
my $files_from = mk_epoch_date('search', 'from'); |
67 |
|
push @conditions, qq{ files.date >= $files_from } if ($files_from); |
68 |
|
my $files_to = mk_epoch_date('search', 'to'); |
69 |
|
push @conditions, qq{ files.date <= $files_to } if ($files_to); |
70 |
|
|
71 |
my $backup_from = mk_iso_date('search_backup', 'from'); |
print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:" . join(" | ",@conditions); |
|
push @conditions, qq{ date(backups.date, 'unixepoch','localtime') >= '$backup_from' } if ($backup_from); |
|
|
my $backup_to = mk_iso_date('search_backup', 'to'); |
|
|
push @conditions, qq{ date(backups.date, 'unixepoch','localtime') <= '$backup_to' } if ($backup_to); |
|
|
|
|
|
my $files_from = mk_iso_date('search', 'from'); |
|
|
push @conditions, qq{ date(files.date, 'unixepoch','localtime') >= '$files_from' } if ($files_from); |
|
|
my $files_to = mk_iso_date('search', 'to'); |
|
|
push @conditions, qq{ date(files.date, 'unixepoch','localtime') <= '$files_to' } if ($files_to); |
|
|
|
|
|
print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:",join(" | ",@conditions); |
|
72 |
|
|
73 |
push( @conditions, ' backups.hostID = ' . $param->{'search_host'} ) if ($param->{'search_host'}); |
push( @conditions, ' files.shareid = ' . $param->{'search_share'} ) if ($param->{'search_share'}); |
74 |
|
|
75 |
push (@conditions, " upper(files.name) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'}); |
push (@conditions, " upper(files.path) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'}); |
76 |
|
|
77 |
return ( |
return ( |
78 |
join(" and ", @conditions), |
join(" and ", @conditions), |
85 |
sub getFiles($$) { |
sub getFiles($$) { |
86 |
my ($where, $offset) = @_; |
my ($where, $offset) = @_; |
87 |
|
|
88 |
my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", |
my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); |
|
"", "", { RaiseError => 1, AutoCommit => 1 } ); |
|
89 |
|
|
90 |
my $sql_cols = qq{ |
my $sql_cols = qq{ |
91 |
files.id AS fid, |
files.id AS fid, |
95 |
files.backupNum AS backupNum, |
files.backupNum AS backupNum, |
96 |
files.name AS filename, |
files.name AS filename, |
97 |
files.path AS filepath, |
files.path AS filepath, |
98 |
shares.share||files.fullpath AS networkPath, |
files.date AS date, |
|
date(files.date, 'unixepoch', 'localtime') AS date, |
|
99 |
files.type AS filetype, |
files.type AS filetype, |
100 |
files.size AS size, |
files.size AS size, |
101 |
dvds.name AS dvd |
dvds.name AS dvd |
105 |
FROM files |
FROM files |
106 |
INNER JOIN shares ON files.shareID=shares.ID |
INNER JOIN shares ON files.shareID=shares.ID |
107 |
INNER JOIN hosts ON hosts.ID = shares.hostID |
INNER JOIN hosts ON hosts.ID = shares.hostID |
108 |
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID |
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID |
109 |
|
}; |
110 |
|
|
111 |
|
my $sql_dvd_from = qq{ |
112 |
LEFT JOIN dvds ON dvds.ID = files.dvdid |
LEFT JOIN dvds ON dvds.ID = files.dvdid |
113 |
}; |
}; |
114 |
|
|
116 |
$sql_where = " WHERE ". $where if ($where); |
$sql_where = " WHERE ". $where if ($where); |
117 |
|
|
118 |
my $sql_order = qq{ |
my $sql_order = qq{ |
119 |
ORDER BY files.id |
ORDER BY files.date |
120 |
LIMIT $on_page |
LIMIT $on_page |
121 |
OFFSET ? |
OFFSET ? |
122 |
}; |
}; |
123 |
|
|
124 |
|
my $sql_count = qq{ select count(files.id) $sql_from $sql_where }; |
125 |
|
my $sql_results = qq{ select $sql_cols $sql_from $sql_dvd_from $sql_where $sql_order }; |
126 |
|
|
127 |
$offset ||= 0; |
$offset ||= 0; |
128 |
$offset = ($offset * $on_page) + 1; |
$offset = ($offset * $on_page); |
129 |
|
|
130 |
my $sth = $dbh->prepare(qq{ select count(files.id) $sql_from $sql_where }); |
my $sth = $dbh->prepare($sql_count); |
131 |
$sth->execute(); |
$sth->execute(); |
|
|
|
132 |
my ($results) = $sth->fetchrow_array(); |
my ($results) = $sth->fetchrow_array(); |
133 |
|
|
134 |
$sth = $dbh->prepare(qq{ select $sql_cols $sql_from $sql_where $sql_order }); |
$sth = $dbh->prepare($sql_results); |
135 |
$sth->execute( $offset ); |
$sth->execute( $offset ); |
136 |
|
|
137 |
|
if ($sth->rows != $results) { |
138 |
|
my $bug = "$0 BUG: [[ $sql_count ]] = $results while [[ $sql_results ]] = " . $sth->rows; |
139 |
|
$bug =~ s/\s+/ /gs; |
140 |
|
print STDERR "$bug\n"; |
141 |
|
} |
142 |
|
|
143 |
my @ret; |
my @ret; |
144 |
|
|
145 |
while (my $row = $sth->fetchrow_hashref()) { |
while (my $row = $sth->fetchrow_hashref()) { |
147 |
'hname' => $row->{'hname'}, |
'hname' => $row->{'hname'}, |
148 |
'sname' => $row->{'sname'}, |
'sname' => $row->{'sname'}, |
149 |
'sharename' => $row->{'sharename'}, |
'sharename' => $row->{'sharename'}, |
150 |
'backupno' => $row->{'backupNum'}, |
'backupno' => $row->{'backupnum'}, |
151 |
'fname' => $row->{'filename'}, |
'fname' => $row->{'filename'}, |
152 |
'fpath' => $row->{'filepath'}, |
'fpath' => $row->{'filepath'}, |
153 |
'networkpath' => $row->{'networkPath'}, |
'networkpath' => $row->{'networkpath'}, |
154 |
'date' => $row->{'date'}, |
'date' => $row->{'date'}, |
155 |
'type' => $row->{'filetype'}, |
'type' => $row->{'filetype'}, |
156 |
'size' => $row->{'size'}, |
'size' => $row->{'size'}, |
158 |
'dvd' => $row->{'dvd'} |
'dvd' => $row->{'dvd'} |
159 |
}); |
}); |
160 |
} |
} |
161 |
|
|
162 |
$sth->finish(); |
$sth->finish(); |
163 |
$dbh->disconnect(); |
$dbh->disconnect(); |
164 |
return ($results, \@ret); |
return ($results, \@ret); |
165 |
} |
} |
166 |
|
|
167 |
sub getBackupsNotBurned() |
sub getBackupsNotBurned() { |
168 |
{ |
|
169 |
my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", |
my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); |
170 |
"", "", { RaiseError => 1, AutoCommit => 1 } ); |
my $sql = q{ |
171 |
my $sql = q{ |
SELECT |
172 |
SELECT |
backups.hostID AS hostid, |
173 |
hosts.ID AS hostID, |
min(hosts.name) AS host, |
174 |
hosts.name AS host, |
backups.num AS backupno, |
175 |
backups.num AS backupno, |
min(backups.type) AS type, |
176 |
backups.type AS type, |
min(backups.date) AS date, |
177 |
backups.date AS date |
min(backups.size) AS size |
178 |
FROM backups, shares, files, hosts |
FROM backups |
179 |
WHERE |
INNER JOIN hosts ON hosts.ID = backups.hostID |
180 |
backups.num = files.backupNum AND |
WHERE |
181 |
shares.ID = files.shareID AND |
files.dvdid IS NULL |
182 |
backups.hostID = shares.hostID AND |
GROUP BY |
183 |
hosts.ID = backups.hostID AND |
backups.hostID, backups.num |
184 |
files.dvdid IS NULL |
ORDER BY min(backups.date) |
185 |
GROUP BY |
}; |
186 |
backups.hostID, backups.num |
my $sth = $dbh->prepare( $sql ); |
187 |
}; |
my @ret; |
188 |
my $st = $dbh -> prepare( $sql ); |
$sth->execute(); |
189 |
my @ret = (); |
|
190 |
$st -> execute(); |
while ( my $row = $sth->fetchrow_hashref() ) { |
191 |
|
$row->{'age'} = sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) ); |
192 |
while ( my $row = $st -> fetchrow_hashref() ) |
$row->{'size'} = sprintf("%0.2f", $row->{'size'} / 1024 / 1024); |
193 |
{ |
push @ret, $row; |
|
push(@ret, { |
|
|
'host' => $row->{'host'}, |
|
|
'hostid' => $row->{'hostID'}, |
|
|
'backupno' => $row->{'backupno'}, |
|
|
'type' => $row->{'type'}, |
|
|
'date' => $row->{'date'} |
|
|
} |
|
|
); |
|
194 |
} |
} |
195 |
|
|
196 |
return @ret; |
return @ret; |
197 |
} |
} |
198 |
|
|
199 |
sub displayBackupsGrid() |
sub displayBackupsGrid() |
200 |
{ |
{ |
233 |
if ($addForm) { |
if ($addForm) { |
234 |
$retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>"; |
$retHTML .= "<td class=\"tableheader\"><input type=\"checkbox\" name=\"allFiles\" onClick=\"checkAll('allFiles');\"></td>"; |
235 |
} |
} |
236 |
$retHTML .= qq{<td class="tableheader">Host</td><td class="tableheader">Backup no</td><td class="tableheader">Type</td><td class="tableheader">date</td></tr>}; |
$retHTML .= qq{ |
237 |
|
<td class="tableheader">Host</td> |
238 |
|
<td class="tableheader">Backup no</td> |
239 |
|
<td class="tableheader">Type</td> |
240 |
|
<td class="tableheader">date</td> |
241 |
|
<td class="tableheader">age/days</td> |
242 |
|
<td class="tableheader">size/MB</td> |
243 |
|
</tr> |
244 |
|
}; |
245 |
|
|
246 |
my @backups = getBackupsNotBurned(); |
my @backups = getBackupsNotBurned(); |
247 |
my $backup; |
my $backup; |
248 |
|
|
249 |
if ($addForm) { |
if ($addForm) { |
250 |
$retHTML .= qq{<tr><td colspan=7 style="tableheader"> |
$retHTML .= qq{ |
251 |
|
<tr><td colspan=7 style="tableheader"> |
252 |
<input type="submit" value="Burn selected backups on medium" name="submitBurner"> |
<input type="submit" value="Burn selected backups on medium" name="submitBurner"> |
253 |
</td></tr>}; |
</td></tr> |
254 |
|
}; |
255 |
} |
} |
256 |
|
|
257 |
foreach $backup(@backups) { |
foreach $backup(@backups) { |
260 |
|
|
261 |
$retHTML .= "<tr>"; |
$retHTML .= "<tr>"; |
262 |
if ($addForm) { |
if ($addForm) { |
263 |
$retHTML .= qq{<td class="fview"><input type="checkbox" name="fcb} . |
$retHTML .= '<td class="fview"><input type="checkbox" name="fcb' . |
264 |
$backup->{'hostid'}."_".$backup->{'backupno'} . |
$backup->{'hostid'}.'_'.$backup->{'backupno'} . |
265 |
qq{" value="} . $backup->{'hostid'}."_".$backup->{'backupno'} . |
'" value="' . $backup->{'hostid'}.'_'.$backup->{'backupno'} . |
266 |
qq{"></td>}; |
'"></td>'; |
267 |
} |
} |
268 |
|
|
269 |
$retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' . |
$retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' . |
270 |
'<td class="fviewborder">' . $backup->{'backupno'} . '</td>' . |
'<td class="fviewborder">' . $backup->{'backupno'} . '</td>' . |
271 |
'<td class="fviewborder">' . $backup->{'type'} . '</td>' . |
'<td class="fviewborder">' . $backup->{'type'} . '</td>' . |
272 |
'<td class="fviewborder">' . $backup->{'date'} . '<td>' . |
'<td class="fviewborder">' . epoch_to_iso( $backup->{'date'} ) . '</td>' . |
273 |
|
'<td class="fviewborder">' . $backup->{'age'} . '</td>' . |
274 |
|
'<td class="fviewborder">' . $backup->{'size'} . '</td>' . |
275 |
'</tr>'; |
'</tr>'; |
276 |
} |
} |
277 |
|
|
288 |
my ($where, $addForm, $offset, $hilite) = @_; |
my ($where, $addForm, $offset, $hilite) = @_; |
289 |
my $retHTML = ""; |
my $retHTML = ""; |
290 |
|
|
291 |
|
my $start_t = time(); |
292 |
|
|
293 |
|
my ($results, $files) = getFiles($where, $offset); |
294 |
|
|
295 |
|
my $dur_t = time() - $start_t; |
296 |
|
my $dur = sprintf("%0.4fs", $dur_t); |
297 |
|
|
298 |
|
my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page); |
299 |
|
|
300 |
|
if ($results <= 0) { |
301 |
|
$retHTML .= qq{ |
302 |
|
<p style="color: red;">No results found...</p> |
303 |
|
}; |
304 |
|
return $retHTML; |
305 |
|
} else { |
306 |
|
# DEBUG |
307 |
|
#use Data::Dumper; |
308 |
|
#$retHTML .= '<pre>' . Dumper($files) . '</pre>'; |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
if ($addForm) { |
if ($addForm) { |
313 |
$retHTML .= qq{<form name="forma" method="GET" action="$MyURL">}; |
$retHTML .= qq{<form name="forma" method="GET" action="$MyURL">}; |
314 |
$retHTML.= qq{<input type="hidden" value="search" name="action">}; |
$retHTML.= qq{<input type="hidden" value="search" name="action">}; |
315 |
$retHTML .= qq{<input type="hidden" value="results" name="search_results">}; |
$retHTML .= qq{<input type="hidden" value="results" name="search_results">}; |
316 |
} |
} |
317 |
|
|
|
my ($results, $files) = getFiles($where, $offset); |
|
|
|
|
|
my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page); |
|
318 |
|
|
319 |
$retHTML .= qq{ |
$retHTML .= qq{ |
320 |
<br/>Found $results files, showing $from - $to |
<br/>Found <b>$results files</b> showing <b>$from - $to</b> (took $dur) |
321 |
<table style="fview" width="100%"> |
<table style="fview" width="100%"> |
322 |
<tr> |
<tr> |
323 |
<td class="tableheader">Share</td> |
<td class="tableheader">Share</td> |
355 |
$typeStr, |
$typeStr, |
356 |
restore_link( $typeStr, $file->{'hname'}, $file->{'backupno'}, $file->{'sname'}, $file->{'fpath'}, $file->{'backupno'} ), |
restore_link( $typeStr, $file->{'hname'}, $file->{'backupno'}, $file->{'sname'}, $file->{'fpath'}, $file->{'backupno'} ), |
357 |
$file->{'size'}, |
$file->{'size'}, |
358 |
$file->{'date'}, |
epoch_to_iso( $file->{'date'} ), |
359 |
$file->{'dvd'} |
$file->{'dvd'} |
360 |
)) { |
)) { |
361 |
$retHTML .= qq{<td class="fviewborder">$v</td>}; |
$retHTML .= qq{<td class="fviewborder">$v</td>}; |