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

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

  ViewVC Help
Powered by ViewVC 1.1.26