/[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 118 by dpavlin, Sun Sep 11 14:31:49 2005 UTC
# Line 11  use Getopt::Std; Line 11  use Getopt::Std;
11  use Time::HiRes qw/time/;  use Time::HiRes qw/time/;
12  use File::Pid;  use File::Pid;
13  use POSIX qw/strftime/;  use POSIX qw/strftime/;
14    use BackupPC::SearchLib;
15    
16  use constant BPC_FTYPE_DIR => 5;  use constant BPC_FTYPE_DIR => 5;
17    use constant EST_CHUNK => 100000;
18    
19  my $debug = 0;  my $debug = 0;
20  $|=1;  $|=1;
# Line 41  my $beenThere = {}; Line 43  my $beenThere = {};
43  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
44  my $user = $Conf{SearchUser} || '';  my $user = $Conf{SearchUser} || '';
45    
46    my $use_hest = $Conf{HyperEstraierIndex};
47    my ($index_path, $index_node_url) = BackupPC::SearchLib::getHyperEstraier_url($use_hest);
48    
49  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
50    
51  my %opt;  my %opt;
# Line 64  if ($opt{v}) { Line 69  if ($opt{v}) {
69          $debug = $opt{v};          $debug = $opt{v};
70  }  }
71    
72  ## update index ##  #---- subs ----
73  if ($opt{i}) {  
74    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          my $index_dir = '/var/tmp/casket';  sub curr_time {
84            return strftime($t_fmt,localtime());
85    }
86    
87          print "updating HyperEstraier index $index_dir...";  my $hest_db;
88    my $hest_node;
89    
90          use HyperEstraier;  sub signal {
91          my $db = HyperEstraier::Database->new();          my($sig) = @_;
92          $db->open($index_dir, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);          if ($hest_db) {
93                    print "\nCaught a SIG$sig--syncing database and shutting down\n";
94          my $sth = $dbh->prepare(qq{                  $hest_db->sync();
95                  SELECT                  $hest_db->close();
96                          files.id                        AS fid,          }
97                          hosts.name                      AS hname,          exit(0);
98                          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  
         });  
99    
100          $sth->execute();  $SIG{'INT'}  = \&signal;
101    $SIG{'QUIT'} = \&signal;
102    
103          my $dot = int($sth->rows / 15);  sub hest_update {
104    
105          print $sth->rows, " files ($dot/#) ";          my ($host_id, $share_id, $num) = @_;
106    
107          sub fmt_date {          unless ($use_hest) {
108                  my $t = shift || return;                  print STDERR "HyperEstraier support not enabled in configuration\n";
109                  my $iso = BackupPC::Lib::timeStamp($t);                  return;
                 $iso =~ s/\s/T/;  
                 return $iso;  
110          }          }
111    
112          my $i = 0;          print curr_time," updating HyperEstraier:";
113          my $max = int($sth->rows / $dot);  
114            my $t = time();
115    
116          while (my $row = $sth->fetchrow_hashref()) {          my $offset = 0;
117            my $added = 0;
118    
119                  # create a document object          print " opening index $use_hest";
120                  my $doc = HyperEstraier::Document->new;          if ($index_path) {
121                    $hest_db = HyperEstraier::Database->new();
122                    $hest_db->open($TopDir . $index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
123                    print " directly";
124            } elsif ($index_node_url) {
125                    $hest_node ||= HyperEstraier::Node->new($index_node_url);
126                    $hest_node->set_auth('admin', 'admin');
127                    print " via node URL";
128            } else {
129                    die "don't know how to use HyperEstraier Index $use_hest";
130            }
131            print " increment is " . EST_CHUNK . " files:";
132    
133                  # add attributes to the document object          my $results = 0;
                 $doc->add_attr('@uri', 'file:///' . $row->{'fid'});  
134    
135                  foreach my $c (qw/fid hname sname sharename backupNum filename filepath shareid/) {          do {
136                          $doc->add_attr($c, $row->{$c}) if ($row->{$c});  
137                    my $where = '';
138                    my @data;
139                    if ($host_id && $share_id && $num) {
140                            $where = qq{
141                            WHERE
142                                    hosts.id = ? AND
143                                    shares.id = ? AND
144                                    files.backupnum = ?
145                            };
146                            @data = ( $host_id, $share_id, $num );
147                  }                  }
148    
149                  $doc->add_attr('date', fmt_date($row->{'date'}));                  my $limit = sprintf('LIMIT '.EST_CHUNK.' OFFSET %d', $offset);
150    
151                  # add the body text to the document object                  my $sth = $dbh->prepare(qq{
152                  my $path = $row->{'filepath'};                          SELECT
153                  $doc->add_text($path);                                  files.id                        AS fid,
154                  $path =~ s/(.)/$1 /g;                                  hosts.name                      AS hname,
155                  $doc->add_hidden_text($path);                                  shares.name                     AS sname,
156                                    -- shares.share                 AS sharename,
157                  print STDERR $doc->dump_draft,"\n" if ($debug > 1);                                  files.backupnum                 AS backupnum,
158                                    -- files.name                   AS filename,
159                  # register the document object to the database                                  files.path                      AS filepath,
160                  $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);                                  files.date                      AS date,
161                                    files.type                      AS type,
162                  $i++;                                  files.size                      AS size,
163                  if ($i % $dot == 0) {                                  files.shareid                   AS shareid,
164                          print "$max ";                                  backups.date                    AS backup_date
165                          $max--;                          FROM files
166                                    INNER JOIN shares       ON files.shareID=shares.ID
167                                    INNER JOIN hosts        ON hosts.ID = shares.hostID
168                                    INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID
169                            $where
170                            $limit
171                    });
172    
173                    $sth->execute(@data);
174                    $results = $sth->rows;
175    
176                    if ($results == 0) {
177                            print " - no new files\n";
178                            last;
179                  }                  }
180    
181          }                  sub fmt_date {
182                            my $t = shift || return;
183                            my $iso = BackupPC::Lib::timeStamp($t);
184                            $iso =~ s/\s/T/;
185                            return $iso;
186                    }
187    
188                    while (my $row = $sth->fetchrow_hashref()) {
189    
190                            my $fid = $row->{'fid'} || die "no fid?";
191                            my $uri = 'file:///' . $fid;
192    
193          print "sync";                          my $id = ($hest_db || $hest_node)->uri_to_id($uri);
194          $db->sync();                          next unless ($id == -1);
         print " close\n";  
         $db->close();  
195    
196          exit;                          # create a document object
197                            my $doc = HyperEstraier::Document->new;
198    
199                            # add attributes to the document object
200                            $doc->add_attr('@uri', $uri);
201    
202                            foreach my $c (@{ $sth->{NAME} }) {
203                                    $doc->add_attr($c, $row->{$c}) if ($row->{$c});
204                            }
205    
206                            #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
207    
208                            # add the body text to the document object
209                            my $path = $row->{'filepath'};
210                            $doc->add_text($path);
211                            $path =~ s/(.)/$1 /g;
212                            $doc->add_hidden_text($path);
213    
214                            print STDERR $doc->dump_draft,"\n" if ($debug > 1);
215    
216                            # register the document object to the database
217                            if ($hest_db) {
218                                    $hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
219                            } elsif ($hest_node) {
220                                    $hest_node->put_doc($doc);
221                            } else {
222                                    die "not supported";
223                            }
224                            $added++;
225                    }
226    
227                    print " $added";
228                    $hest_db->sync() if ($index_path);
229    
230                    $offset += EST_CHUNK;
231    
232            } while ($results == EST_CHUNK);
233    
234            if ($index_path) {
235                    print ", close";
236                    $hest_db->close();
237            }
238    
239            my $dur = (time() - $t) || 1;
240            printf(" [%.2f/s dur: %s]\n",
241                    ( $added / $dur ),
242                    fmt_time($dur)
243            );
244  }  }
245    
246  ###################################create tables############################3  #---- /subs ----
247    
248    
249    ## update index ##
250    if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) {
251            # update all
252            print "force update of HyperEstraier index ";
253            print "importing existing data" unless (-e $index_path);
254            print "by -i flag" if ($opt{i});
255            print "\n";
256            hest_update();
257    }
258    
259    ## create tables ##
260  if ($opt{c}) {  if ($opt{c}) {
261          sub do_index {          sub do_index {
262                  my $index = shift || return;                  my $index = shift || return;
# Line 242  if ($opt{c}) { Line 346  if ($opt{c}) {
346    
347  }  }
348    
349    ## delete data before inseting ##
350  if ($opt{d}) {  if ($opt{d}) {
351          print "deleting ";          print "deleting ";
352          foreach my $table (qw(files dvds backups shares hosts)) {          foreach my $table (qw(files dvds backups shares hosts)) {
# Line 253  if ($opt{d}) { Line 358  if ($opt{d}) {
358          $dbh->commit;          $dbh->commit;
359  }  }
360    
361  #################################INSERT VALUES#############################  ## insert new values ##
362    
363  # get hosts  # get hosts
364  $hosts = $bpc->HostInfoRead();  $hosts = $bpc->HostInfoRead();
# Line 287  INSERT INTO files Line 392  INSERT INTO files
392          VALUES (?,?,?,?,?,?,?)          VALUES (?,?,?,?,?,?,?)
393  });  });
394    
 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;  
 }  
   
395  foreach my $host_key (keys %{$hosts}) {  foreach my $host_key (keys %{$hosts}) {
396    
397          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 447  foreach my $host_key (keys %{$hosts}) {
447                          next if ($count > 0);                          next if ($count > 0);
448    
449                          # dump some log                          # dump some log
450                          print strftime($t_fmt,localtime())," ", $share;                          print curr_time," ", $share;
451    
452                          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);
453    
# Line 374  foreach my $host_key (keys %{$hosts}) { Line 470  foreach my $host_key (keys %{$hosts}) {
470                                  ( ($f+$d) / $dur ),                                  ( ($f+$d) / $dur ),
471                                  fmt_time($dur)                                  fmt_time($dur)
472                          );                          );
473    
474                            hest_update($hostID, $shareID, $backupNum) if ($nf + $nd > 0);
475                  }                  }
476    
477          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.26