/[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 67 by dpavlin, Mon Aug 22 08:58:59 2005 UTC revision 90 by dpavlin, Sun Aug 28 17:42:25 2005 UTC
# Line 40  my $beenThere = {}; Line 40  my $beenThere = {};
40    
41  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
42  my $user = $Conf{SearchUser} || '';  my $user = $Conf{SearchUser} || '';
43    my $index_path = $Conf{HyperEstraierIndex};
44    $index_path = $TopDir . '/' . $index_path;
45    $index_path =~ s#//#/#g;
46    
47    
48  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
49    
50  my %opt;  my %opt;
51    
52  if ( !getopts("cdm:v:", \%opt ) ) {  if ( !getopts("cdm:v:i", \%opt ) ) {
53          print STDERR <<EOF;          print STDERR <<EOF;
54  usage: $0 [-c|-d] [-m num] [-v|-v level]  usage: $0 [-c|-d] [-m num] [-v|-v level] [-i]
55    
56  Options:  Options:
57          -c      create database on first use          -c      create database on first use
58          -d      delete database before import          -d      delete database before import
59          -m num  import just num increments for one host          -m num  import just num increments for one host
60          -v num  set verbosity (debug) level (default $debug)          -v num  set verbosity (debug) level (default $debug)
61            -i      update HyperEstraier full text index
62  EOF  EOF
63          exit 1;          exit 1;
64  }  }
65    
66  ###################################create tables############################3  if ($opt{v}) {
67            print "Debug level at $opt{v}\n";
68            $debug = $opt{v};
69    }
70    
71    #---- subs ----
72    
73    sub fmt_time {
74            my $t = shift || return;
75            my $out = "";
76            my ($ss,$mm,$hh) = gmtime($t);
77            $out .= "${hh}h" if ($hh);
78            $out .= sprintf("%02d:%02d", $mm,$ss);
79            return $out;
80    }
81    
82    sub curr_time {
83            return strftime($t_fmt,localtime());
84    }
85    
86    my $hest_db;
87    
88    sub hest_update {
89    
90            my ($host_id, $share_id, $num) = @_;
91    
92            print curr_time," updating HyperEstraier: files";
93    
94            my $t = time();
95    
96            my $where = '';
97            if ($host_id && $share_id && $num) {
98                    $where = qq{
99                    WHERE
100                            hosts.id = ? AND
101                            shares.id = ? AND
102                            files.backupnum = ?
103                    };
104            }
105    
106            my $sth = $dbh->prepare(qq{
107                    SELECT
108                            files.id                        AS fid,
109                            hosts.name                      AS hname,
110                            shares.name                     AS sname,
111                            -- shares.share                 AS sharename,
112                            files.backupnum                 AS backupnum,
113                            -- files.name                   AS filename,
114                            files.path                      AS filepath,
115                            files.date                      AS date,
116                            files.type                      AS type,
117                            files.size                      AS size,
118                            files.shareid                   AS shareid,
119                            backups.date                    AS backup_date
120                    FROM files
121                            INNER JOIN shares       ON files.shareID=shares.ID
122                            INNER JOIN hosts        ON hosts.ID = shares.hostID
123                            INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID
124                    $where
125            });
126    
127            $sth->execute(@_);
128            my $results = $sth->rows;
129    
130            if ($results == 0) {
131                    print " no files\n";
132                    return;
133            }
134    
135            my $dot = int($results / 15) || 1;
136    
137            print " $results ($dot/#)";
138    
139            sub fmt_date {
140                    my $t = shift || return;
141                    my $iso = BackupPC::Lib::timeStamp($t);
142                    $iso =~ s/\s/T/;
143                    return $iso;
144            }
145    
146            my $max = int($results / $dot);
147    
148            print " index $index_path...";
149            use HyperEstraier;
150            my $db = HyperEstraier::Database->new();
151    
152    #       unless ($hest_db) {
153    #               print " open reader";
154    #               $hest_db = HyperEstraier::Database->new();
155    #
156    #       }
157    
158    
159            $db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
160    
161            my $added = 0;
162    
163            while (my $row = $sth->fetchrow_hashref()) {
164    
165                    my $fid = $row->{'fid'} || die "no fid?";
166                    my $uri = 'file:///' . $fid;
167    
168                    my $id = $db->uri_to_id($uri);
169                    next unless ($id == -1);
170    
171                    # create a document object
172                    my $doc = HyperEstraier::Document->new;
173    
174                    # add attributes to the document object
175                    $doc->add_attr('@uri', $uri);
176    
177                    foreach my $c (@{ $sth->{NAME} }) {
178                            $doc->add_attr($c, $row->{$c}) if ($row->{$c});
179                    }
180    
181                    #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
182    
183                    # add the body text to the document object
184                    my $path = $row->{'filepath'};
185                    $doc->add_text($path);
186                    $path =~ s/(.)/$1 /g;
187                    $doc->add_hidden_text($path);
188    
189                    print STDERR $doc->dump_draft,"\n" if ($debug > 1);
190    
191                    # register the document object to the database
192                    $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
193    
194                    $added++;
195                    if ($added % $dot == 0) {
196                            print "$max ";
197                            $max--;
198                    }
199    
200            }
201    
202            print "sync $added new files";
203            $db->sync();
204            print " close";
205            $db->close();
206    
207            my $dur = (time() - $t) || 1;
208            printf(" [%.2f/s new %.2f/s dur: %s]\n",
209                    ( $results / $dur ),
210                    ( $added / $dur ),
211                    fmt_time($dur)
212            );
213    }
214    
215    #---- /subs ----
216    
217    
218    ## update index ##
219    if ($opt{i} || ($index_path && ! -e $index_path)) {
220            # update all
221            print "force update of HyperEstraier index ";
222            print "importing existing data" unless (-e $index_path);
223            print "by -i flag" if ($opt{i});
224            print "\n";
225            hest_update();
226    }
227    
228    ## create tables ##
229  if ($opt{c}) {  if ($opt{c}) {
230          sub do_index {          sub do_index {
231                  my $index = shift || return;                  my $index = shift || return;
# Line 149  if ($opt{c}) { Line 315  if ($opt{c}) {
315    
316  }  }
317    
318    ## delete data before inseting ##
319  if ($opt{d}) {  if ($opt{d}) {
320          print "deleting ";          print "deleting ";
321          foreach my $table (qw(files dvds backups shares hosts)) {          foreach my $table (qw(files dvds backups shares hosts)) {
# Line 160  if ($opt{d}) { Line 327  if ($opt{d}) {
327          $dbh->commit;          $dbh->commit;
328  }  }
329    
330  if ($opt{v}) {  ## insert new values ##
         print "Debug level at $opt{v}\n";  
         $debug = $opt{v};  
 }  
   
 #################################INSERT VALUES#############################  
331    
332  # get hosts  # get hosts
333  $hosts = $bpc->HostInfoRead();  $hosts = $bpc->HostInfoRead();
# Line 199  INSERT INTO files Line 361  INSERT INTO files
361          VALUES (?,?,?,?,?,?,?)          VALUES (?,?,?,?,?,?,?)
362  });  });
363    
 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;  
 }  
   
364  foreach my $host_key (keys %{$hosts}) {  foreach my $host_key (keys %{$hosts}) {
365    
366          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 263  foreach my $host_key (keys %{$hosts}) { Line 416  foreach my $host_key (keys %{$hosts}) {
416                          next if ($count > 0);                          next if ($count > 0);
417    
418                          # dump some log                          # dump some log
419                          print strftime($t_fmt,localtime())," ", $share;                          print curr_time," ", $share;
420    
421                          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);
422    
# Line 286  foreach my $host_key (keys %{$hosts}) { Line 439  foreach my $host_key (keys %{$hosts}) {
439                                  ( ($f+$d) / $dur ),                                  ( ($f+$d) / $dur ),
440                                  fmt_time($dur)                                  fmt_time($dur)
441                          );                          );
442    
443                            hest_update($hostID, $shareID, $backupNum);
444                  }                  }
445    
446          }          }
# Line 330  sub found_in_db { Line 485  sub found_in_db {
485          my @data = @_;          my @data = @_;
486          shift @data;          shift @data;
487    
488          my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_;          my ($key, $shareID,undef,$name,$path,$date,undef,$size) = @_;
489    
490          return $beenThere->{$key} if (defined($beenThere->{$key}));          return $beenThere->{$key} if (defined($beenThere->{$key}));
491    
# Line 340  sub found_in_db { Line 495  sub found_in_db {
495                          path = ? and                          path = ? and
496                          date = ? and                          date = ? and
497                          size = ?                          size = ?
498                    LIMIT 1
499          });          });
500    
501          my @param = ($shareID,$path,$date,$size);          my @param = ($shareID,$path,$date,$size);
502          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
503          my $rows = $sth->{file_in_db}->rows;          my $rows = $sth->{file_in_db}->rows;
504          print STDERR "## found_in_db ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3);          print STDERR "## found_in_db($shareID,$path,$date,$size) ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3);
505    
506          $beenThere->{$key}++;          $beenThere->{$key}++;
507    
# Line 392  sub recurseDir($$$$$$$$) { Line 548  sub recurseDir($$$$$$$$) {
548                                  $filesInBackup->{$path_key}->{'size'}                                  $filesInBackup->{$path_key}->{'size'}
549                          ));                          ));
550    
551                            my $found;
552                          if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) {                          if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) {
553                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
554    
555                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
556                                          $new_dirs++;                                          $new_dirs++ unless ($found);
557                                          print STDERR " dir\n" if ($debug >= 2);                                          print STDERR " dir\n" if ($debug >= 2);
558                                  } else {                                  } else {
559                                          $new_files++;                                          $new_files++ unless ($found);
560                                          print STDERR " file\n" if ($debug >= 2);                                          print STDERR " file\n" if ($debug >= 2);
561                                  }                                  }
562                                  $size += $filesInBackup->{$path_key}->{'size'} || 0;                                  $size += $filesInBackup->{$path_key}->{'size'} || 0;

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

  ViewVC Help
Powered by ViewVC 1.1.26