/[BackupPC]/trunk/bin/BackupPC_updatedb
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/bin/BackupPC_updatedb

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 89 by dpavlin, Sun Aug 28 17:04:12 2005 UTC revision 116 by dpavlin, Sun Sep 11 12:39:24 2005 UTC
# Line 13  use File::Pid; Line 13  use File::Pid;
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;
# Line 40  my $beenThere = {}; Line 41  my $beenThere = {};
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;  my $use_hest = $Conf{HyperEstraierIndex};
46  $index_path =~ s#//#/#g;  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 });
# Line 84  sub curr_time { Line 95  sub curr_time {
95  }  }
96    
97  my $hest_db;  my $hest_db;
98    my $hest_node;
99    
100  sub hest_update {  sub signal {
101            my($sig) = @_;
102          my ($host_id, $share_id, $num) = @_;          if ($hest_db) {
103                    print "\nCaught a SIG$sig--syncing database and shutting down\n";
104          print curr_time," updating HyperEstraier: files";                  $hest_db->sync();
105                    $hest_db->close();
         my $t = time();  
   
         my $where = '';  
         if ($host_id && $share_id && $num) {  
                 $where = qq{  
                 WHERE  
                         hosts.id = ? AND  
                         shares.id = ? AND  
                         files.backupnum = ?  
                 };  
106          }          }
107            exit(0);
108    }
109    
110          my $sth = $dbh->prepare(qq{  $SIG{'INT'}  = \&signal;
111                  SELECT  $SIG{'QUIT'} = \&signal;
                         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 filetype,  
                         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  
         });  
112    
113          $sth->execute(@_);  sub hest_update {
         my $results = $sth->rows;  
114    
115          if ($results == 0) {          my ($host_id, $share_id, $num) = @_;
116                  print " no files\n";  
117            unless ($use_hest) {
118                    print STDERR "HyperEstraier support not enabled in configuration\n";
119                  return;                  return;
120          }          }
121    
122          my $dot = int($results / 15) || 1;          print curr_time," updating HyperEstraier:";
   
         print " $results ($dot/#)";  
123    
124          sub fmt_date {          my $t = time();
                 my $t = shift || return;  
                 my $iso = BackupPC::Lib::timeStamp($t);  
                 $iso =~ s/\s/T/;  
                 return $iso;  
         }  
125    
126          my $max = int($results / $dot);          my $offset = 0;
127            my $added = 0;
128    
129          print " index $index_path...";          print " opening index $use_hest";
130          use HyperEstraier;          if ($index_path) {
131          my $db = HyperEstraier::Database->new();                  $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  #       unless ($hest_db) {          my $results = 0;
 #               print " open reader";  
 #               $hest_db = HyperEstraier::Database->new();  
 #  
 #       }  
144    
145            do {
146    
147          $db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);                  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          my $added = 0;                  my $limit = sprintf('LIMIT '.EST_CHUNK.' OFFSET %d', $offset);
160    
161          while (my $row = $sth->fetchrow_hashref()) {                  my $sth = $dbh->prepare(qq{
162                            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                  my $fid = $row->{'fid'} || die "no fid?";                  sub fmt_date {
192                  my $uri = 'file:///' . $fid;                          my $t = shift || return;
193                            my $iso = BackupPC::Lib::timeStamp($t);
194                            $iso =~ s/\s/T/;
195                            return $iso;
196                    }
197    
198                  next if ($db->uri_to_id($uri));                  while (my $row = $sth->fetchrow_hashref()) {
199    
200                  # create a document object                          my $fid = $row->{'fid'} || die "no fid?";
201                  my $doc = HyperEstraier::Document->new;                          my $uri = 'file:///' . $fid;
202    
203                  # add attributes to the document object                          my $id = ($hest_db || $hest_node)->uri_to_id($uri);
204                  $doc->add_attr('@uri', $uri);                          next unless ($id == -1);
205    
206                  foreach my $c (@{ $sth->{NAME} }) {                          # create a document object
207                          $doc->add_attr($c, $row->{$c}) if ($row->{$c});                          my $doc = HyperEstraier::Document->new;
208    
209                            # add attributes to the document object
210                            $doc->add_attr('@uri', $uri);
211    
212                            foreach my $c (@{ $sth->{NAME} }) {
213                                    $doc->add_attr($c, $row->{$c}) if ($row->{$c});
214                            }
215    
216                            #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
217    
218                            # add the body text to the document object
219                            my $path = $row->{'filepath'};
220                            $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                  #$doc->add_attr('@cdate', fmt_date($row->{'date'}));                  print " $added";
238                    $hest_db->sync() if ($index_path);
239    
240                  # add the body text to the document object                  $offset += EST_CHUNK;
                 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--;  
                 }  
241    
242          }          } while ($results == EST_CHUNK);
243    
244          print "sync $added new files";          if ($index_path) {
245          $db->sync();                  print ", close";
246          print " close";                  $hest_db->close();
247          $db->close();          }
248    
249          my $dur = (time() - $t) || 1;          my $dur = (time() - $t) || 1;
250          printf(" [%.2f/s new %.2f/s dur: %s]\n",          printf(" [%.2f/s dur: %s]\n",
                 ( $results / $dur ),  
251                  ( $added / $dur ),                  ( $added / $dur ),
252                  fmt_time($dur)                  fmt_time($dur)
253          );          );
# Line 215  sub hest_update { Line 257  sub hest_update {
257    
258    
259  ## update index ##  ## update index ##
260  if ($opt{i} || ($index_path && ! -e $index_path)) {  if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) {
261          # update all          # update all
262          print "force update of HyperEstraier index ";          print "force update of HyperEstraier index ";
263          print "importing existing data" unless (-e $index_path);          print "importing existing data" unless (-e $index_path);
# Line 439  foreach my $host_key (keys %{$hosts}) { Line 481  foreach my $host_key (keys %{$hosts}) {
481                                  fmt_time($dur)                                  fmt_time($dur)
482                          );                          );
483    
484                          hest_update($hostID, $shareID, $backupNum);                          hest_update($hostID, $shareID, $backupNum) if ($nf + $nd > 0);
485                  }                  }
486    
487          }          }

Legend:
Removed from v.89  
changed lines
  Added in v.116

  ViewVC Help
Powered by ViewVC 1.1.26