/[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 153 by dpavlin, Mon Oct 10 12:07:12 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 143  sub hest_update { Line 162  sub hest_update {
162    
163                  my $where = '';                  my $where = '';
164                  my @data;                  my @data;
165                  if ($host_id && $share_id && $num) {                  if (defined($host_id) && defined($share_id) && defined($num)) {
166                          $where = qq{                          $where = qq{
167                          WHERE                          WHERE
168                                  hosts.id = ? AND                                  hosts.id = ? AND
# Line 209  sub hest_update { Line 228  sub hest_update {
228                          $doc->add_attr('@uri', $uri);                          $doc->add_attr('@uri', $uri);
229    
230                          foreach my $c (@{ $sth->{NAME} }) {                          foreach my $c (@{ $sth->{NAME} }) {
231                                  $doc->add_attr($c, $row->{$c}) if ($row->{$c});                                  $doc->add_attr($c, $row->{$c}) if (defined($row->{$c}));
232                          }                          }
233    
234                          #$doc->add_attr('@cdate', fmt_date($row->{'date'}));                          #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
# Line 256  sub hest_update { Line 275  sub hest_update {
275    
276    
277  ## update index ##  ## update index ##
278  if (($opt{i} || $opt{j} || ($index_path && ! -e $index_path)) && !$opt{c}) {  if (($opt{i} || $opt{j} || ($index_path && ! -e $TopDir . $index_path)) && !$opt{c}) {
279          # update all          # update all
280          print "force update of HyperEstraier index ";          print "force update of HyperEstraier index ";
281          print "importing existing data" unless (-e $index_path);          print "importing existing data" unless (-e $TopDir . $index_path);
282          print "by -i flag" if ($opt{i});          print "by -i flag" if ($opt{i});
283          print "by -j flag" if ($opt{j});          print "by -j flag" if ($opt{j});
284          print "\n";          print "\n";
# Line 278  if ($opt{c}) { Line 297  if ($opt{c}) {
297          }          }
298    
299          print "creating tables...\n";          print "creating tables...\n";
300          
301          $dbh->do(qq{          $dbh->do( qq{
302                  create table hosts (                  create table hosts (
303                          ID      SERIAL          PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
304                          name    VARCHAR(30)     NOT NULL,                          name    VARCHAR(30)     NOT NULL,
305                          IP      VARCHAR(15)                          IP      VARCHAR(15)
306                  );                              );            
307          });  
                 
         $dbh->do(qq{  
308                  create table shares (                  create table shares (
309                          ID      SERIAL          PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
310                          hostID  INTEGER         NOT NULL references hosts(id),                          hostID  INTEGER         NOT NULL references hosts(id),
311                          name    VARCHAR(30)     NOT NULL,                          name    VARCHAR(30)     NOT NULL,
312                          share   VARCHAR(200)    NOT NULL                          share   VARCHAR(200)    NOT NULL
313                  );                              );            
         });  
314    
         $dbh->do(qq{  
315                  create table dvds (                  create table dvds (
316                          ID      SERIAL          PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
317                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
318                          name    VARCHAR(255)    NOT NULL,                          name    VARCHAR(255)    NOT NULL,
319                          mjesto  VARCHAR(255)                          mjesto  VARCHAR(255)
320                  );                  );
321          });  
           
         $dbh->do(qq{  
322                  create table backups (                  create table backups (
323                          id      serial,                          id      serial,
324                          hostID  INTEGER         NOT NULL references hosts(id),                          hostID  INTEGER         NOT NULL references hosts(id),
# Line 316  if ($opt{c}) { Line 329  if ($opt{c}) {
329                          size    bigint          not null,                          size    bigint          not null,
330                          inc_size bigint         not null default -1,                          inc_size bigint         not null default -1,
331                          inc_deleted boolean     default false,                          inc_deleted boolean     default false,
332                            parts   integer         not null default 1,
333                          PRIMARY KEY(id)                          PRIMARY KEY(id)
334                  );                              );            
         });  
335    
         $dbh->do(qq{      
336                  create table files (                  create table files (
337                          ID              SERIAL,                          ID              SERIAL,
338                          shareID         INTEGER NOT NULL references shares(id),                          shareID         INTEGER NOT NULL references shares(id),
# Line 332  if ($opt{c}) { Line 344  if ($opt{c}) {
344                          size            bigint  NOT NULL,                          size            bigint  NOT NULL,
345                          primary key(id)                          primary key(id)
346                  );                  );
         });  
   
347    
         $dbh->do( qq{  
348                  create table archive (                  create table archive (
349                          id              serial,                          id              serial,
350                          dvd_nr          int not null,                          dvd_nr          int not null,
# Line 345  if ($opt{c}) { Line 354  if ($opt{c}) {
354                          date            timestamp default now(),                          date            timestamp default now(),
355                          primary key(id)                          primary key(id)
356                  );                        );      
         }  
         );  
357    
358          $dbh->do( qq{                  create table archive_backup (
359                  create table archive_backup                          archive_id      int not null references archive(id) on delete cascade,
                 (  
                         archive_id      int not null references archive(id),  
360                          backup_id       int not null references backups(id),                          backup_id       int not null references backups(id),
361                          primary key(archive_id, backup_id)                          primary key(archive_id, backup_id)
362                  );                  );
363    
364                    create table archive_burned (
365                            archive_id      int references archive(id),
366                            date            timestamp default now(),
367                            part            int not null default 1,
368                            copy            int not null default 1,
369                            iso_size bigint default -1
370                    );
371    
372                    create table backup_parts (
373                            id serial,
374                            backup_id int references backups(id),
375                            part_nr int not null check (part_nr > 0),
376                            tar_size bigint not null check (tar_size > 0),
377                            size bigint not null check (size > 0),
378                            md5 text not null,
379                            items int not null check (items > 0),
380                            date timestamp default now(),
381                            primary key(id)
382                    );
383          });          });
384    
385          print "creating indexes: ";          print "creating indexes: ";
# Line 372  if ($opt{c}) { Line 397  if ($opt{c}) {
397                  files:date                  files:date
398                  files:size                  files:size
399                  archive:dvd_nr                  archive:dvd_nr
400                    archive_burned:archive_id
401                    backup_parts:backup_id,part_nr
402          )) {          )) {
403                  do_index($index);                  do_index($index);
404          }          }
# Line 426  WHERE hostID=? AND num=? AND shareid=? Line 453  WHERE hostID=? AND num=? AND shareid=?
453    
454  $sth->{insert_backups} = $dbh->prepare(qq{  $sth->{insert_backups} = $dbh->prepare(qq{
455  INSERT INTO backups (hostID, num, date, type, shareid, size)  INSERT INTO backups (hostID, num, date, type, shareid, size)
456  VALUES (?,?,?,?,?,?)  VALUES (?,?,?,?,?,-1)
457    });
458    
459    $sth->{update_backups_size} = $dbh->prepare(qq{
460    UPDATE backups SET size = ?
461    WHERE hostID = ? and num = ? and date = ? and type =? and shareid = ?
462  });  });
463    
464  $sth->{insert_files} = $dbh->prepare(qq{  $sth->{insert_files} = $dbh->prepare(qq{
# Line 435  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 450  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 492  foreach my $host_key (keys %{$hosts}) { Line 529  foreach my $host_key (keys %{$hosts}) {
529                          # dump some log                          # dump some log
530                          print curr_time," ", $share;                          print curr_time," ", $share;
531    
                         my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);  
   
532                          $sth->{insert_backups}->execute(                          $sth->{insert_backups}->execute(
533                                  $hostID,                                  $hostID,
534                                  $backupNum,                                  $backupNum,
535                                  $backup->{'endTime'},                                  $backup->{'endTime'},
536                                  substr($backup->{'type'},0,4),                                  substr($backup->{'type'},0,4),
537                                  $shareID,                                  $shareID,
                                 $size,  
538                          );                          );
539    
540                          print " commit";                          my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
541                          $dbh->commit();  
542                            eval {
543                                    $sth->{update_backups_size}->execute(
544                                            $size,
545                                            $hostID,
546                                            $backupNum,
547                                            $backup->{'endTime'},
548                                            substr($backup->{'type'},0,4),
549                                            $shareID,
550                                    );
551                                    print " commit";
552                                    $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 567  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 622  sub recurseDir($$$$$$$$) { Line 672  sub recurseDir($$$$$$$$) {
672                                  $filesInBackup->{$path_key}->{'size'}                                  $filesInBackup->{$path_key}->{'size'}
673                          ));                          ));
674    
675                            my $key_dst_prev = join(" ", (
676                                    $shareID,
677                                    $dir,
678                                    $path_key,
679                                    $filesInBackup->{$path_key}->{'mtime'} - $dst_offset,
680                                    $filesInBackup->{$path_key}->{'size'}
681                            ));
682    
683                            my $key_dst_next = join(" ", (
684                                    $shareID,
685                                    $dir,
686                                    $path_key,
687                                    $filesInBackup->{$path_key}->{'mtime'} + $dst_offset,
688                                    $filesInBackup->{$path_key}->{'size'}
689                            ));
690    
691                          my $found;                          my $found;
692                          if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) {                          if (
693                                    ! defined($beenThere->{$key}) &&
694                                    ! defined($beenThere->{$key_dst_prev}) &&
695                                    ! defined($beenThere->{$key_dst_next}) &&
696                                    ! ($found = found_in_db($key, @data))
697                            ) {
698                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
699    
700                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {

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

  ViewVC Help
Powered by ViewVC 1.1.26