/[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 242 by dpavlin, Fri Nov 18 19:40:47 2005 UTC revision 250 by dpavlin, Sun Dec 11 14:27:45 2005 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;
19    
20    # daylight saving time change offset for 1h
21    my $dst_offset = 60 * 60;
22    
23  my $debug = 0;  my $debug = 0;
24  $|=1;  $|=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 29  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 50  my $dbh = DBI->connect($dsn, $user, "", Line 59  my $dbh = DBI->connect($dsn, $user, "",
59    
60  my %opt;  my %opt;
61    
62  if ( !getopts("cdm:v:ij", \%opt ) ) {  if ( !getopts("cdm:v:ijf", \%opt ) ) {
63          print STDERR <<EOF;          print STDERR <<EOF;
64  usage: $0 [-c|-d] [-m num] [-v|-v level] [-i]  usage: $0 [-c|-d] [-m num] [-v|-v level] [-i|-j|-f]
65    
66  Options:  Options:
67          -c      create database on first use          -c      create database on first use
# Line 61  Options: Line 70  Options:
70          -v num  set verbosity (debug) level (default $debug)          -v num  set verbosity (debug) level (default $debug)
71          -i      update Hyper Estraier full text index          -i      update Hyper Estraier full text index
72          -j      update full text, don't check existing files          -j      update full text, don't check existing files
73            -f      don't do anything with full text index
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.
77    
78    Option -f will create database which is out of sync with full text index. You
79    will have to re-run $0 with -i to fix it.
80    
81  EOF  EOF
82          exit 1;          exit 1;
83  }  }
# Line 72  EOF Line 85  EOF
85  if ($opt{v}) {  if ($opt{v}) {
86          print "Debug level at $opt{v}\n";          print "Debug level at $opt{v}\n";
87          $debug = $opt{v};          $debug = $opt{v};
88    } elsif ($opt{f}) {
89            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);
91  }  }
92    
93  #---- subs ----  #---- subs ----
# Line 111  sub hest_update { Line 127  sub hest_update {
127    
128          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";
129    
130          unless ($use_hest) {          unless (defined($use_hest)) {
131                  print STDERR "HyperEstraier support not enabled in configuration\n";                  print STDERR "HyperEstraier support not enabled in configuration\n";
132                    $use_hest = 0;
133                  return;                  return;
134          }          }
135    
136            return unless($use_hest);
137    
138          print curr_time," updating HyperEstraier:";          print curr_time," updating HyperEstraier:";
139    
140          my $t = time();          my $t = time();
# Line 448  INSERT INTO files Line 467  INSERT INTO files
467          VALUES (?,?,?,?,?,?,?)          VALUES (?,?,?,?,?,?,?)
468  });  });
469    
470  foreach my $host_key (keys %{$hosts}) {  my @hosts = keys %{$hosts};
471    my $host_nr = 0;
472    
473    foreach my $host_key (@hosts) {
474    
475          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";
476    
# Line 463  foreach my $host_key (keys %{$hosts}) { Line 485  foreach my $host_key (keys %{$hosts}) {
485                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);
486          }          }
487    
488          print "host ".$hosts->{$host_key}->{'host'}.": ";          $host_nr++;
489            print "host ", $hosts->{$host_key}->{'host'}, " [",
490                    $host_nr, "/", ($#hosts + 1), "]: ";
491    
492          # get backups for a host          # get backups for a host
493          my @backups = $bpc->BackupInfoRead($hostname);          my @backups = $bpc->BackupInfoRead($hostname);
# Line 515  foreach my $host_key (keys %{$hosts}) { Line 539  foreach my $host_key (keys %{$hosts}) {
539    
540                          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);
541    
542                          $sth->{update_backups_size}->execute(                          eval {
543                                  $size,                                  $sth->{update_backups_size}->execute(
544                                  $hostID,                                          $size,
545                                  $backupNum,                                          $hostID,
546                                  $backup->{'endTime'},                                          $backupNum,
547                                  substr($backup->{'type'},0,4),                                          $backup->{'endTime'},
548                                  $shareID,                                          substr($backup->{'type'},0,4),
549                          );                                          $shareID,
550                                    );
551                          print " commit";                                  print " commit";
552                          $dbh->commit();                                  $dbh->commit();
553                            };
554                            if ($@) {
555                                    print " rollback";
556                                    $dbh->rollback();
557                            }
558    
559                          my $dur = (time() - $t) || 1;                          my $dur = (time() - $t) || 1;
560                          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",
# Line 588  sub found_in_db { Line 617  sub found_in_db {
617                  SELECT 1 FROM files                  SELECT 1 FROM files
618                  WHERE shareID = ? and                  WHERE shareID = ? and
619                          path = ? and                          path = ? and
620                          date = ? and                          size = ? and
621                          size = ?                          ( date = ? or date = ? or date = ? )
622                  LIMIT 1                  LIMIT 1
623          });          });
624    
625          my @param = ($shareID,$path,$date,$size);          my @param = ($shareID,$path,$size,$date, $date-$dst_offset, $date+$dst_offset);
626          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
627          my $rows = $sth->{file_in_db}->rows;          my $rows = $sth->{file_in_db}->rows;
628          print STDERR "## found_in_db($shareID,$path,$date,$size) ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3);          print STDERR "## found_in_db($shareID,$path,$date,$size) ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3);
# Line 643  sub recurseDir($$$$$$$$) { Line 672  sub recurseDir($$$$$$$$) {
672                                  $filesInBackup->{$path_key}->{'size'}                                  $filesInBackup->{$path_key}->{'size'}
673                          ));                          ));
674    
                         # daylight saving time change offset for 1h  
                         my $dst_offset = 60 * 60;  
   
675                          my $key_dst_prev = join(" ", (                          my $key_dst_prev = join(" ", (
676                                  $shareID,                                  $shareID,
677                                  $dir,                                  $dir,

Legend:
Removed from v.242  
changed lines
  Added in v.250

  ViewVC Help
Powered by ViewVC 1.1.26