--- trunk/bin/BackupPC_updatedb 2005/08/26 17:18:27 74 +++ trunk/bin/BackupPC_updatedb 2005/08/28 18:02:58 92 @@ -40,26 +40,192 @@ my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; my $user = $Conf{SearchUser} || ''; +my $index_path = $Conf{HyperEstraierIndex}; +$index_path = $TopDir . '/' . $index_path; +$index_path =~ s#//#/#g; + my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 }); my %opt; -if ( !getopts("cdm:v:", \%opt ) ) { +if ( !getopts("cdm:v:i", \%opt ) ) { print STDERR <prepare(qq{ + SELECT + files.id AS fid, + hosts.name AS hname, + shares.name AS sname, + -- shares.share AS sharename, + files.backupnum AS backupnum, + -- files.name AS filename, + files.path AS filepath, + files.date AS date, + files.type AS type, + files.size AS size, + files.shareid AS shareid, + backups.date AS backup_date + 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 + $where + }); + + $sth->execute(@_); + my $results = $sth->rows; + + if ($results == 0) { + print " - no files, skipping\n"; + return; + } + + my $dot = int($results / 15) || 1; + + print " $results ($dot/#)"; + + sub fmt_date { + my $t = shift || return; + my $iso = BackupPC::Lib::timeStamp($t); + $iso =~ s/\s/T/; + return $iso; + } + + my $max = int($results / $dot); + + print ", opening index $index_path..."; + use HyperEstraier; + my $db = HyperEstraier::Database->new(); + +# unless ($hest_db) { +# print " open reader"; +# $hest_db = HyperEstraier::Database->new(); +# +# } + + + $db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); + + my $added = 0; + + while (my $row = $sth->fetchrow_hashref()) { + + my $fid = $row->{'fid'} || die "no fid?"; + my $uri = 'file:///' . $fid; + + my $id = $db->uri_to_id($uri); + next unless ($id == -1); + + # create a document object + my $doc = HyperEstraier::Document->new; + + # add attributes to the document object + $doc->add_attr('@uri', $uri); + + foreach my $c (@{ $sth->{NAME} }) { + $doc->add_attr($c, $row->{$c}) if ($row->{$c}); + } + + #$doc->add_attr('@cdate', fmt_date($row->{'date'})); + + # add the body text to the document object + my $path = $row->{'filepath'}; + $doc->add_text($path); + $path =~ s/(.)/$1 /g; + $doc->add_hidden_text($path); + + print STDERR $doc->dump_draft,"\n" if ($debug > 1); + + # register the document object to the database + $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); + + $added++; + if ($added % $dot == 0) { + print "$max "; + $max--; + } + + } + + print "sync $added new files"; + $db->sync(); + print ", close"; + $db->close(); + + my $dur = (time() - $t) || 1; + printf(" [%.2f/s new %.2f/s dur: %s]\n", + ( $results / $dur ), + ( $added / $dur ), + fmt_time($dur) + ); +} + +#---- /subs ---- + +## update index ## +if ($opt{i} || ($index_path && ! -e $index_path)) { + # update all + print "force update of HyperEstraier index "; + print "importing existing data" unless (-e $index_path); + print "by -i flag" if ($opt{i}); + print "\n"; + hest_update(); +} + +## create tables ## if ($opt{c}) { sub do_index { my $index = shift || return; @@ -149,6 +315,7 @@ } +## delete data before inseting ## if ($opt{d}) { print "deleting "; foreach my $table (qw(files dvds backups shares hosts)) { @@ -160,12 +327,7 @@ $dbh->commit; } -if ($opt{v}) { - print "Debug level at $opt{v}\n"; - $debug = $opt{v}; -} - -#################################INSERT VALUES############################# +## insert new values ## # get hosts $hosts = $bpc->HostInfoRead(); @@ -199,15 +361,6 @@ VALUES (?,?,?,?,?,?,?) }); -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; -} - foreach my $host_key (keys %{$hosts}) { my $hostname = $hosts->{$host_key}->{'host'} || die "can't find host for $host_key"; @@ -263,7 +416,7 @@ next if ($count > 0); # dump some log - print strftime($t_fmt,localtime())," ", $share; + print curr_time," ", $share; my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID); @@ -286,6 +439,8 @@ ( ($f+$d) / $dur ), fmt_time($dur) ); + + hest_update($hostID, $shareID, $backupNum); } }