/[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 131 by dpavlin, Fri Sep 23 08:54:11 2005 UTC revision 246 by dpavlin, Fri Dec 9 12:49:55 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:ij", \%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
# Line 61  Options: Line 64  Options:
64          -v num  set verbosity (debug) level (default $debug)          -v num  set verbosity (debug) level (default $debug)
65          -i      update Hyper Estraier full text index          -i      update Hyper Estraier full text index
66          -j      update full text, don't check existing files          -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  Option -j is variation on -i. It will allow faster initial creation
70  of full-text index from existing database.  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 72  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 143  sub hest_update { Line 153  sub hest_update {
153    
154                  my $where = '';                  my $where = '';
155                  my @data;                  my @data;
156                  if ($host_id && $share_id && $num) {                  if (defined($host_id) && defined($share_id) && defined($num)) {
157                          $where = qq{                          $where = qq{
158                          WHERE                          WHERE
159                                  hosts.id = ? AND                                  hosts.id = ? AND
# Line 209  sub hest_update { Line 219  sub hest_update {
219                          $doc->add_attr('@uri', $uri);                          $doc->add_attr('@uri', $uri);
220    
221                          foreach my $c (@{ $sth->{NAME} }) {                          foreach my $c (@{ $sth->{NAME} }) {
222                                  $doc->add_attr($c, $row->{$c}) if ($row->{$c});                                  $doc->add_attr($c, $row->{$c}) if (defined($row->{$c}));
223                          }                          }
224    
225                          #$doc->add_attr('@cdate', fmt_date($row->{'date'}));                          #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
# Line 256  sub hest_update { Line 266  sub hest_update {
266    
267    
268  ## update index ##  ## update index ##
269  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}) {
270          # update all          # update all
271          print "force update of HyperEstraier index ";          print "force update of HyperEstraier index ";
272          print "importing existing data" unless (-e $index_path);          print "importing existing data" unless (-e $TopDir . $index_path);
273          print "by -i flag" if ($opt{i});          print "by -i flag" if ($opt{i});
274          print "by -j flag" if ($opt{j});          print "by -j flag" if ($opt{j});
275          print "\n";          print "\n";
# Line 270  if (($opt{i} || $opt{j} || ($index_path Line 280  if (($opt{i} || $opt{j} || ($index_path
280  if ($opt{c}) {  if ($opt{c}) {
281          sub do_index {          sub do_index {
282                  my $index = shift || return;                  my $index = shift || return;
283                  my ($table,$col,$unique) = split(/_/, $index);                  my ($table,$col,$unique) = split(/:/, $index);
284                  $unique ||= '';                  $unique ||= '';
285                  $index =~ s/,/_/g;                  $index =~ s/\W+/_/g;
286                    print "$index on $table($col)" . ( $unique ? "u" : "" ) . " ";
287                  $dbh->do(qq{ create $unique index $index on $table($col) });                  $dbh->do(qq{ create $unique index $index on $table($col) });
288          }          }
289    
290          print "creating tables...\n";          print "creating tables...\n";
291          
292          $dbh->do(qq{          $dbh->do( qq{
293                  create table hosts (                  create table hosts (
294                          ID      SERIAL          PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
295                          name    VARCHAR(30)     NOT NULL,                          name    VARCHAR(30)     NOT NULL,
296                          IP      VARCHAR(15)                          IP      VARCHAR(15)
297                  );                              );            
298          });  
                 
         $dbh->do(qq{  
299                  create table shares (                  create table shares (
300                          ID      SERIAL          PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
301                          hostID  INTEGER         NOT NULL references hosts(id),                          hostID  INTEGER         NOT NULL references hosts(id),
302                          name    VARCHAR(30)     NOT NULL,                          name    VARCHAR(30)     NOT NULL,
303                          share   VARCHAR(200)    NOT NULL,                          share   VARCHAR(200)    NOT NULL
                         localpath VARCHAR(200)        
304                  );                              );            
         });  
305    
         $dbh->do(qq{  
306                  create table dvds (                  create table dvds (
307                          ID      SERIAL          PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
308                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
309                          name    VARCHAR(255)    NOT NULL,                          name    VARCHAR(255)    NOT NULL,
310                          mjesto  VARCHAR(255)                          mjesto  VARCHAR(255)
311                  );                  );
312          });  
           
         $dbh->do(qq{  
313                  create table backups (                  create table backups (
314                            id      serial,
315                          hostID  INTEGER         NOT NULL references hosts(id),                          hostID  INTEGER         NOT NULL references hosts(id),
316                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
317                          date    integer         NOT NULL,                          date    integer         NOT NULL,
318                          type    CHAR(4)         not null,                          type    CHAR(4)         not null,
319                          shareID integer         not null references shares(id),                          shareID integer         not null references shares(id),
320                          size    integer         not null,                          size    bigint          not null,
321                          PRIMARY KEY(hostID, num, shareID)                          inc_size bigint         not null default -1,
322                            inc_deleted boolean     default false,
323                            parts   integer         not null default 1,
324                            PRIMARY KEY(id)
325                  );                              );            
         });  
   
         #do_index('backups_hostid,num_unique');  
   
326    
         $dbh->do(qq{      
327                  create table files (                  create table files (
328                          ID      SERIAL          PRIMARY KEY,                            ID              SERIAL,
329                          shareID INTEGER         NOT NULL references shares(id),                          shareID         INTEGER NOT NULL references shares(id),
330                          backupNum  INTEGER      NOT NULL,                          backupNum       INTEGER NOT NULL,
331                          name       VARCHAR(255) NOT NULL,                          name            VARCHAR(255) NOT NULL,
332                          path       VARCHAR(255) NOT NULL,                          path            VARCHAR(255) NOT NULL,
333                          date       integer      NOT NULL,                          date            integer NOT NULL,
334                          type       INTEGER      NOT NULL,                          type            INTEGER NOT NULL,
335                          size       INTEGER      NOT NULL                          size            bigint  NOT NULL,
336                            primary key(id)
337                  );                  );
         });  
338    
339                    create table archive (
340          $dbh->do( qq{                          id              serial,
                 create table archive  
                 (  
                         id                      int not null,  
341                          dvd_nr          int not null,                          dvd_nr          int not null,
342                            total_size      bigint default -1,
343                          note            text,                          note            text,
344                          username        varchar(20) not null,                          username        varchar(20) not null,
345                          date            timestamp,                          date            timestamp default now(),
346                          primary key(id)                          primary key(id)
347                  );                        );      
         }  
         );  
348    
349          $dbh->do( qq{                  create table archive_backup (
350                  create table archive_backup                          archive_id      int not null references archive(id) on delete cascade,
351                  (                          backup_id       int not null references backups(id),
                         archive_id      int not null,  
                         backup_id       int not null,  
                         status          text,  
352                          primary key(archive_id, backup_id)                          primary key(archive_id, backup_id)
353                  );                  );
         });  
354    
355          $dbh->do( qq{                  create table archive_burned (
356                  create table workflows(                          archive_id      int references archive(id),
357                          id                      int not null,                          date            timestamp default now(),
358                          step_id         int not null,                          part            int not null default 1,
359                          start           timestamp,                          copy            int not null default 1,
360                          stop            timestamp,                          iso_size bigint default -1
                         username        varchar(20),  
                         archive_id      int not null,  
                         running         boolean default true,  
                         primary key(id)  
361                  );                  );
         });  
   
         $dbh->do( qq{  
                 create table workflow_step  
                 (  
                         step_id         int not null,  
                         code            text,  
                         next_step       int,  
                         stop            boolean default false,  
                         primary key(step_id)  
                 );  
         });  
362    
363          $dbh->do( qq{                  create table backup_parts (
364                          alter table workflow_step                          id serial,
365                                  add constraint fk_workflow_next_step                          backup_id int references backups(id),
366                                  foreign key(next_step)                          part_nr int not null check (part_nr > 0),
367                                  references workflow_step(step_id);                          tar_size bigint not null check (tar_size > 0),
368          });                          size bigint not null check (size > 0),
369                            md5 text not null,
370          $dbh->do( qq{                          items int not null check (items > 0),
371                  alter table workflows                          date timestamp default now(),
372                          add constraint fk_workflows_step_id                          primary key(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)  
373                  );                  );
374          });          });
375    
376          $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:";  
377    
378          foreach my $index (qw(          foreach my $index (qw(
379                  hosts_name                  hosts:name
380                  backups_hostID                  backups:hostID
381                  backups_num                  backups:num
382                  shares_hostID                  backups:shareID
383                  shares_name                  shares:hostID
384                  files_shareID                  shares:name
385                  files_path                  files:shareID
386                  files_name                  files:path
387                  files_date                  files:name
388                  files_size                  files:date
389                    files:size
390                    archive:dvd_nr
391                    archive_burned:archive_id
392                    backup_parts:backup_id,part_nr
393          )) {          )) {
                 print " $index";  
394                  do_index($index);                  do_index($index);
395          }          }
396    
397            print " creating sequence: ";
398            foreach my $seq (qw/dvd_nr/) {
399                    print "$seq ";
400                    $dbh->do( qq{ CREATE SEQUENCE $seq } );
401            }
402    
403    
404          print "...\n";          print "...\n";
405    
406          $dbh->commit;          $dbh->commit;
# Line 487  WHERE hostID=? AND num=? AND shareid=? Line 444  WHERE hostID=? AND num=? AND shareid=?
444    
445  $sth->{insert_backups} = $dbh->prepare(qq{  $sth->{insert_backups} = $dbh->prepare(qq{
446  INSERT INTO backups (hostID, num, date, type, shareid, size)  INSERT INTO backups (hostID, num, date, type, shareid, size)
447  VALUES (?,?,?,?,?,?)  VALUES (?,?,?,?,?,-1)
448    });
449    
450    $sth->{update_backups_size} = $dbh->prepare(qq{
451    UPDATE backups SET size = ?
452    WHERE hostID = ? and num = ? and date = ? and type =? and shareid = ?
453  });  });
454    
455  $sth->{insert_files} = $dbh->prepare(qq{  $sth->{insert_files} = $dbh->prepare(qq{
# Line 553  foreach my $host_key (keys %{$hosts}) { Line 515  foreach my $host_key (keys %{$hosts}) {
515                          # dump some log                          # dump some log
516                          print curr_time," ", $share;                          print curr_time," ", $share;
517    
                         my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);  
   
518                          $sth->{insert_backups}->execute(                          $sth->{insert_backups}->execute(
519                                  $hostID,                                  $hostID,
520                                  $backupNum,                                  $backupNum,
521                                  $backup->{'endTime'},                                  $backup->{'endTime'},
522                                  substr($backup->{'type'},0,4),                                  substr($backup->{'type'},0,4),
523                                  $shareID,                                  $shareID,
524                            );
525    
526                            my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
527    
528                            $sth->{update_backups_size}->execute(
529                                  $size,                                  $size,
530                                    $hostID,
531                                    $backupNum,
532                                    $backup->{'endTime'},
533                                    substr($backup->{'type'},0,4),
534                                    $shareID,
535                          );                          );
536    
537                          print " commit";                          print " commit";
# Line 604  sub getShareID() { Line 574  sub getShareID() {
574    
575          $sth->{insert_share} ||= $dbh->prepare(qq{          $sth->{insert_share} ||= $dbh->prepare(qq{
576                  INSERT INTO shares                  INSERT INTO shares
577                          (hostID,name,share,localpath)                          (hostID,name,share)
578                  VALUES (?,?,?,?)                  VALUES (?,?,?)
579          });          });
580    
581          my $drop_down = $hostname . '/' . $share;          my $drop_down = $hostname . '/' . $share;
582          $drop_down =~ s#//+#/#g;          $drop_down =~ s#//+#/#g;
583    
584          $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);          $sth->{insert_share}->execute($hostID,$share, $drop_down);
585          return $dbh->last_insert_id(undef,undef,'shares',undef);          return $dbh->last_insert_id(undef,undef,'shares',undef);
586  }  }
587    
# Line 628  sub found_in_db { Line 598  sub found_in_db {
598                  SELECT 1 FROM files                  SELECT 1 FROM files
599                  WHERE shareID = ? and                  WHERE shareID = ? and
600                          path = ? and                          path = ? and
601                          date = ? and                          size = ? and
602                          size = ?                          ( date = ? or date = ? or date = ? )
603                  LIMIT 1                  LIMIT 1
604          });          });
605    
606          my @param = ($shareID,$path,$date,$size);          my @param = ($shareID,$path,$size,$date, $date-$dst_offset, $date+$dst_offset);
607          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
608          my $rows = $sth->{file_in_db}->rows;          my $rows = $sth->{file_in_db}->rows;
609          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 683  sub recurseDir($$$$$$$$) { Line 653  sub recurseDir($$$$$$$$) {
653                                  $filesInBackup->{$path_key}->{'size'}                                  $filesInBackup->{$path_key}->{'size'}
654                          ));                          ));
655    
656                            my $key_dst_prev = join(" ", (
657                                    $shareID,
658                                    $dir,
659                                    $path_key,
660                                    $filesInBackup->{$path_key}->{'mtime'} - $dst_offset,
661                                    $filesInBackup->{$path_key}->{'size'}
662                            ));
663    
664                            my $key_dst_next = join(" ", (
665                                    $shareID,
666                                    $dir,
667                                    $path_key,
668                                    $filesInBackup->{$path_key}->{'mtime'} + $dst_offset,
669                                    $filesInBackup->{$path_key}->{'size'}
670                            ));
671    
672                          my $found;                          my $found;
673                          if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) {                          if (
674                                    ! defined($beenThere->{$key}) &&
675                                    ! defined($beenThere->{$key_dst_prev}) &&
676                                    ! defined($beenThere->{$key_dst_next}) &&
677                                    ! ($found = found_in_db($key, @data))
678                            ) {
679                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
680    
681                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {

Legend:
Removed from v.131  
changed lines
  Added in v.246

  ViewVC Help
Powered by ViewVC 1.1.26