13 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
14 |
|
|
15 |
use constant BPC_FTYPE_DIR => 5; |
use constant BPC_FTYPE_DIR => 5; |
16 |
|
use constant EST_CHUNK => 100000; |
17 |
|
|
18 |
my $debug = 0; |
my $debug = 0; |
19 |
$|=1; |
$|=1; |
41 |
|
|
42 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
43 |
my $user = $Conf{SearchUser} || ''; |
my $user = $Conf{SearchUser} || ''; |
44 |
my $index_path = $Conf{HyperEstraierIndex}; |
|
45 |
|
my $use_hest = $Conf{HyperEstraierIndex}; |
46 |
|
my ($index_path, $index_node_url); |
47 |
|
if ($use_hest) { |
48 |
|
use HyperEstraier; |
49 |
|
if ($use_hest =~ m#^http://#) { |
50 |
|
$index_node_url = $use_hest; |
51 |
|
} else { |
52 |
|
$index_path = $TopDir . '/' . $index_path; |
53 |
|
$index_path =~ s#//#/#g; |
54 |
|
} |
55 |
|
} |
56 |
|
print "-- $use_hest : $index_path OR $index_node_url --\n"; |
57 |
|
|
58 |
|
|
59 |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
60 |
|
|
79 |
$debug = $opt{v}; |
$debug = $opt{v}; |
80 |
} |
} |
81 |
|
|
82 |
## update index ## |
#---- subs ---- |
|
if ($opt{i}) { |
|
83 |
|
|
84 |
print "updating HyperEstraier files "; |
sub fmt_time { |
85 |
|
my $t = shift || return; |
86 |
my $sth = $dbh->prepare(qq{ |
my $out = ""; |
87 |
SELECT |
my ($ss,$mm,$hh) = gmtime($t); |
88 |
files.id AS fid, |
$out .= "${hh}h" if ($hh); |
89 |
hosts.name AS hname, |
$out .= sprintf("%02d:%02d", $mm,$ss); |
90 |
shares.name AS sname, |
return $out; |
91 |
shares.share AS sharename, |
} |
92 |
files.backupNum AS backupNum, |
|
93 |
files.name AS filename, |
sub curr_time { |
94 |
files.path AS filepath, |
return strftime($t_fmt,localtime()); |
95 |
files.date AS date, |
} |
|
files.type AS filetype, |
|
|
files.size AS size, |
|
|
files.shareid AS shareid |
|
|
FROM files |
|
|
INNER JOIN shares ON files.shareID=shares.ID |
|
|
INNER JOIN hosts ON hosts.ID = shares.hostID |
|
|
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID |
|
|
}); |
|
96 |
|
|
97 |
$sth->execute(); |
my $hest_db; |
98 |
|
my $hest_node; |
99 |
|
|
100 |
my $dot = int($sth->rows / 15); |
sub signal { |
101 |
|
my($sig) = @_; |
102 |
|
if ($hest_db) { |
103 |
|
print "\nCaught a SIG$sig--syncing database and shutting down\n"; |
104 |
|
$hest_db->sync(); |
105 |
|
$hest_db->close(); |
106 |
|
} |
107 |
|
exit(0); |
108 |
|
} |
109 |
|
|
110 |
|
$SIG{'INT'} = \&signal; |
111 |
|
$SIG{'QUIT'} = \&signal; |
112 |
|
|
113 |
print $sth->rows, " files ($dot/#) "; |
sub hest_update { |
114 |
|
|
115 |
sub fmt_date { |
my ($host_id, $share_id, $num) = @_; |
116 |
my $t = shift || return; |
|
117 |
my $iso = BackupPC::Lib::timeStamp($t); |
unless ($use_hest) { |
118 |
$iso =~ s/\s/T/; |
print STDERR "HyperEstraier support not enabled in configuration\n"; |
119 |
return $iso; |
return; |
120 |
} |
} |
121 |
|
|
122 |
my $i = 0; |
print curr_time," updating HyperEstraier:"; |
|
my $max = int($sth->rows / $dot); |
|
123 |
|
|
124 |
$index_path = $TopDir . '/' . $index_path; |
my $t = time(); |
|
$index_path =~ s#//#/#g; |
|
125 |
|
|
126 |
print "index $index_path..."; |
my $offset = 0; |
127 |
use HyperEstraier; |
my $added = 0; |
128 |
my $db = HyperEstraier::Database->new(); |
|
129 |
$db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
print " opening index $use_hest"; |
130 |
|
if ($index_path) { |
131 |
|
$hest_db = HyperEstraier::Database->new(); |
132 |
|
$hest_db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
133 |
|
print " directly"; |
134 |
|
} elsif ($index_node_url) { |
135 |
|
$hest_node ||= HyperEstraier::Node->new($index_node_url); |
136 |
|
$hest_node->set_auth('admin', 'admin'); |
137 |
|
print " via node URL"; |
138 |
|
} else { |
139 |
|
die "don't know how to use HyperEstraier Index $use_hest"; |
140 |
|
} |
141 |
|
print " increment is " . EST_CHUNK . " files:"; |
142 |
|
|
143 |
|
my $results = 0; |
144 |
|
|
145 |
|
do { |
146 |
|
|
147 |
while (my $row = $sth->fetchrow_hashref()) { |
my $where = ''; |
148 |
|
my @data; |
149 |
|
if ($host_id && $share_id && $num) { |
150 |
|
$where = qq{ |
151 |
|
WHERE |
152 |
|
hosts.id = ? AND |
153 |
|
shares.id = ? AND |
154 |
|
files.backupnum = ? |
155 |
|
}; |
156 |
|
@data = ( $host_id, $share_id, $num ); |
157 |
|
} |
158 |
|
|
159 |
# create a document object |
my $limit = sprintf('LIMIT '.EST_CHUNK.' OFFSET %d', $offset); |
|
my $doc = HyperEstraier::Document->new; |
|
160 |
|
|
161 |
# add attributes to the document object |
my $sth = $dbh->prepare(qq{ |
162 |
$doc->add_attr('@uri', 'file:///' . $row->{'fid'}); |
SELECT |
163 |
|
files.id AS fid, |
164 |
|
hosts.name AS hname, |
165 |
|
shares.name AS sname, |
166 |
|
-- shares.share AS sharename, |
167 |
|
files.backupnum AS backupnum, |
168 |
|
-- files.name AS filename, |
169 |
|
files.path AS filepath, |
170 |
|
files.date AS date, |
171 |
|
files.type AS type, |
172 |
|
files.size AS size, |
173 |
|
files.shareid AS shareid, |
174 |
|
backups.date AS backup_date |
175 |
|
FROM files |
176 |
|
INNER JOIN shares ON files.shareID=shares.ID |
177 |
|
INNER JOIN hosts ON hosts.ID = shares.hostID |
178 |
|
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID |
179 |
|
$where |
180 |
|
$limit |
181 |
|
}); |
182 |
|
|
183 |
|
$sth->execute(@data); |
184 |
|
$results = $sth->rows; |
185 |
|
|
186 |
|
if ($results == 0) { |
187 |
|
print " - no new files\n"; |
188 |
|
last; |
189 |
|
} |
190 |
|
|
191 |
foreach my $c (qw/fid hname sname sharename backupNum filename filepath shareid/) { |
sub fmt_date { |
192 |
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
my $t = shift || return; |
193 |
|
my $iso = BackupPC::Lib::timeStamp($t); |
194 |
|
$iso =~ s/\s/T/; |
195 |
|
return $iso; |
196 |
} |
} |
197 |
|
|
198 |
$doc->add_attr('date', fmt_date($row->{'date'})); |
while (my $row = $sth->fetchrow_hashref()) { |
199 |
|
|
200 |
|
my $fid = $row->{'fid'} || die "no fid?"; |
201 |
|
my $uri = 'file:///' . $fid; |
202 |
|
|
203 |
|
my $id = ($hest_db || $hest_node)->uri_to_id($uri); |
204 |
|
next unless ($id == -1); |
205 |
|
|
206 |
# add the body text to the document object |
# create a document object |
207 |
my $path = $row->{'filepath'}; |
my $doc = HyperEstraier::Document->new; |
208 |
$doc->add_text($path); |
|
209 |
$path =~ s/(.)/$1 /g; |
# add attributes to the document object |
210 |
$doc->add_hidden_text($path); |
$doc->add_attr('@uri', $uri); |
211 |
|
|
212 |
print STDERR $doc->dump_draft,"\n" if ($debug > 1); |
foreach my $c (@{ $sth->{NAME} }) { |
213 |
|
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
214 |
# register the document object to the database |
} |
215 |
$db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
|
216 |
|
#$doc->add_attr('@cdate', fmt_date($row->{'date'})); |
217 |
$i++; |
|
218 |
if ($i % $dot == 0) { |
# add the body text to the document object |
219 |
print "$max "; |
my $path = $row->{'filepath'}; |
220 |
$max--; |
$doc->add_text($path); |
221 |
|
$path =~ s/(.)/$1 /g; |
222 |
|
$doc->add_hidden_text($path); |
223 |
|
|
224 |
|
print STDERR $doc->dump_draft,"\n" if ($debug > 1); |
225 |
|
|
226 |
|
# register the document object to the database |
227 |
|
if ($hest_db) { |
228 |
|
$hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
229 |
|
} elsif ($hest_node) { |
230 |
|
$hest_node->put_doc($doc); |
231 |
|
} else { |
232 |
|
die "not supported"; |
233 |
|
} |
234 |
|
$added++; |
235 |
} |
} |
236 |
|
|
237 |
} |
print " $added"; |
238 |
|
$hest_db->sync() if ($index_path); |
239 |
|
|
240 |
|
$offset += EST_CHUNK; |
241 |
|
|
242 |
|
} while ($results == EST_CHUNK); |
243 |
|
|
244 |
print "sync"; |
if ($index_path) { |
245 |
$db->sync(); |
print ", close"; |
246 |
print " close\n"; |
$hest_db->close(); |
247 |
$db->close(); |
} |
248 |
|
|
249 |
exit; |
my $dur = (time() - $t) || 1; |
250 |
|
printf(" [%.2f/s dur: %s]\n", |
251 |
|
( $added / $dur ), |
252 |
|
fmt_time($dur) |
253 |
|
); |
254 |
} |
} |
255 |
|
|
256 |
###################################create tables############################3 |
#---- /subs ---- |
257 |
|
|
258 |
|
|
259 |
|
## update index ## |
260 |
|
if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) { |
261 |
|
# update all |
262 |
|
print "force update of HyperEstraier index "; |
263 |
|
print "importing existing data" unless (-e $index_path); |
264 |
|
print "by -i flag" if ($opt{i}); |
265 |
|
print "\n"; |
266 |
|
hest_update(); |
267 |
|
} |
268 |
|
|
269 |
|
## create tables ## |
270 |
if ($opt{c}) { |
if ($opt{c}) { |
271 |
sub do_index { |
sub do_index { |
272 |
my $index = shift || return; |
my $index = shift || return; |
356 |
|
|
357 |
} |
} |
358 |
|
|
359 |
|
## delete data before inseting ## |
360 |
if ($opt{d}) { |
if ($opt{d}) { |
361 |
print "deleting "; |
print "deleting "; |
362 |
foreach my $table (qw(files dvds backups shares hosts)) { |
foreach my $table (qw(files dvds backups shares hosts)) { |
368 |
$dbh->commit; |
$dbh->commit; |
369 |
} |
} |
370 |
|
|
371 |
#################################INSERT VALUES############################# |
## insert new values ## |
372 |
|
|
373 |
# get hosts |
# get hosts |
374 |
$hosts = $bpc->HostInfoRead(); |
$hosts = $bpc->HostInfoRead(); |
402 |
VALUES (?,?,?,?,?,?,?) |
VALUES (?,?,?,?,?,?,?) |
403 |
}); |
}); |
404 |
|
|
|
sub fmt_time { |
|
|
my $t = shift || return; |
|
|
my $out = ""; |
|
|
my ($ss,$mm,$hh) = gmtime($t); |
|
|
$out .= "${hh}h" if ($hh); |
|
|
$out .= sprintf("%02d:%02d", $mm,$ss); |
|
|
return $out; |
|
|
} |
|
|
|
|
405 |
foreach my $host_key (keys %{$hosts}) { |
foreach my $host_key (keys %{$hosts}) { |
406 |
|
|
407 |
my $hostname = $hosts->{$host_key}->{'host'} || die "can't find host for $host_key"; |
my $hostname = $hosts->{$host_key}->{'host'} || die "can't find host for $host_key"; |
457 |
next if ($count > 0); |
next if ($count > 0); |
458 |
|
|
459 |
# dump some log |
# dump some log |
460 |
print strftime($t_fmt,localtime())," ", $share; |
print curr_time," ", $share; |
461 |
|
|
462 |
my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); |
463 |
|
|
480 |
( ($f+$d) / $dur ), |
( ($f+$d) / $dur ), |
481 |
fmt_time($dur) |
fmt_time($dur) |
482 |
); |
); |
483 |
|
|
484 |
|
hest_update($hostID, $shareID, $backupNum) if ($nf + $nd > 0); |
485 |
} |
} |
486 |
|
|
487 |
} |
} |