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; |
my $tmp; |
21 |
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 } ); |
|
22 |
my $st = |
my $st = |
23 |
$dbh->prepare( |
$dbh->prepare( |
24 |
" SELECT shares.ID AS ID, shares.share AS name FROM shares;"); |
" SELECT shares.ID AS ID, shares.share AS name FROM shares;"); |
31 |
return @ret; |
return @ret; |
32 |
} |
} |
33 |
|
|
34 |
|
sub epoch_to_iso { |
35 |
|
my $t = shift || return; |
36 |
|
my $dt = DateTime->from_epoch( epoch => $t ) || return; |
37 |
|
print STDERR "$t == ",$dt->epoch,"\n"; |
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 |
my $backup_from = mk_iso_date('search_backup', 'from'); |
day => $dd |
57 |
push @conditions, qq{ date(backups.date, 'unixepoch','localtime') >= '$backup_from' } if ($backup_from); |
); |
58 |
my $backup_to = mk_iso_date('search_backup', 'to'); |
return $dt->epoch || 'NULL'; |
59 |
push @conditions, qq{ date(backups.date, 'unixepoch','localtime') <= '$backup_to' } if ($backup_to); |
} |
60 |
|
|
61 |
my $files_from = mk_iso_date('search', 'from'); |
my $backup_from = mk_epoch_date('search_backup', 'from'); |
62 |
push @conditions, qq{ date(files.date, 'unixepoch','localtime') >= '$files_from' } if ($files_from); |
push @conditions, qq{ backups.date >= $backup_from } if ($backup_from); |
63 |
my $files_to = mk_iso_date('search', 'to'); |
my $backup_to = mk_epoch_date('search_backup', 'to'); |
64 |
push @conditions, qq{ date(files.date, 'unixepoch','localtime') <= '$files_to' } if ($files_to); |
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 |
print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:",join(" | ",@conditions); |
print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:",join(" | ",@conditions); |
72 |
|
|
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, |
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, |
shares.share||files.fullpath AS networkPath, |
99 |
date(files.date, 'unixepoch', 'localtime') AS date, |
files.date AS date, |
100 |
files.type AS filetype, |
files.type AS filetype, |
101 |
files.size AS size, |
files.size AS size, |
102 |
|
}; |
103 |
|
|
104 |
|
my $sql_dvd_cols = qq{ |
105 |
dvds.name AS dvd |
dvds.name AS dvd |
106 |
}; |
}; |
107 |
|
|
110 |
INNER JOIN shares ON files.shareID=shares.ID |
INNER JOIN shares ON files.shareID=shares.ID |
111 |
INNER JOIN hosts ON hosts.ID = shares.hostID |
INNER JOIN hosts ON hosts.ID = shares.hostID |
112 |
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 |
113 |
|
}; |
114 |
|
|
115 |
|
my $sql_dvd_from = qq{ |
116 |
LEFT JOIN dvds ON dvds.ID = files.dvdid |
LEFT JOIN dvds ON dvds.ID = files.dvdid |
117 |
}; |
}; |
118 |
|
|
133 |
|
|
134 |
my ($results) = $sth->fetchrow_array(); |
my ($results) = $sth->fetchrow_array(); |
135 |
|
|
136 |
$sth = $dbh->prepare(qq{ select $sql_cols $sql_from $sql_where $sql_order }); |
$sth = $dbh->prepare(qq{ select $sql_cols $sql_dvd_cols $sql_from $sql_dvd_from $sql_where $sql_order }); |
137 |
$sth->execute( $offset ); |
$sth->execute( $offset ); |
138 |
|
|
139 |
my @ret; |
my @ret; |
143 |
'hname' => $row->{'hname'}, |
'hname' => $row->{'hname'}, |
144 |
'sname' => $row->{'sname'}, |
'sname' => $row->{'sname'}, |
145 |
'sharename' => $row->{'sharename'}, |
'sharename' => $row->{'sharename'}, |
146 |
'backupno' => $row->{'backupNum'}, |
'backupno' => $row->{'backupnum'}, |
147 |
'fname' => $row->{'filename'}, |
'fname' => $row->{'filename'}, |
148 |
'fpath' => $row->{'filepath'}, |
'fpath' => $row->{'filepath'}, |
149 |
'networkpath' => $row->{'networkPath'}, |
'networkpath' => $row->{'networkpath'}, |
150 |
'date' => $row->{'date'}, |
'date' => $row->{'date'}, |
151 |
'type' => $row->{'filetype'}, |
'type' => $row->{'filetype'}, |
152 |
'size' => $row->{'size'}, |
'size' => $row->{'size'}, |
160 |
return ($results, \@ret); |
return ($results, \@ret); |
161 |
} |
} |
162 |
|
|
163 |
sub getBackupsNotBurned() |
sub getBackupsNotBurned() { |
164 |
{ |
|
165 |
my $dbh = DBI->connect( "dbi:SQLite:dbname=${TopDir}/$Conf{SearchDB}", |
my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } ); |
166 |
"", "", { RaiseError => 1, AutoCommit => 1 } ); |
my $sql = q{ |
167 |
my $sql = q{ |
SELECT |
168 |
SELECT |
hosts.ID AS hostid, |
169 |
hosts.ID AS hostID, |
min(hosts.name) AS host, |
170 |
hosts.name AS host, |
backups.num AS backupno, |
171 |
backups.num AS backupno, |
min(backups.type) AS type, |
172 |
backups.type AS type, |
min(backups.date) AS date |
173 |
backups.date AS date |
FROM backups, shares, files, hosts |
174 |
FROM backups, shares, files, hosts |
WHERE |
175 |
WHERE |
backups.num = files.backupNum AND |
176 |
backups.num = files.backupNum AND |
shares.ID = files.shareID AND |
177 |
shares.ID = files.shareID AND |
backups.hostID = shares.hostID AND |
178 |
backups.hostID = shares.hostID AND |
hosts.ID = backups.hostID AND |
179 |
hosts.ID = backups.hostID AND |
files.dvdid IS NULL |
180 |
files.dvdid IS NULL |
GROUP BY |
181 |
GROUP BY |
backups.hostID, backups.num, hosts.id |
182 |
backups.hostID, backups.num |
ORDER BY min(backups.date) |
183 |
}; |
}; |
184 |
my $st = $dbh -> prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
185 |
my @ret = (); |
my @ret; |
186 |
$st -> execute(); |
$sth->execute(); |
187 |
|
|
188 |
while ( my $row = $st -> fetchrow_hashref() ) |
while ( my $row = $sth->fetchrow_hashref() ) { |
189 |
{ |
push(@ret, { |
190 |
push(@ret, { |
'host' => $row->{'host'}, |
191 |
'host' => $row->{'host'}, |
'hostid' => $row->{'hostid'}, |
192 |
'hostid' => $row->{'hostID'}, |
'backupno' => $row->{'backupno'}, |
193 |
'backupno' => $row->{'backupno'}, |
'type' => $row->{'type'}, |
194 |
'type' => $row->{'type'}, |
'date' => $row->{'date'}, |
195 |
'date' => $row->{'date'} |
'age' => sprintf("%0.1f", ( (time() - $row->{'date'}) / 86400 ) ), |
196 |
} |
} |
197 |
); |
); |
198 |
} |
} |
199 |
|
|
200 |
return @ret; |
return @ret; |
201 |
} |
} |
202 |
|
|
203 |
sub displayBackupsGrid() |
sub displayBackupsGrid() |
204 |
{ |
{ |
237 |
if ($addForm) { |
if ($addForm) { |
238 |
$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>"; |
239 |
} |
} |
240 |
$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{ |
241 |
|
<td class="tableheader">Host</td> |
242 |
|
<td class="tableheader">Backup no</td> |
243 |
|
<td class="tableheader">Type</td> |
244 |
|
<td class="tableheader">date</td> |
245 |
|
<td class="tableheader">age/days</td> |
246 |
|
</tr> |
247 |
|
}; |
248 |
|
|
249 |
my @backups = getBackupsNotBurned(); |
my @backups = getBackupsNotBurned(); |
250 |
my $backup; |
my $backup; |
251 |
|
|
252 |
if ($addForm) { |
if ($addForm) { |
253 |
$retHTML .= qq{<tr><td colspan=7 style="tableheader"> |
$retHTML .= qq{ |
254 |
|
<tr><td colspan=7 style="tableheader"> |
255 |
<input type="submit" value="Burn selected backups on medium" name="submitBurner"> |
<input type="submit" value="Burn selected backups on medium" name="submitBurner"> |
256 |
</td></tr>}; |
</td></tr> |
257 |
|
}; |
258 |
} |
} |
259 |
|
|
260 |
foreach $backup(@backups) { |
foreach $backup(@backups) { |
263 |
|
|
264 |
$retHTML .= "<tr>"; |
$retHTML .= "<tr>"; |
265 |
if ($addForm) { |
if ($addForm) { |
266 |
$retHTML .= qq{<td class="fview"><input type="checkbox" name="fcb} . |
$retHTML .= '<td class="fview"><input type="checkbox" name="fcb' . |
267 |
$backup->{'hostid'}."_".$backup->{'backupno'} . |
$backup->{'hostid'}.'_'.$backup->{'backupno'} . |
268 |
qq{" value="} . $backup->{'hostid'}."_".$backup->{'backupno'} . |
'" value="' . $backup->{'hostid'}.'_'.$backup->{'backupno'} . |
269 |
qq{"></td>}; |
'"></td>'; |
270 |
} |
} |
271 |
|
|
272 |
$retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' . |
$retHTML .= '<td class="fviewborder">' . $backup->{'host'} . '</td>' . |
273 |
'<td class="fviewborder">' . $backup->{'backupno'} . '</td>' . |
'<td class="fviewborder">' . $backup->{'backupno'} . '</td>' . |
274 |
'<td class="fviewborder">' . $backup->{'type'} . '</td>' . |
'<td class="fviewborder">' . $backup->{'type'} . '</td>' . |
275 |
'<td class="fviewborder">' . $backup->{'date'} . '<td>' . |
'<td class="fviewborder">' . epoch_to_iso( $backup->{'date'} ) . '</td>' . |
276 |
|
'<td class="fviewborder">' . $backup->{'age'} . '</td>' . |
277 |
'</tr>'; |
'</tr>'; |
278 |
} |
} |
279 |
|
|
296 |
$retHTML .= qq{<input type="hidden" value="results" name="search_results">}; |
$retHTML .= qq{<input type="hidden" value="results" name="search_results">}; |
297 |
} |
} |
298 |
|
|
299 |
|
my $start_t = time(); |
300 |
|
|
301 |
my ($results, $files) = getFiles($where, $offset); |
my ($results, $files) = getFiles($where, $offset); |
302 |
|
|
303 |
|
my $dur_t = time() - $start_t; |
304 |
|
my $dur = sprintf("%0.4fs", $dur_t); |
305 |
|
|
306 |
my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page); |
my ($from, $to) = (($offset * $on_page) + 1, ($offset * $on_page) + $on_page); |
307 |
|
|
308 |
$retHTML .= qq{ |
$retHTML .= qq{ |
309 |
<br/>Found $results files, showing $from - $to |
<br/>Found <b>$results files</b> showing <b>$from - $to</b> (took $dur) |
310 |
<table style="fview" width="100%"> |
<table style="fview" width="100%"> |
311 |
<tr> |
<tr> |
312 |
<td class="tableheader">Share</td> |
<td class="tableheader">Share</td> |
344 |
$typeStr, |
$typeStr, |
345 |
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'} ), |
346 |
$file->{'size'}, |
$file->{'size'}, |
347 |
$file->{'date'}, |
epoch_to_iso( $file->{'date'} ), |
348 |
$file->{'dvd'} |
$file->{'dvd'} |
349 |
)) { |
)) { |
350 |
$retHTML .= qq{<td class="fviewborder">$v</td>}; |
$retHTML .= qq{<td class="fviewborder">$v</td>}; |