/[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 130 by dpavlin, Fri Sep 23 08:54:10 2005 UTC revision 249 by dpavlin, Fri Dec 9 16:27:49 2005 UTC
# Line 16  use BackupPC::SearchLib; Line 16  use BackupPC::SearchLib;
16  use constant BPC_FTYPE_DIR => 5;  use constant BPC_FTYPE_DIR => 5;
17  use constant EST_CHUNK => 100000;  use constant EST_CHUNK => 100000;
18    
19    # daylight saving time change offset for 1h
20    my $dst_offset = 60 * 60;
21    
22  my $debug = 0;  my $debug = 0;
23  $|=1;  $|=1;
24    
# Line 50  my $dbh = DBI->connect($dsn, $user, "", Line 53  my $dbh = DBI->connect($dsn, $user, "",
53    
54  my %opt;  my %opt;
55    
56  if ( !getopts("cdm:v:i", \%opt ) ) {  if ( !getopts("cdm:v:ijf", \%opt ) ) {
57          print STDERR <<EOF;          print STDERR <<EOF;
58  usage: $0 [-c|-d] [-m num] [-v|-v level] [-i]  usage: $0 [-c|-d] [-m num] [-v|-v level] [-i|-j|-f]
59    
60  Options:  Options:
61          -c      create database on first use          -c      create database on first use
62          -d      delete database before import          -d      delete database before import
63          -m num  import just num increments for one host          -m num  import just num increments for one host
64          -v num  set verbosity (debug) level (default $debug)          -v num  set verbosity (debug) level (default $debug)
65          -i      update HyperEstraier full text index          -i      update Hyper Estraier full text index
66            -j      update full text, don't check existing files
67            -f      don't do anything with full text index
68    
69    Option -j is variation on -i. It will allow faster initial creation
70    of full-text index from existing database.
71    
72    Option -f will create database which is out of sync with full text index. You
73    will have to re-run $0 with -i to fix it.
74    
75  EOF  EOF
76          exit 1;          exit 1;
77  }  }
# Line 67  EOF Line 79  EOF
79  if ($opt{v}) {  if ($opt{v}) {
80          print "Debug level at $opt{v}\n";          print "Debug level at $opt{v}\n";
81          $debug = $opt{v};          $debug = $opt{v};
82    } elsif ($opt{f}) {
83            print "WARNING: disabling full-text index update. You need to re-run $0 -j !\n";
84            ($use_hest, $index_path, $index_node_url) = (undef, undef, undef);
85  }  }
86    
87  #---- subs ----  #---- subs ----
# Line 104  sub hest_update { Line 119  sub hest_update {
119    
120          my ($host_id, $share_id, $num) = @_;          my ($host_id, $share_id, $num) = @_;
121    
122          unless ($use_hest) {          my $skip_check = $opt{j} && print STDERR "Skipping check for existing files -- this should be used only with initital import\n";
123    
124            unless (defined($use_hest)) {
125                  print STDERR "HyperEstraier support not enabled in configuration\n";                  print STDERR "HyperEstraier support not enabled in configuration\n";
126                    $use_hest = 0;
127                  return;                  return;
128          }          }
129    
130            return unless($use_hest);
131    
132          print curr_time," updating HyperEstraier:";          print curr_time," updating HyperEstraier:";
133    
134          my $t = time();          my $t = time();
# Line 136  sub hest_update { Line 156  sub hest_update {
156    
157                  my $where = '';                  my $where = '';
158                  my @data;                  my @data;
159                  if ($host_id && $share_id && $num) {                  if (defined($host_id) && defined($share_id) && defined($num)) {
160                          $where = qq{                          $where = qq{
161                          WHERE                          WHERE
162                                  hosts.id = ? AND                                  hosts.id = ? AND
# Line 190  sub hest_update { Line 210  sub hest_update {
210                          my $fid = $row->{'fid'} || die "no fid?";                          my $fid = $row->{'fid'} || die "no fid?";
211                          my $uri = 'file:///' . $fid;                          my $uri = 'file:///' . $fid;
212    
213                          my $id = ($hest_db || $hest_node)->uri_to_id($uri);                          unless ($skip_check) {
214                          next unless ($id == -1);                                  my $id = ($hest_db || $hest_node)->uri_to_id($uri);
215                                    next unless ($id == -1);
216                            }
217    
218                          # create a document object                          # create a document object
219                          my $doc = HyperEstraier::Document->new;                          my $doc = HyperEstraier::Document->new;
# Line 200  sub hest_update { Line 222  sub hest_update {
222                          $doc->add_attr('@uri', $uri);                          $doc->add_attr('@uri', $uri);
223    
224                          foreach my $c (@{ $sth->{NAME} }) {                          foreach my $c (@{ $sth->{NAME} }) {
225                                  $doc->add_attr($c, $row->{$c}) if ($row->{$c});                                  $doc->add_attr($c, $row->{$c}) if (defined($row->{$c}));
226                          }                          }
227    
228                          #$doc->add_attr('@cdate', fmt_date($row->{'date'}));                          #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
# Line 247  sub hest_update { Line 269  sub hest_update {
269    
270    
271  ## update index ##  ## update index ##
272  if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) {  if (($opt{i} || $opt{j} || ($index_path && ! -e $TopDir . $index_path)) && !$opt{c}) {
273          # update all          # update all
274          print "force update of HyperEstraier index ";          print "force update of HyperEstraier index ";
275          print "importing existing data" unless (-e $index_path);          print "importing existing data" unless (-e $TopDir . $index_path);
276          print "by -i flag" if ($opt{i});          print "by -i flag" if ($opt{i});
277            print "by -j flag" if ($opt{j});
278          print "\n";          print "\n";
279          hest_update();          hest_update();
280  }  }
# Line 260  if (($opt{i} || ($index_path && ! -e $in Line 283  if (($opt{i} || ($index_path && ! -e $in
283  if ($opt{c}) {  if ($opt{c}) {
284          sub do_index {          sub do_index {
285                  my $index = shift || return;                  my $index = shift || return;
286                  my ($table,$col,$unique) = split(/_/, $index);                  my ($table,$col,$unique) = split(/:/, $index);
287                  $unique ||= '';                  $unique ||= '';
288                  $index =~ s/,/_/g;                  $index =~ s/\W+/_/g;
289                    print "$index on $table($col)" . ( $unique ? "u" : "" ) . " ";
290                  $dbh->do(qq{ create $unique index $index on $table($col) });                  $dbh->do(qq{ create $unique index $index on $table($col) });
291          }          }
292    
293          print "creating tables...\n";          print "creating tables...\n";
294          
295          $dbh->do(qq{          $dbh->do( qq{
296                  create table hosts (                  create table hosts (
297                          ID      SERIAL          PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
298                          name    VARCHAR(30)     NOT NULL,                          name    VARCHAR(30)     NOT NULL,
299                          IP      VARCHAR(15)                          IP      VARCHAR(15)
300                  );                              );            
301          });  
                 
         $dbh->do(qq{  
302                  create table shares (                  create table shares (
303                          ID      SERIAL          PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
304                          hostID  INTEGER         NOT NULL references hosts(id),                          hostID  INTEGER         NOT NULL references hosts(id),
305                          name    VARCHAR(30)     NOT NULL,                          name    VARCHAR(30)     NOT NULL,
306                          share   VARCHAR(200)    NOT NULL,                          share   VARCHAR(200)    NOT NULL
                         localpath VARCHAR(200)        
307                  );                              );            
         });  
308    
         $dbh->do(qq{  
309                  create table dvds (                  create table dvds (
310                          ID      SERIAL          PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
311                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
312                          name    VARCHAR(255)    NOT NULL,                          name    VARCHAR(255)    NOT NULL,
313                          mjesto  VARCHAR(255)                          mjesto  VARCHAR(255)
314                  );                  );
315          });  
           
         $dbh->do(qq{  
316                  create table backups (                  create table backups (
317                            id      serial,
318                          hostID  INTEGER         NOT NULL references hosts(id),                          hostID  INTEGER         NOT NULL references hosts(id),
319                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
320                          date    integer         NOT NULL,                          date    integer         NOT NULL,
321                          type    CHAR(4)         not null,                          type    CHAR(4)         not null,
322                          shareID integer         not null references shares(id),                          shareID integer         not null references shares(id),
323                          size    integer         not null,                          size    bigint          not null,
324                          PRIMARY KEY(hostID, num, shareID)                          inc_size bigint         not null default -1,
325                            inc_deleted boolean     default false,
326                            parts   integer         not null default 1,
327                            PRIMARY KEY(id)
328                  );                              );            
         });  
329    
         #do_index('backups_hostid,num_unique');  
   
   
         $dbh->do(qq{      
330                  create table files (                  create table files (
331                          ID      SERIAL          PRIMARY KEY,                            ID              SERIAL,
332                          shareID INTEGER         NOT NULL references shares(id),                          shareID         INTEGER NOT NULL references shares(id),
333                          backupNum  INTEGER      NOT NULL,                          backupNum       INTEGER NOT NULL,
334                          name       VARCHAR(255) NOT NULL,                          name            VARCHAR(255) NOT NULL,
335                          path       VARCHAR(255) NOT NULL,                          path            VARCHAR(255) NOT NULL,
336                          date       integer      NOT NULL,                          date            integer NOT NULL,
337                          type       INTEGER      NOT NULL,                          type            INTEGER NOT NULL,
338                          size       INTEGER      NOT NULL                          size            bigint  NOT NULL,
339                            primary key(id)
340                  );                  );
         });  
   
341    
342          $dbh->do( qq{                  create table archive (
343                  create table archive                          id              serial,
                 (  
                         id                      int not null,  
344                          dvd_nr          int not null,                          dvd_nr          int not null,
345                            total_size      bigint default -1,
346                          note            text,                          note            text,
347                          username        varchar(20) not null,                          username        varchar(20) not null,
348                          date            timestamp,                          date            timestamp default now(),
349                          primary key(id)                          primary key(id)
350                  );                        );      
         }  
         );  
351    
352          $dbh->do( qq{                  create table archive_backup (
353                  create table archive_backup                          archive_id      int not null references archive(id) on delete cascade,
354                  (                          backup_id       int not null references backups(id),
                         archive_id      int not null,  
                         backup_id       int not null,  
                         status          text,  
355                          primary key(archive_id, backup_id)                          primary key(archive_id, backup_id)
356                  );                  );
         });  
357    
358          $dbh->do( qq{                  create table archive_burned (
359                  create table workflows(                          archive_id      int references archive(id),
360                          id                      int not null,                          date            timestamp default now(),
361                          step_id         int not null,                          part            int not null default 1,
362                          start           timestamp,                          copy            int not null default 1,
363                          stop            timestamp,                          iso_size bigint default -1
                         username        varchar(20),  
                         archive_id      int not null,  
                         running         boolean default true,  
                         primary key(id)  
364                  );                  );
         });  
365    
366          $dbh->do( qq{                  create table backup_parts (
367                  create table workflow_step                          id serial,
368                  (                          backup_id int references backups(id),
369                          step_id         int not null,                          part_nr int not null check (part_nr > 0),
370                          code            text,                          tar_size bigint not null check (tar_size > 0),
371                          next_step       int,                          size bigint not null check (size > 0),
372                          stop            boolean default false,                          md5 text not null,
373                          primary key(step_id)                          items int not null check (items > 0),
374                  );                          date timestamp default now(),
375          });                          primary key(id)
   
         $dbh->do( qq{  
                         alter table workflow_step  
                                 add constraint fk_workflow_next_step  
                                 foreign key(next_step)  
                                 references workflow_step(step_id);  
         });  
   
         $dbh->do( qq{  
                 alter table workflows  
                         add constraint fk_workflows_step_id  
                         foreign key(step_id)  
                         references workflow_step(step_id);  
         });  
           
         $dbh->do( qq{  
                 alter table workflows  
                         add constraint fk_workflows_archive_id  
                         foreign key(archive_id)  
                         references archive(id);  
         });  
   
         $dbh->do( qq{  
                 create table workflow_log  
                 (  
                         workflow_id             int not null,  
                         step_id                 int not null,  
                         date                    timestamp not null,  
                         status                  text,  
                         primary key(workflow_id, step_id)  
376                  );                  );
377          });          });
378    
379          $dbh->do( qq{          print "creating indexes: ";
                 alter table workflow_log  
                         add constraint fk_workflow_log_workflow_id  
                         foreign key (workflow_id)  
                         references workflows(id);  
                 });  
           
         $dbh->do( qq{  
                 alter table workflow_log  
                         add constraint fk_workflow_log_step_id  
                         foreign key (step_id)  
                         references      workflow_step(step_id);  
                 });  
   
         print "creating indexes:";  
380    
381          foreach my $index (qw(          foreach my $index (qw(
382                  hosts_name                  hosts:name
383                  backups_hostID                  backups:hostID
384                  backups_num                  backups:num
385                  shares_hostID                  backups:shareID
386                  shares_name                  shares:hostID
387                  files_shareID                  shares:name
388                  files_path                  files:shareID
389                  files_name                  files:path
390                  files_date                  files:name
391                  files_size                  files:date
392                    files:size
393                    archive:dvd_nr
394                    archive_burned:archive_id
395                    backup_parts:backup_id,part_nr
396          )) {          )) {
                 print " $index";  
397                  do_index($index);                  do_index($index);
398          }          }
399    
400            print " creating sequence: ";
401            foreach my $seq (qw/dvd_nr/) {
402                    print "$seq ";
403                    $dbh->do( qq{ CREATE SEQUENCE $seq } );
404            }
405    
406    
407          print "...\n";          print "...\n";
408    
409          $dbh->commit;          $dbh->commit;
# Line 477  WHERE hostID=? AND num=? AND shareid=? Line 447  WHERE hostID=? AND num=? AND shareid=?
447    
448  $sth->{insert_backups} = $dbh->prepare(qq{  $sth->{insert_backups} = $dbh->prepare(qq{
449  INSERT INTO backups (hostID, num, date, type, shareid, size)  INSERT INTO backups (hostID, num, date, type, shareid, size)
450  VALUES (?,?,?,?,?,?)  VALUES (?,?,?,?,?,-1)
451    });
452    
453    $sth->{update_backups_size} = $dbh->prepare(qq{
454    UPDATE backups SET size = ?
455    WHERE hostID = ? and num = ? and date = ? and type =? and shareid = ?
456  });  });
457    
458  $sth->{insert_files} = $dbh->prepare(qq{  $sth->{insert_files} = $dbh->prepare(qq{
# Line 486  INSERT INTO files Line 461  INSERT INTO files
461          VALUES (?,?,?,?,?,?,?)          VALUES (?,?,?,?,?,?,?)
462  });  });
463    
464  foreach my $host_key (keys %{$hosts}) {  my @hosts = keys %{$hosts};
465    my $host_nr = 0;
466    
467    foreach my $host_key (@hosts) {
468    
469          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";
470    
# Line 501  foreach my $host_key (keys %{$hosts}) { Line 479  foreach my $host_key (keys %{$hosts}) {
479                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);
480          }          }
481    
482          print "host ".$hosts->{$host_key}->{'host'}.": ";          $host_nr++;
483            print "host ", $hosts->{$host_key}->{'host'}, " [",
484                    $host_nr, "/", ($#hosts + 1), "]: ";
485    
486          # get backups for a host          # get backups for a host
487          my @backups = $bpc->BackupInfoRead($hostname);          my @backups = $bpc->BackupInfoRead($hostname);
# Line 543  foreach my $host_key (keys %{$hosts}) { Line 523  foreach my $host_key (keys %{$hosts}) {
523                          # dump some log                          # dump some log
524                          print curr_time," ", $share;                          print curr_time," ", $share;
525    
                         my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);  
   
526                          $sth->{insert_backups}->execute(                          $sth->{insert_backups}->execute(
527                                  $hostID,                                  $hostID,
528                                  $backupNum,                                  $backupNum,
529                                  $backup->{'endTime'},                                  $backup->{'endTime'},
530                                  substr($backup->{'type'},0,4),                                  substr($backup->{'type'},0,4),
531                                  $shareID,                                  $shareID,
532                            );
533    
534                            my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
535    
536                            $sth->{update_backups_size}->execute(
537                                  $size,                                  $size,
538                                    $hostID,
539                                    $backupNum,
540                                    $backup->{'endTime'},
541                                    substr($backup->{'type'},0,4),
542                                    $shareID,
543                          );                          );
544    
545                          print " commit";                          print " commit";
# Line 594  sub getShareID() { Line 582  sub getShareID() {
582    
583          $sth->{insert_share} ||= $dbh->prepare(qq{          $sth->{insert_share} ||= $dbh->prepare(qq{
584                  INSERT INTO shares                  INSERT INTO shares
585                          (hostID,name,share,localpath)                          (hostID,name,share)
586                  VALUES (?,?,?,?)                  VALUES (?,?,?)
587          });          });
588    
589          my $drop_down = $hostname . '/' . $share;          my $drop_down = $hostname . '/' . $share;
590          $drop_down =~ s#//+#/#g;          $drop_down =~ s#//+#/#g;
591    
592          $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);          $sth->{insert_share}->execute($hostID,$share, $drop_down);
593          return $dbh->last_insert_id(undef,undef,'shares',undef);          return $dbh->last_insert_id(undef,undef,'shares',undef);
594  }  }
595    
# Line 618  sub found_in_db { Line 606  sub found_in_db {
606                  SELECT 1 FROM files                  SELECT 1 FROM files
607                  WHERE shareID = ? and                  WHERE shareID = ? and
608                          path = ? and                          path = ? and
609                          date = ? and                          size = ? and
610                          size = ?                          ( date = ? or date = ? or date = ? )
611                  LIMIT 1                  LIMIT 1
612          });          });
613    
614          my @param = ($shareID,$path,$date,$size);          my @param = ($shareID,$path,$size,$date, $date-$dst_offset, $date+$dst_offset);
615          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
616          my $rows = $sth->{file_in_db}->rows;          my $rows = $sth->{file_in_db}->rows;
617          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 673  sub recurseDir($$$$$$$$) { Line 661  sub recurseDir($$$$$$$$) {
661                                  $filesInBackup->{$path_key}->{'size'}                                  $filesInBackup->{$path_key}->{'size'}
662                          ));                          ));
663    
664                            my $key_dst_prev = join(" ", (
665                                    $shareID,
666                                    $dir,
667                                    $path_key,
668                                    $filesInBackup->{$path_key}->{'mtime'} - $dst_offset,
669                                    $filesInBackup->{$path_key}->{'size'}
670                            ));
671    
672                            my $key_dst_next = join(" ", (
673                                    $shareID,
674                                    $dir,
675                                    $path_key,
676                                    $filesInBackup->{$path_key}->{'mtime'} + $dst_offset,
677                                    $filesInBackup->{$path_key}->{'size'}
678                            ));
679    
680                          my $found;                          my $found;
681                          if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) {                          if (
682                                    ! defined($beenThere->{$key}) &&
683                                    ! defined($beenThere->{$key_dst_prev}) &&
684                                    ! defined($beenThere->{$key_dst_next}) &&
685                                    ! ($found = found_in_db($key, @data))
686                            ) {
687                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
688    
689                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {

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

  ViewVC Help
Powered by ViewVC 1.1.26