/[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 82 by dpavlin, Sun Aug 28 09:12:54 2005 UTC revision 98 by dpavlin, Tue Aug 30 14:19:54 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 => 10000;
17    
18  my $debug = 0;  my $debug = 0;
19  $|=1;  $|=1;
# Line 41  my $beenThere = {}; Line 42  my $beenThere = {};
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};  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    
# Line 65  if ($opt{v}) { Line 72  if ($opt{v}) {
72          $debug = $opt{v};          $debug = $opt{v};
73  }  }
74    
75  ## update index ##  #---- subs ----
 if ($opt{i}) {  
   
         print "updating HyperEstraier files ";  
           
         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,  
                         files.path                      AS filepath,  
                         files.date                      AS date,  
                         files.type                      AS filetype,  
                         files.size                      AS size,  
                         files.shareid                   AS shareid  
                 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  
         });  
76    
77          $sth->execute();  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          my $dot = int($sth->rows / 15);  sub curr_time {
87            return strftime($t_fmt,localtime());
88    }
89    
90          print $sth->rows, " files ($dot/#) ";  my $hest_db;
91    
92          sub fmt_date {  sub signal {
93                  my $t = shift || return;          my($sig) = @_;
94                  my $iso = BackupPC::Lib::timeStamp($t);          if ($hest_db) {
95                  $iso =~ s/\s/T/;                  print "\nCaught a SIG$sig--syncing database and shutting down\n";
96                  return $iso;                  $hest_db->sync();
97                    $hest_db->close();
98          }          }
99            exit(0);
100    }
101    
102          my $i = 0;  $SIG{'INT'}  = \&signal;
103          my $max = int($sth->rows / $dot);  $SIG{'QUIT'} = \&signal;
104    
105          $index_path = $TopDir . '/' . $index_path;  sub hest_update {
         $index_path =~ s#//#/#g;  
106    
107          print "index $index_path...";          my ($host_id, $share_id, $num) = @_;
108          use HyperEstraier;  
109          my $db = HyperEstraier::Database->new();          print curr_time," updating HyperEstraier:";
         $db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);  
110    
111            my $t = time();
112    
113          while (my $row = $sth->fetchrow_hashref()) {          my $offset = 0;
114            my $added = 0;
115    
116                  # create a document object          print " opening index $index_path";
117                  my $doc = HyperEstraier::Document->new;          $hest_db = HyperEstraier::Database->new();
118            $hest_db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
119    
120                  # add attributes to the document object          my $results = 0;
                 $doc->add_attr('@uri', 'file:///' . $row->{'fid'});  
121    
122                  foreach my $c (qw/fid hname sname sharename backupNum filename filepath shareid/) {          do {
123                          $doc->add_attr($c, $row->{$c}) if ($row->{$c});  
124                    my $where = '';
125                    if ($host_id && $share_id && $num) {
126                            $where = qq{
127                            WHERE
128                                    hosts.id = ? AND
129                                    shares.id = ? AND
130                                    files.backupnum = ?
131                            };
132                  }                  }
133    
134                  $doc->add_attr('date', fmt_date($row->{'date'}));                  my $limit = sprintf('LIMIT '.EST_CHUNK.' OFFSET %d', $offset);
135    
136                  # add the body text to the document object                  my $sth = $dbh->prepare(qq{
137                  my $path = $row->{'filepath'};                          SELECT
138                  $doc->add_text($path);                                  files.id                        AS fid,
139                  $path =~ s/(.)/$1 /g;                                  hosts.name                      AS hname,
140                  $doc->add_hidden_text($path);                                  shares.name                     AS sname,
141                                    -- shares.share                 AS sharename,
142                  print STDERR $doc->dump_draft,"\n" if ($debug > 1);                                  files.backupnum                 AS backupnum,
143                                    -- files.name                   AS filename,
144                  # register the document object to the database                                  files.path                      AS filepath,
145                  $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);                                  files.date                      AS date,
146                                    files.type                      AS type,
147                  $i++;                                  files.size                      AS size,
148                  if ($i % $dot == 0) {                                  files.shareid                   AS shareid,
149                          print "$max ";                                  backups.date                    AS backup_date
150                          $max--;                          FROM files
151                                    INNER JOIN shares       ON files.shareID=shares.ID
152                                    INNER JOIN hosts        ON hosts.ID = shares.hostID
153                                    INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID
154                            $where
155                            $limit
156                    });
157    
158                    $sth->execute(@_);
159                    $results = $sth->rows;
160    
161                    if ($results == 0) {
162                            print " - no more files\n";
163                            last;
164                  }                  }
165    
166          }                  sub fmt_date {
167                            my $t = shift || return;
168                            my $iso = BackupPC::Lib::timeStamp($t);
169                            $iso =~ s/\s/T/;
170                            return $iso;
171                    }
172    
173                    while (my $row = $sth->fetchrow_hashref()) {
174    
175                            my $fid = $row->{'fid'} || die "no fid?";
176                            my $uri = 'file:///' . $fid;
177    
178                            my $id = $hest_db->uri_to_id($uri);
179                            next unless ($id == -1);
180    
181          print "sync";                          # create a document object
182          $db->sync();                          my $doc = HyperEstraier::Document->new;
         print " close\n";  
         $db->close();  
183    
184          exit;                          # add attributes to the document object
185                            $doc->add_attr('@uri', $uri);
186    
187                            foreach my $c (@{ $sth->{NAME} }) {
188                                    $doc->add_attr($c, $row->{$c}) if ($row->{$c});
189                            }
190    
191                            #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
192    
193                            # add the body text to the document object
194                            my $path = $row->{'filepath'};
195                            $doc->add_text($path);
196                            $path =~ s/(.)/$1 /g;
197                            $doc->add_hidden_text($path);
198    
199                            print STDERR $doc->dump_draft,"\n" if ($debug > 1);
200    
201                            # register the document object to the database
202                            $hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
203                            $added++;
204                    }
205    
206                    print " $added";
207                    $hest_db->sync();
208    
209                    $offset += EST_CHUNK;
210    
211            } while ($results == EST_CHUNK);
212    
213            print ", close";
214            $hest_db->close();
215    
216            my $dur = (time() - $t) || 1;
217            printf(" [%.2f/s dur: %s]\n",
218                    ( $added / $dur ),
219                    fmt_time($dur)
220            );
221  }  }
222    
223  ###################################create tables############################3  #---- /subs ----
224    
225    
226    ## update index ##
227    if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) {
228            # update all
229            print "force update of HyperEstraier index ";
230            print "importing existing data" unless (-e $index_path);
231            print "by -i flag" if ($opt{i});
232            print "\n";
233            hest_update();
234    }
235    
236    ## create tables ##
237  if ($opt{c}) {  if ($opt{c}) {
238          sub do_index {          sub do_index {
239                  my $index = shift || return;                  my $index = shift || return;
# Line 246  if ($opt{c}) { Line 323  if ($opt{c}) {
323    
324  }  }
325    
326    ## delete data before inseting ##
327  if ($opt{d}) {  if ($opt{d}) {
328          print "deleting ";          print "deleting ";
329          foreach my $table (qw(files dvds backups shares hosts)) {          foreach my $table (qw(files dvds backups shares hosts)) {
# Line 257  if ($opt{d}) { Line 335  if ($opt{d}) {
335          $dbh->commit;          $dbh->commit;
336  }  }
337    
338  #################################INSERT VALUES#############################  ## insert new values ##
339    
340  # get hosts  # get hosts
341  $hosts = $bpc->HostInfoRead();  $hosts = $bpc->HostInfoRead();
# Line 291  INSERT INTO files Line 369  INSERT INTO files
369          VALUES (?,?,?,?,?,?,?)          VALUES (?,?,?,?,?,?,?)
370  });  });
371    
 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;  
 }  
   
372  foreach my $host_key (keys %{$hosts}) {  foreach my $host_key (keys %{$hosts}) {
373    
374          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";
# Line 355  foreach my $host_key (keys %{$hosts}) { Line 424  foreach my $host_key (keys %{$hosts}) {
424                          next if ($count > 0);                          next if ($count > 0);
425    
426                          # dump some log                          # dump some log
427                          print strftime($t_fmt,localtime())," ", $share;                          print curr_time," ", $share;
428    
429                          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);
430    
# Line 378  foreach my $host_key (keys %{$hosts}) { Line 447  foreach my $host_key (keys %{$hosts}) {
447                                  ( ($f+$d) / $dur ),                                  ( ($f+$d) / $dur ),
448                                  fmt_time($dur)                                  fmt_time($dur)
449                          );                          );
450    
451                            hest_update($hostID, $shareID, $backupNum);
452                  }                  }
453    
454          }          }

Legend:
Removed from v.82  
changed lines
  Added in v.98

  ViewVC Help
Powered by ViewVC 1.1.26