/[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 90 by dpavlin, Sun Aug 28 17:42:25 2005 UTC revision 117 by dpavlin, Sun Sep 11 13:05:06 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} || '';
 my $index_path = $Conf{HyperEstraierIndex};  
 $index_path = $TopDir . '/' . $index_path;  
 $index_path =~ s#//#/#g;  
44    
45    my $use_hest = $Conf{HyperEstraierIndex};
46    my ($index_path, $index_node_url) = getHyperEstraier_url($use_hest);
47    
48  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
49    
# Line 84  sub curr_time { Line 84  sub curr_time {
84  }  }
85    
86  my $hest_db;  my $hest_db;
87    my $hest_node;
88    
89  sub hest_update {  sub signal {
90            my($sig) = @_;
91          my ($host_id, $share_id, $num) = @_;          if ($hest_db) {
92                    print "\nCaught a SIG$sig--syncing database and shutting down\n";
93          print curr_time," updating HyperEstraier: files";                  $hest_db->sync();
94                    $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 = ?  
                 };  
95          }          }
96            exit(0);
97    }
98    
99          my $sth = $dbh->prepare(qq{  $SIG{'INT'}  = \&signal;
100                  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 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  
         });  
101    
102          $sth->execute(@_);  sub hest_update {
103          my $results = $sth->rows;  
104            my ($host_id, $share_id, $num) = @_;
105    
106          if ($results == 0) {          unless ($use_hest) {
107                  print " no files\n";                  print STDERR "HyperEstraier support not enabled in configuration\n";
108                  return;                  return;
109          }          }
110    
111          my $dot = int($results / 15) || 1;          print curr_time," updating HyperEstraier:";
112    
113          print " $results ($dot/#)";          my $t = time();
   
         sub fmt_date {  
                 my $t = shift || return;  
                 my $iso = BackupPC::Lib::timeStamp($t);  
                 $iso =~ s/\s/T/;  
                 return $iso;  
         }  
114    
115          my $max = int($results / $dot);          my $offset = 0;
116            my $added = 0;
117    
118          print " index $index_path...";          print " opening index $use_hest";
119          use HyperEstraier;          if ($index_path) {
120          my $db = HyperEstraier::Database->new();                  $hest_db = HyperEstraier::Database->new();
121                    $hest_db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
122                    print " directly";
123            } elsif ($index_node_url) {
124                    $hest_node ||= HyperEstraier::Node->new($index_node_url);
125                    $hest_node->set_auth('admin', 'admin');
126                    print " via node URL";
127            } else {
128                    die "don't know how to use HyperEstraier Index $use_hest";
129            }
130            print " increment is " . EST_CHUNK . " files:";
131    
132  #       unless ($hest_db) {          my $results = 0;
 #               print " open reader";  
 #               $hest_db = HyperEstraier::Database->new();  
 #  
 #       }  
133    
134            do {
135    
136          $db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);                  my $where = '';
137                    my @data;
138                    if ($host_id && $share_id && $num) {
139                            $where = qq{
140                            WHERE
141                                    hosts.id = ? AND
142                                    shares.id = ? AND
143                                    files.backupnum = ?
144                            };
145                            @data = ( $host_id, $share_id, $num );
146                    }
147    
148          my $added = 0;                  my $limit = sprintf('LIMIT '.EST_CHUNK.' OFFSET %d', $offset);
149    
150          while (my $row = $sth->fetchrow_hashref()) {                  my $sth = $dbh->prepare(qq{
151                            SELECT
152                                    files.id                        AS fid,
153                                    hosts.name                      AS hname,
154                                    shares.name                     AS sname,
155                                    -- shares.share                 AS sharename,
156                                    files.backupnum                 AS backupnum,
157                                    -- files.name                   AS filename,
158                                    files.path                      AS filepath,
159                                    files.date                      AS date,
160                                    files.type                      AS type,
161                                    files.size                      AS size,
162                                    files.shareid                   AS shareid,
163                                    backups.date                    AS backup_date
164                            FROM files
165                                    INNER JOIN shares       ON files.shareID=shares.ID
166                                    INNER JOIN hosts        ON hosts.ID = shares.hostID
167                                    INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID
168                            $where
169                            $limit
170                    });
171    
172                    $sth->execute(@data);
173                    $results = $sth->rows;
174    
175                    if ($results == 0) {
176                            print " - no new files\n";
177                            last;
178                    }
179    
180                  my $fid = $row->{'fid'} || die "no fid?";                  sub fmt_date {
181                  my $uri = 'file:///' . $fid;                          my $t = shift || return;
182                            my $iso = BackupPC::Lib::timeStamp($t);
183                            $iso =~ s/\s/T/;
184                            return $iso;
185                    }
186    
187                  my $id = $db->uri_to_id($uri);                  while (my $row = $sth->fetchrow_hashref()) {
                 next unless ($id == -1);  
188    
189                  # create a document object                          my $fid = $row->{'fid'} || die "no fid?";
190                  my $doc = HyperEstraier::Document->new;                          my $uri = 'file:///' . $fid;
191    
192                  # add attributes to the document object                          my $id = ($hest_db || $hest_node)->uri_to_id($uri);
193                  $doc->add_attr('@uri', $uri);                          next unless ($id == -1);
194    
195                  foreach my $c (@{ $sth->{NAME} }) {                          # create a document object
196                          $doc->add_attr($c, $row->{$c}) if ($row->{$c});                          my $doc = HyperEstraier::Document->new;
197    
198                            # add attributes to the document object
199                            $doc->add_attr('@uri', $uri);
200    
201                            foreach my $c (@{ $sth->{NAME} }) {
202                                    $doc->add_attr($c, $row->{$c}) if ($row->{$c});
203                            }
204    
205                            #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
206    
207                            # add the body text to the document object
208                            my $path = $row->{'filepath'};
209                            $doc->add_text($path);
210                            $path =~ s/(.)/$1 /g;
211                            $doc->add_hidden_text($path);
212    
213                            print STDERR $doc->dump_draft,"\n" if ($debug > 1);
214    
215                            # register the document object to the database
216                            if ($hest_db) {
217                                    $hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
218                            } elsif ($hest_node) {
219                                    $hest_node->put_doc($doc);
220                            } else {
221                                    die "not supported";
222                            }
223                            $added++;
224                  }                  }
225    
226                  #$doc->add_attr('@cdate', fmt_date($row->{'date'}));                  print " $added";
227                    $hest_db->sync() if ($index_path);
228    
229                  # 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--;  
                 }  
230    
231          }          } while ($results == EST_CHUNK);
232    
233          print "sync $added new files";          if ($index_path) {
234          $db->sync();                  print ", close";
235          print " close";                  $hest_db->close();
236          $db->close();          }
237    
238          my $dur = (time() - $t) || 1;          my $dur = (time() - $t) || 1;
239          printf(" [%.2f/s new %.2f/s dur: %s]\n",          printf(" [%.2f/s dur: %s]\n",
                 ( $results / $dur ),  
240                  ( $added / $dur ),                  ( $added / $dur ),
241                  fmt_time($dur)                  fmt_time($dur)
242          );          );
# Line 216  sub hest_update { Line 246  sub hest_update {
246    
247    
248  ## update index ##  ## update index ##
249  if ($opt{i} || ($index_path && ! -e $index_path)) {  if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) {
250          # update all          # update all
251          print "force update of HyperEstraier index ";          print "force update of HyperEstraier index ";
252          print "importing existing data" unless (-e $index_path);          print "importing existing data" unless (-e $index_path);
# Line 440  foreach my $host_key (keys %{$hosts}) { Line 470  foreach my $host_key (keys %{$hosts}) {
470                                  fmt_time($dur)                                  fmt_time($dur)
471                          );                          );
472    
473                          hest_update($hostID, $shareID, $backupNum);                          hest_update($hostID, $shareID, $backupNum) if ($nf + $nd > 0);
474                  }                  }
475    
476          }          }

Legend:
Removed from v.90  
changed lines
  Added in v.117

  ViewVC Help
Powered by ViewVC 1.1.26