--- trunk/bin/BackupPC_updatedb 2005/08/28 08:40:06 81 +++ trunk/bin/BackupPC_updatedb 2005/08/30 09:55:34 95 @@ -40,6 +40,10 @@ 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 }); @@ -64,41 +68,73 @@ $debug = $opt{v}; } -## update index ## -if ($opt{i}) { +#---- subs ---- + +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; +} - my $index_dir = '/var/tmp/casket'; +sub curr_time { + return strftime($t_fmt,localtime()); +} - print "updating HyperEstraier index $index_dir..."; +my $hest_db; - use HyperEstraier; - my $db = HyperEstraier::Database->new(); - $db->open($index_dir, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT); +sub hest_update { + + my ($host_id, $share_id, $num) = @_; + + print curr_time," updating HyperEstraier: select files"; + + my $t = time(); + + my $where = ''; + if ($host_id && $share_id && $num) { + $where = qq{ + WHERE + hosts.id = ? AND + shares.id = ? AND + files.backupnum = ? + }; + } my $sth = $dbh->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, + -- shares.share AS sharename, + files.backupnum AS backupnum, + -- files.name AS filename, files.path AS filepath, files.date AS date, - files.type AS filetype, + files.type AS type, files.size AS size, - files.shareid AS shareid + 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(); + $sth->execute(@_); + my $results = $sth->rows; - my $dot = int($sth->rows / 15); + if ($results == 0) { + print " - no files, skipping\n"; + return; + } - print $sth->rows, " files ($dot/#) "; + my $dot = int($results / 15) || 1; + + print " $results ($dot/#)"; sub fmt_date { my $t = shift || return; @@ -107,22 +143,42 @@ return $iso; } - my $i = 0; - my $max = int($sth->rows / $dot); + 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', 'file:///' . $row->{'fid'}); + $doc->add_attr('@uri', $uri); - foreach my $c (qw/fid hname sname sharename backupNum filename filepath shareid/) { + foreach my $c (@{ $sth->{NAME} }) { $doc->add_attr($c, $row->{$c}) if ($row->{$c}); } - $doc->add_attr('date', fmt_date($row->{'date'})); + #$doc->add_attr('@cdate', fmt_date($row->{'date'})); # add the body text to the document object my $path = $row->{'filepath'}; @@ -135,24 +191,41 @@ # register the document object to the database $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN); - $i++; - if ($i % $dot == 0) { + $added++; + if ($added % $dot == 0) { print "$max "; $max--; } } - print "sync"; + print "sync $added new files"; $db->sync(); - print " close\n"; + print ", close"; $db->close(); - exit; + my $dur = (time() - $t) || 1; + printf(" [%.2f/s new %.2f/s dur: %s]\n", + ( $results / $dur ), + ( $added / $dur ), + fmt_time($dur) + ); } -###################################create tables############################3 +#---- /subs ---- + + +## update index ## +if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) { + # 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; @@ -242,6 +315,7 @@ } +## delete data before inseting ## if ($opt{d}) { print "deleting "; foreach my $table (qw(files dvds backups shares hosts)) { @@ -253,7 +327,7 @@ $dbh->commit; } -#################################INSERT VALUES############################# +## insert new values ## # get hosts $hosts = $bpc->HostInfoRead(); @@ -287,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"; @@ -351,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); @@ -374,6 +439,8 @@ ( ($f+$d) / $dur ), fmt_time($dur) ); + + hest_update($hostID, $shareID, $backupNum); } }