/[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 81 by dpavlin, Sun Aug 28 08:40:06 2005 UTC revision 97 by dpavlin, Tue Aug 30 09:55:55 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_SYNC_EVERY => 10000;
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;
46    $index_path =~ s#//#/#g;
47    
48    
49  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
50    
# Line 64  if ($opt{v}) { Line 69  if ($opt{v}) {
69          $debug = $opt{v};          $debug = $opt{v};
70  }  }
71    
72  ## update index ##  #---- subs ----
 if ($opt{i}) {  
73    
74          my $index_dir = '/var/tmp/casket';  sub fmt_time {
75            my $t = shift || return;
76            my $out = "";
77            my ($ss,$mm,$hh) = gmtime($t);
78            $out .= "${hh}h" if ($hh);
79            $out .= sprintf("%02d:%02d", $mm,$ss);
80            return $out;
81    }
82    
83          print "updating HyperEstraier index $index_dir...";  sub curr_time {
84            return strftime($t_fmt,localtime());
85    }
86    
87          use HyperEstraier;  my $hest_db;
88          my $db = HyperEstraier::Database->new();  
89          $db->open($index_dir, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);  sub signal {
90            my($sig) = @_;
91            if ($hest_db) {
92                    print "\nCaught a SIG$sig--syncing database and shutting down\n";
93                    $hest_db->sync();
94                    $hest_db->close();
95            }
96            exit(0);
97    }
98    
99    $SIG{'INT'}  = \&signal;
100    $SIG{'QUIT'} = \&signal;
101    
102    sub hest_update {
103    
104            my ($host_id, $share_id, $num) = @_;
105    
106            print curr_time," updating HyperEstraier: select files";
107    
108            my $t = time();
109    
110            my $where = '';
111            if ($host_id && $share_id && $num) {
112                    $where = qq{
113                    WHERE
114                            hosts.id = ? AND
115                            shares.id = ? AND
116                            files.backupnum = ?
117                    };
118            }
119    
120          my $sth = $dbh->prepare(qq{          my $sth = $dbh->prepare(qq{
121                  SELECT                  SELECT
122                          files.id                        AS fid,                          files.id                        AS fid,
123                          hosts.name                      AS hname,                          hosts.name                      AS hname,
124                          shares.name                     AS sname,                          shares.name                     AS sname,
125                          shares.share                    AS sharename,                          -- shares.share                 AS sharename,
126                          files.backupNum                 AS backupNum,                          files.backupnum                 AS backupnum,
127                          files.name                      AS filename,                          -- files.name                   AS filename,
128                          files.path                      AS filepath,                          files.path                      AS filepath,
129                          files.date                      AS date,                          files.date                      AS date,
130                          files.type                      AS filetype,                          files.type                      AS type,
131                          files.size                      AS size,                          files.size                      AS size,
132                          files.shareid                   AS shareid                          files.shareid                   AS shareid,
133                            backups.date                    AS backup_date
134                  FROM files                  FROM files
135                          INNER JOIN shares       ON files.shareID=shares.ID                          INNER JOIN shares       ON files.shareID=shares.ID
136                          INNER JOIN hosts        ON hosts.ID = shares.hostID                          INNER JOIN hosts        ON hosts.ID = shares.hostID
137                          INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID                          INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID
138                    $where
139          });          });
140    
141          $sth->execute();          $sth->execute(@_);
142            my $results = $sth->rows;
143    
144            if ($results == 0) {
145                    print " - no files, skipping\n";
146                    return;
147            }
148    
149          my $dot = int($sth->rows / 15);          my $dot = int($results / 15) || 1;
150    
151          print $sth->rows, " files ($dot/#) ";          print " $results ($dot/#)";
152    
153          sub fmt_date {          sub fmt_date {
154                  my $t = shift || return;                  my $t = shift || return;
# Line 107  if ($opt{i}) { Line 157  if ($opt{i}) {
157                  return $iso;                  return $iso;
158          }          }
159    
160          my $i = 0;          my $max = int($results / $dot);
161          my $max = int($sth->rows / $dot);  
162            print ", opening index $index_path...";
163            use HyperEstraier;
164            my $db = HyperEstraier::Database->new();
165    
166    #       unless ($hest_db) {
167    #               print " open reader";
168    #               $hest_db = HyperEstraier::Database->new();
169    #
170    #       }
171    
172    
173            $db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
174    
175            my $added = 0;
176    
177          while (my $row = $sth->fetchrow_hashref()) {          while (my $row = $sth->fetchrow_hashref()) {
178    
179                    my $fid = $row->{'fid'} || die "no fid?";
180                    my $uri = 'file:///' . $fid;
181    
182                    my $id = $db->uri_to_id($uri);
183                    next unless ($id == -1);
184    
185                  # create a document object                  # create a document object
186                  my $doc = HyperEstraier::Document->new;                  my $doc = HyperEstraier::Document->new;
187    
188                  # add attributes to the document object                  # add attributes to the document object
189                  $doc->add_attr('@uri', 'file:///' . $row->{'fid'});                  $doc->add_attr('@uri', $uri);
190    
191                  foreach my $c (qw/fid hname sname sharename backupNum filename filepath shareid/) {                  foreach my $c (@{ $sth->{NAME} }) {
192                          $doc->add_attr($c, $row->{$c}) if ($row->{$c});                          $doc->add_attr($c, $row->{$c}) if ($row->{$c});
193                  }                  }
194    
195                  $doc->add_attr('date', fmt_date($row->{'date'}));                  #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
196    
197                  # add the body text to the document object                  # add the body text to the document object
198                  my $path = $row->{'filepath'};                  my $path = $row->{'filepath'};
# Line 135  if ($opt{i}) { Line 205  if ($opt{i}) {
205                  # register the document object to the database                  # register the document object to the database
206                  $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);                  $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
207    
208                  $i++;                  $added++;
209                  if ($i % $dot == 0) {                  if ($added % $dot == 0) {
210                          print "$max ";                          print "$max ";
211                          $max--;                          $max--;
212                  }                  }
213    
214                    if ($added % EST_SYNC_EVERY == 0) {
215                            print "sync ";
216                            $db->sync();
217                    }
218    
219          }          }
220    
221          print "sync";          print "sync $added new files";
222          $db->sync();          $db->sync();
223          print " close\n";          print ", close";
224          $db->close();          $db->close();
225    
226          exit;          my $dur = (time() - $t) || 1;
227            printf(" [%.2f/s new %.2f/s dur: %s]\n",
228                    ( $results / $dur ),
229                    ( $added / $dur ),
230                    fmt_time($dur)
231            );
232  }  }
233    
234  ###################################create tables############################3  #---- /subs ----
235    
236    
237    ## update index ##
238    if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) {
239            # update all
240            print "force update of HyperEstraier index ";
241            print "importing existing data" unless (-e $index_path);
242            print "by -i flag" if ($opt{i});
243            print "\n";
244            hest_update();
245    }
246    
247    ## create tables ##
248  if ($opt{c}) {  if ($opt{c}) {
249          sub do_index {          sub do_index {
250                  my $index = shift || return;                  my $index = shift || return;
# Line 242  if ($opt{c}) { Line 334  if ($opt{c}) {
334    
335  }  }
336    
337    ## delete data before inseting ##
338  if ($opt{d}) {  if ($opt{d}) {
339          print "deleting ";          print "deleting ";
340          foreach my $table (qw(files dvds backups shares hosts)) {          foreach my $table (qw(files dvds backups shares hosts)) {
# Line 253  if ($opt{d}) { Line 346  if ($opt{d}) {
346          $dbh->commit;          $dbh->commit;
347  }  }
348    
349  #################################INSERT VALUES#############################  ## insert new values ##
350    
351  # get hosts  # get hosts
352  $hosts = $bpc->HostInfoRead();  $hosts = $bpc->HostInfoRead();
# Line 287  INSERT INTO files Line 380  INSERT INTO files
380          VALUES (?,?,?,?,?,?,?)          VALUES (?,?,?,?,?,?,?)
381  });  });
382    
 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;  
 }  
   
383  foreach my $host_key (keys %{$hosts}) {  foreach my $host_key (keys %{$hosts}) {
384    
385          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 351  foreach my $host_key (keys %{$hosts}) { Line 435  foreach my $host_key (keys %{$hosts}) {
435                          next if ($count > 0);                          next if ($count > 0);
436    
437                          # dump some log                          # dump some log
438                          print strftime($t_fmt,localtime())," ", $share;                          print curr_time," ", $share;
439    
440                          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);
441    
# Line 374  foreach my $host_key (keys %{$hosts}) { Line 458  foreach my $host_key (keys %{$hosts}) {
458                                  ( ($f+$d) / $dur ),                                  ( ($f+$d) / $dur ),
459                                  fmt_time($dur)                                  fmt_time($dur)
460                          );                          );
461    
462                            hest_update($hostID, $shareID, $backupNum);
463                  }                  }
464    
465          }          }

Legend:
Removed from v.81  
changed lines
  Added in v.97

  ViewVC Help
Powered by ViewVC 1.1.26