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 |
|
$index_path = $TopDir . '/' . $index_path; |
46 |
|
$index_path =~ s#//#/#g; |
47 |
|
if ($index_path) { |
48 |
|
use HyperEstraier; |
49 |
|
} |
50 |
|
|
51 |
|
|
52 |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); |
53 |
|
|
54 |
my %opt; |
my %opt; |
55 |
|
|
56 |
if ( !getopts("cdm:v:", \%opt ) ) { |
if ( !getopts("cdm:v:i", \%opt ) ) { |
57 |
print STDERR <<EOF; |
print STDERR <<EOF; |
58 |
usage: $0 [-c|-d] [-m num] [-v|-v level] |
usage: $0 [-c|-d] [-m num] [-v|-v level] [-i] |
59 |
|
|
60 |
Options: |
Options: |
61 |
-c create database on first use |
-c create database on first use |
62 |
-d delete database before import |
-d delete database before import |
63 |
-m num import just num increments for one host |
-m num import just num increments for one host |
64 |
-v num set verbosity (debug) level (default $debug) |
-v num set verbosity (debug) level (default $debug) |
65 |
|
-i update HyperEstraier full text index |
66 |
EOF |
EOF |
67 |
exit 1; |
exit 1; |
68 |
} |
} |
69 |
|
|
70 |
###################################create tables############################3 |
if ($opt{v}) { |
71 |
|
print "Debug level at $opt{v}\n"; |
72 |
|
$debug = $opt{v}; |
73 |
|
} |
74 |
|
|
75 |
|
#---- subs ---- |
76 |
|
|
77 |
|
sub fmt_time { |
78 |
|
my $t = shift || return; |
79 |
|
my $out = ""; |
80 |
|
my ($ss,$mm,$hh) = gmtime($t); |
81 |
|
$out .= "${hh}h" if ($hh); |
82 |
|
$out .= sprintf("%02d:%02d", $mm,$ss); |
83 |
|
return $out; |
84 |
|
} |
85 |
|
|
86 |
|
sub curr_time { |
87 |
|
return strftime($t_fmt,localtime()); |
88 |
|
} |
89 |
|
|
90 |
|
my $hest_db; |
91 |
|
|
92 |
|
sub signal { |
93 |
|
my($sig) = @_; |
94 |
|
if ($hest_db) { |
95 |
|
print "\nCaught a SIG$sig--syncing database and shutting down\n"; |
96 |
|
$hest_db->sync(); |
97 |
|
$hest_db->close(); |
98 |
|
} |
99 |
|
exit(0); |
100 |
|
} |
101 |
|
|
102 |
|
$SIG{'INT'} = \&signal; |
103 |
|
$SIG{'QUIT'} = \&signal; |
104 |
|
|
105 |
|
sub hest_update { |
106 |
|
|
107 |
|
my ($host_id, $share_id, $num) = @_; |
108 |
|
|
109 |
|
print curr_time," updating HyperEstraier:"; |
110 |
|
|
111 |
|
my $t = time(); |
112 |
|
|
113 |
|
my $offset = 0; |
114 |
|
my $added = 0; |
115 |
|
|
116 |
|
print " opening index $index_path"; |
117 |
|
$hest_db = HyperEstraier::Database->new(); |
118 |
|
$hest_db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); |
119 |
|
|
120 |
|
print " increment is " . EST_CHUNK . " files"; |
121 |
|
|
122 |
|
my $results = 0; |
123 |
|
|
124 |
|
do { |
125 |
|
|
126 |
|
my $where = ''; |
127 |
|
if ($host_id && $share_id && $num) { |
128 |
|
$where = qq{ |
129 |
|
WHERE |
130 |
|
hosts.id = ? AND |
131 |
|
shares.id = ? AND |
132 |
|
files.backupnum = ? |
133 |
|
}; |
134 |
|
} |
135 |
|
|
136 |
|
my $limit = sprintf('LIMIT '.EST_CHUNK.' OFFSET %d', $offset); |
137 |
|
|
138 |
|
my $sth = $dbh->prepare(qq{ |
139 |
|
SELECT |
140 |
|
files.id AS fid, |
141 |
|
hosts.name AS hname, |
142 |
|
shares.name AS sname, |
143 |
|
-- shares.share AS sharename, |
144 |
|
files.backupnum AS backupnum, |
145 |
|
-- files.name AS filename, |
146 |
|
files.path AS filepath, |
147 |
|
files.date AS date, |
148 |
|
files.type AS type, |
149 |
|
files.size AS size, |
150 |
|
files.shareid AS shareid, |
151 |
|
backups.date AS backup_date |
152 |
|
FROM files |
153 |
|
INNER JOIN shares ON files.shareID=shares.ID |
154 |
|
INNER JOIN hosts ON hosts.ID = shares.hostID |
155 |
|
INNER JOIN backups ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID |
156 |
|
$where |
157 |
|
$limit |
158 |
|
}); |
159 |
|
|
160 |
|
$sth->execute(@_); |
161 |
|
$results = $sth->rows; |
162 |
|
|
163 |
|
if ($results == 0) { |
164 |
|
print " - no more files\n"; |
165 |
|
last; |
166 |
|
} |
167 |
|
|
168 |
|
sub fmt_date { |
169 |
|
my $t = shift || return; |
170 |
|
my $iso = BackupPC::Lib::timeStamp($t); |
171 |
|
$iso =~ s/\s/T/; |
172 |
|
return $iso; |
173 |
|
} |
174 |
|
|
175 |
|
while (my $row = $sth->fetchrow_hashref()) { |
176 |
|
|
177 |
|
my $fid = $row->{'fid'} || die "no fid?"; |
178 |
|
my $uri = 'file:///' . $fid; |
179 |
|
|
180 |
|
my $id = $hest_db->uri_to_id($uri); |
181 |
|
next unless ($id == -1); |
182 |
|
|
183 |
|
# create a document object |
184 |
|
my $doc = HyperEstraier::Document->new; |
185 |
|
|
186 |
|
# add attributes to the document object |
187 |
|
$doc->add_attr('@uri', $uri); |
188 |
|
|
189 |
|
foreach my $c (@{ $sth->{NAME} }) { |
190 |
|
$doc->add_attr($c, $row->{$c}) if ($row->{$c}); |
191 |
|
} |
192 |
|
|
193 |
|
#$doc->add_attr('@cdate', fmt_date($row->{'date'})); |
194 |
|
|
195 |
|
# add the body text to the document object |
196 |
|
my $path = $row->{'filepath'}; |
197 |
|
$doc->add_text($path); |
198 |
|
$path =~ s/(.)/$1 /g; |
199 |
|
$doc->add_hidden_text($path); |
200 |
|
|
201 |
|
print STDERR $doc->dump_draft,"\n" if ($debug > 1); |
202 |
|
|
203 |
|
# register the document object to the database |
204 |
|
$hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); |
205 |
|
$added++; |
206 |
|
} |
207 |
|
|
208 |
|
print " $added"; |
209 |
|
$hest_db->sync(); |
210 |
|
|
211 |
|
$offset += EST_CHUNK; |
212 |
|
|
213 |
|
} while ($results == EST_CHUNK); |
214 |
|
|
215 |
|
print ", close"; |
216 |
|
$hest_db->close(); |
217 |
|
|
218 |
|
my $dur = (time() - $t) || 1; |
219 |
|
printf(" [%.2f/s dur: %s]\n", |
220 |
|
( $added / $dur ), |
221 |
|
fmt_time($dur) |
222 |
|
); |
223 |
|
} |
224 |
|
|
225 |
|
#---- /subs ---- |
226 |
|
|
227 |
|
|
228 |
|
## update index ## |
229 |
|
if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) { |
230 |
|
# update all |
231 |
|
print "force update of HyperEstraier index "; |
232 |
|
print "importing existing data" unless (-e $index_path); |
233 |
|
print "by -i flag" if ($opt{i}); |
234 |
|
print "\n"; |
235 |
|
hest_update(); |
236 |
|
} |
237 |
|
|
238 |
|
## create tables ## |
239 |
if ($opt{c}) { |
if ($opt{c}) { |
240 |
sub do_index { |
sub do_index { |
241 |
my $index = shift || return; |
my $index = shift || return; |
325 |
|
|
326 |
} |
} |
327 |
|
|
328 |
|
## delete data before inseting ## |
329 |
if ($opt{d}) { |
if ($opt{d}) { |
330 |
print "deleting "; |
print "deleting "; |
331 |
foreach my $table (qw(files dvds backups shares hosts)) { |
foreach my $table (qw(files dvds backups shares hosts)) { |
337 |
$dbh->commit; |
$dbh->commit; |
338 |
} |
} |
339 |
|
|
340 |
if ($opt{v}) { |
## insert new values ## |
|
print "Debug level at $opt{v}\n"; |
|
|
$debug = $opt{v}; |
|
|
} |
|
|
|
|
|
#################################INSERT VALUES############################# |
|
341 |
|
|
342 |
# get hosts |
# get hosts |
343 |
$hosts = $bpc->HostInfoRead(); |
$hosts = $bpc->HostInfoRead(); |
371 |
VALUES (?,?,?,?,?,?,?) |
VALUES (?,?,?,?,?,?,?) |
372 |
}); |
}); |
373 |
|
|
|
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; |
|
|
} |
|
|
|
|
374 |
foreach my $host_key (keys %{$hosts}) { |
foreach my $host_key (keys %{$hosts}) { |
375 |
|
|
376 |
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"; |
426 |
next if ($count > 0); |
next if ($count > 0); |
427 |
|
|
428 |
# dump some log |
# dump some log |
429 |
print strftime($t_fmt,localtime())," ", $share; |
print curr_time," ", $share; |
430 |
|
|
431 |
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); |
432 |
|
|
449 |
( ($f+$d) / $dur ), |
( ($f+$d) / $dur ), |
450 |
fmt_time($dur) |
fmt_time($dur) |
451 |
); |
); |
452 |
|
|
453 |
|
hest_update($hostID, $shareID, $backupNum); |
454 |
} |
} |
455 |
|
|
456 |
} |
} |
495 |
my @data = @_; |
my @data = @_; |
496 |
shift @data; |
shift @data; |
497 |
|
|
498 |
my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_; |
my ($key, $shareID,undef,$name,$path,$date,undef,$size) = @_; |
499 |
|
|
500 |
return $beenThere->{$key} if (defined($beenThere->{$key})); |
return $beenThere->{$key} if (defined($beenThere->{$key})); |
501 |
|
|
505 |
path = ? and |
path = ? and |
506 |
date = ? and |
date = ? and |
507 |
size = ? |
size = ? |
508 |
|
LIMIT 1 |
509 |
}); |
}); |
510 |
|
|
511 |
my @param = ($shareID,$path,$date,$size); |
my @param = ($shareID,$path,$date,$size); |
512 |
$sth->{file_in_db}->execute(@param); |
$sth->{file_in_db}->execute(@param); |
513 |
my $rows = $sth->{file_in_db}->rows; |
my $rows = $sth->{file_in_db}->rows; |
514 |
print STDERR "## found_in_db ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3); |
print STDERR "## found_in_db($shareID,$path,$date,$size) ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3); |
515 |
|
|
516 |
$beenThere->{$key}++; |
$beenThere->{$key}++; |
517 |
|
|