/[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 249 by dpavlin, Fri Dec 9 16:27:49 2005 UTC revision 311 by dpavlin, Sun Jan 29 14:54:17 2006 UTC
# Line 12  use Time::HiRes qw/time/; Line 12  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;  use BackupPC::SearchLib;
15    use Cwd qw/abs_path/;
16    
17  use constant BPC_FTYPE_DIR => 5;  use constant BPC_FTYPE_DIR => 5;
18  use constant EST_CHUNK => 100000;  use constant EST_CHUNK => 100000;
# Line 24  $|=1; Line 25  $|=1;
25    
26  my $start_t = time();  my $start_t = time();
27    
28  my $pidfile = new File::Pid;  my $pid_path = abs_path($0);
29    $pid_path =~ s/\W+/_/g;
30    
31    my $pidfile = new File::Pid({
32            file => "/tmp/$pid_path",
33    });
34    
35  if (my $pid = $pidfile->running ) {  if (my $pid = $pidfile->running ) {
36          die "$0 already running: $pid\n";          die "$0 already running: $pid\n";
# Line 32  if (my $pid = $pidfile->running ) { Line 38  if (my $pid = $pidfile->running ) {
38          $pidfile->remove;          $pidfile->remove;
39          $pidfile = new File::Pid;          $pidfile = new File::Pid;
40  }  }
 $pidfile->write;  
41  print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n";  print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n";
42    $pidfile->write;
43    
44  my $t_fmt = '%Y-%m-%d %H:%M:%S';  my $t_fmt = '%Y-%m-%d %H:%M:%S';
45    
# Line 46  my $beenThere = {}; Line 52  my $beenThere = {};
52  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
53  my $user = $Conf{SearchUser} || '';  my $user = $Conf{SearchUser} || '';
54    
55  my $use_hest = $Conf{HyperEstraierIndex};  my $index_node_url = $Conf{HyperEstraierIndex};
 my ($index_path, $index_node_url) = BackupPC::SearchLib::getHyperEstraier_url($use_hest);  
56    
57  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
58    
59  my %opt;  my %opt;
60    
61  if ( !getopts("cdm:v:ijf", \%opt ) ) {  if ( !getopts("cdm:v:ijfq", \%opt ) ) {
62          print STDERR <<EOF;          print STDERR <<EOF;
63  usage: $0 [-c|-d] [-m num] [-v|-v level] [-i|-j|-f]  usage: $0 [-c|-d] [-m num] [-v|-v level] [-i|-j|-f]
64    
# Line 65  Options: Line 70  Options:
70          -i      update Hyper Estraier full text index          -i      update Hyper Estraier full text index
71          -j      update full text, don't check existing files          -j      update full text, don't check existing files
72          -f      don't do anything with full text index          -f      don't do anything with full text index
73            -q      be quiet for hosts without changes
74    
75  Option -j is variation on -i. It will allow faster initial creation  Option -j is variation on -i. It will allow faster initial creation
76  of full-text index from existing database.  of full-text index from existing database.
# Line 81  if ($opt{v}) { Line 87  if ($opt{v}) {
87          $debug = $opt{v};          $debug = $opt{v};
88  } elsif ($opt{f}) {  } elsif ($opt{f}) {
89          print "WARNING: disabling full-text index update. You need to re-run $0 -j !\n";          print "WARNING: disabling full-text index update. You need to re-run $0 -j !\n";
90          ($use_hest, $index_path, $index_node_url) = (undef, undef, undef);          $index_node_url = undef;
91  }  }
92    
93  #---- subs ----  #---- subs ----
# Line 99  sub curr_time { Line 105  sub curr_time {
105          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
106  }  }
107    
 my $hest_db;  
108  my $hest_node;  my $hest_node;
109    
 sub signal {  
         my($sig) = @_;  
         if ($hest_db) {  
                 print "\nCaught a SIG$sig--syncing database and shutting down\n";  
                 $hest_db->sync();  
                 $hest_db->close();  
         }  
         exit(0);  
 }  
   
 $SIG{'INT'}  = \&signal;  
 $SIG{'QUIT'} = \&signal;  
   
110  sub hest_update {  sub hest_update {
111    
112          my ($host_id, $share_id, $num) = @_;          my ($host_id, $share_id, $num) = @_;
113    
114          my $skip_check = $opt{j} && print STDERR "Skipping check for existing files -- this should be used only with initital import\n";          my $skip_check = $opt{j} && print STDERR "Skipping check for existing files -- this should be used only with initital import\n";
115    
116          unless (defined($use_hest)) {          unless (defined($index_node_url)) {
117                  print STDERR "HyperEstraier support not enabled in configuration\n";                  print STDERR "HyperEstraier support not enabled in configuration\n";
118                  $use_hest = 0;                  $index_node_url = 0;
119                  return;                  return;
120          }          }
121    
122          return unless($use_hest);          print curr_time," updating Hyper Estraier:";
   
         print curr_time," updating HyperEstraier:";  
123    
124          my $t = time();          my $t = time();
125    
126          my $offset = 0;          my $offset = 0;
127          my $added = 0;          my $added = 0;
128    
129          print " opening index $use_hest";          print " opening index $index_node_url";
130          if ($index_path) {          if ($index_node_url) {
131                  $hest_db = HyperEstraier::Database->new();                  $hest_node ||= Search::Estraier::Node->new(
132                  $hest_db->open($TopDir . $index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);                          url => $index_node_url,
133                  print " directly";                          user => 'admin',
134          } elsif ($index_node_url) {                          passwd => 'admin',
135                  $hest_node ||= HyperEstraier::Node->new($index_node_url);                          croak_on_error => 1,
136                  $hest_node->set_auth('admin', 'admin');                  );
137                  print " via node URL";                  print " via node URL";
138          } else {          } else {
139                  die "don't know how to use HyperEstraier Index $use_hest";                  die "don't know how to use Hyper Estraier Index $index_node_url";
140          }          }
         print " increment is " . EST_CHUNK . " files:";  
141    
142          my $results = 0;          my $results = 0;
143    
# Line 195  sub hest_update { Line 184  sub hest_update {
184    
185                  if ($results == 0) {                  if ($results == 0) {
186                          print " - no new files\n";                          print " - no new files\n";
187                          last;                          return;
188                    } else {
189                            print " - $results files: ";
190                  }                  }
191    
192                  sub fmt_date {                  sub fmt_date {
# Line 211  sub hest_update { Line 202  sub hest_update {
202                          my $uri = 'file:///' . $fid;                          my $uri = 'file:///' . $fid;
203    
204                          unless ($skip_check) {                          unless ($skip_check) {
205                                  my $id = ($hest_db || $hest_node)->uri_to_id($uri);                                  my $id = $hest_node->uri_to_id($uri);
206                                  next unless ($id == -1);                                  next if ($id && $id == -1);
207                          }                          }
208    
209                          # create a document object                          # create a document object
210                          my $doc = HyperEstraier::Document->new;                          my $doc = Search::Estraier::Document->new;
211    
212                          # add attributes to the document object                          # add attributes to the document object
213                          $doc->add_attr('@uri', $uri);                          $doc->add_attr('@uri', $uri);
214    
215                          foreach my $c (@{ $sth->{NAME} }) {                          foreach my $c (@{ $sth->{NAME} }) {
216                                    print STDERR "attr $c = $row->{$c}\n" if ($debug > 2);
217                                  $doc->add_attr($c, $row->{$c}) if (defined($row->{$c}));                                  $doc->add_attr($c, $row->{$c}) if (defined($row->{$c}));
218                          }                          }
219    
# Line 236  sub hest_update { Line 228  sub hest_update {
228                          print STDERR $doc->dump_draft,"\n" if ($debug > 1);                          print STDERR $doc->dump_draft,"\n" if ($debug > 1);
229    
230                          # register the document object to the database                          # register the document object to the database
231                          if ($hest_db) {                          if ($hest_node) {
                                 $hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);  
                         } elsif ($hest_node) {  
232                                  $hest_node->put_doc($doc);                                  $hest_node->put_doc($doc);
233                          } else {                          } else {
234                                  die "not supported";                                  die "not supported";
# Line 247  sub hest_update { Line 237  sub hest_update {
237                  }                  }
238    
239                  print " $added";                  print " $added";
                 $hest_db->sync() if ($index_path);  
240    
241                  $offset += EST_CHUNK;                  $offset += EST_CHUNK;
242    
243          } while ($results == EST_CHUNK);          } while ($results == EST_CHUNK);
244    
         if ($index_path) {  
                 print ", close";  
                 $hest_db->close();  
         }  
   
245          my $dur = (time() - $t) || 1;          my $dur = (time() - $t) || 1;
246          printf(" [%.2f/s dur: %s]\n",          printf(" [%.2f/s dur: %s]\n",
247                  ( $added / $dur ),                  ( $added / $dur ),
# Line 269  sub hest_update { Line 253  sub hest_update {
253    
254    
255  ## update index ##  ## update index ##
256  if (($opt{i} || $opt{j} || ($index_path && ! -e $TopDir . $index_path)) && !$opt{c}) {  if ( ( $opt{i} || $opt{j} ) && !$opt{c} ) {
257          # update all          # update all
258          print "force update of HyperEstraier index ";          print "force update of Hyper Estraier index ";
         print "importing existing data" unless (-e $TopDir . $index_path);  
259          print "by -i flag" if ($opt{i});          print "by -i flag" if ($opt{i});
260          print "by -j flag" if ($opt{j});          print "by -j flag" if ($opt{j});
261          print "\n";          print "\n";
# Line 480  foreach my $host_key (@hosts) { Line 463  foreach my $host_key (@hosts) {
463          }          }
464    
465          $host_nr++;          $host_nr++;
         print "host ", $hosts->{$host_key}->{'host'}, " [",  
                 $host_nr, "/", ($#hosts + 1), "]: ";  
   
466          # get backups for a host          # get backups for a host
467          my @backups = $bpc->BackupInfoRead($hostname);          my @backups = $bpc->BackupInfoRead($hostname);
468          my $incs = scalar @backups;          my $incs = scalar @backups;
         print  "$incs increments\n";  
469    
470            my $host_header = sprintf("host %s [%d/%d]: %d increments\n",
471                    $hosts->{$host_key}->{'host'},
472                    $host_nr,
473                    ($#hosts + 1),
474                    $incs
475            );
476            print $host_header unless ($opt{q});
477    
478          my $inc_nr = 0;          my $inc_nr = 0;
479          $beenThere = {};          $beenThere = {};
480    
# Line 499  foreach my $host_key (@hosts) { Line 486  foreach my $host_key (@hosts) {
486                  my $backupNum = $backup->{'num'};                  my $backupNum = $backup->{'num'};
487                  my @backupShares = ();                  my @backupShares = ();
488    
489                  printf("%-10s %2d/%-2d #%-2d %s %5s/%5s files (date: %s dur: %s)\n",                  my $share_header = sprintf("%-10s %2d/%-2d #%-2d %s %5s/%5s files (date: %s dur: %s)\n",
490                          $hosts->{$host_key}->{'host'},                          $hosts->{$host_key}->{'host'},
491                          $inc_nr, $incs, $backupNum,                          $inc_nr, $incs, $backupNum,
492                          $backup->{type} || '?',                          $backup->{type} || '?',
# Line 507  foreach my $host_key (@hosts) { Line 494  foreach my $host_key (@hosts) {
494                          strftime($t_fmt,localtime($backup->{startTime})),                          strftime($t_fmt,localtime($backup->{startTime})),
495                          fmt_time($backup->{endTime} - $backup->{startTime})                          fmt_time($backup->{endTime} - $backup->{startTime})
496                  );                  );
497                    print $share_header unless ($opt{q});
498    
499                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
500                  foreach my $share ($files->shareList($backupNum)) {                  foreach my $share ($files->shareList($backupNum)) {
# Line 520  foreach my $host_key (@hosts) { Line 508  foreach my $host_key (@hosts) {
508                          # skip if allready in database!                          # skip if allready in database!
509                          next if ($count > 0);                          next if ($count > 0);
510    
511                            # dump host and share header for -q
512                            if ($opt{q}) {
513                                    if ($host_header) {
514                                            print $host_header;
515                                            $host_header = undef;
516                                    }
517                                    print $share_header;
518                            }
519    
520                          # dump some log                          # dump some log
521                          print curr_time," ", $share;                          print curr_time," ", $share;
522    
# Line 533  foreach my $host_key (@hosts) { Line 530  foreach my $host_key (@hosts) {
530    
531                          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);
532    
533                          $sth->{update_backups_size}->execute(                          eval {
534                                  $size,                                  $sth->{update_backups_size}->execute(
535                                  $hostID,                                          $size,
536                                  $backupNum,                                          $hostID,
537                                  $backup->{'endTime'},                                          $backupNum,
538                                  substr($backup->{'type'},0,4),                                          $backup->{'endTime'},
539                                  $shareID,                                          substr($backup->{'type'},0,4),
540                          );                                          $shareID,
541                                    );
542                          print " commit";                                  print " commit";
543                          $dbh->commit();                                  $dbh->commit();
544                            };
545                            if ($@) {
546                                    print " rollback";
547                                    $dbh->rollback();
548                            }
549    
550                          my $dur = (time() - $t) || 1;                          my $dur = (time() - $t) || 1;
551                          printf(" %d/%d files %d/%d dirs %0.2f MB [%.2f/s dur: %s]\n",                          printf(" %d/%d files %d/%d dirs %0.2f MB [%.2f/s dur: %s]\n",

Legend:
Removed from v.249  
changed lines
  Added in v.311

  ViewVC Help
Powered by ViewVC 1.1.26