/[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 329 by dpavlin, Tue Jan 31 22:04:47 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 => 4096;
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 43  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:i", \%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]  usage: $0 [-c|-d] [-m num] [-v|-v level] [-i|-j|-f]
64    
65  Options:  Options:
66          -c      create database on first use          -c      create database on first use
67          -d      delete database before import          -d      delete database before import
68          -m num  import just num increments for one host          -m num  import just num increments for one host
69          -v num  set verbosity (debug) level (default $debug)          -v num  set verbosity (debug) level (default $debug)
70          -i      update HyperEstraier full text index          -i      update Hyper Estraier full text index
71            -j      update full text, don't check existing files
72            -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
76    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 67  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            $index_node_url = undef;
91  }  }
92    
93  #---- subs ----  #---- subs ----
# Line 84  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          unless ($use_hest) {          my $skip_check = $opt{j} && print STDERR "Skipping check for existing files -- this should be used only with initital import\n";
115                  print STDERR "HyperEstraier support not enabled in configuration\n";  
116            unless ($index_node_url && $index_node_url =~ m#^http://#) {
117                    print STDERR "HyperEstraier support not enabled or index node invalid\n" if ($debug);
118                    $index_node_url = 0;
119                  return;                  return;
120          }          }
121    
122          print curr_time," updating HyperEstraier:";          print curr_time," updating Hyper Estraier:";
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";          if ($index_node_url) {
130          if ($index_path) {                  print " opening index $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";
         } else {  
                 die "don't know how to use HyperEstraier Index $use_hest";  
138          }          }
         print " increment is " . EST_CHUNK . " files:";  
139    
140          my $results = 0;          my $results = 0;
141    
# Line 136  sub hest_update { Line 143  sub hest_update {
143    
144                  my $where = '';                  my $where = '';
145                  my @data;                  my @data;
146                  if ($host_id && $share_id && $num) {                  if (defined($host_id) && defined($share_id) && defined($num)) {
147                          $where = qq{                          $where = qq{
148                          WHERE                          WHERE
149                                  hosts.id = ? AND                                  hosts.id = ? AND
# Line 175  sub hest_update { Line 182  sub hest_update {
182    
183                  if ($results == 0) {                  if ($results == 0) {
184                          print " - no new files\n";                          print " - no new files\n";
185                          last;                          return;
186                    } else {
187                            print "...";
188                  }                  }
189    
190                  sub fmt_date {                  sub fmt_date {
# Line 187  sub hest_update { Line 196  sub hest_update {
196    
197                  while (my $row = $sth->fetchrow_hashref()) {                  while (my $row = $sth->fetchrow_hashref()) {
198    
199                          my $fid = $row->{'fid'} || die "no fid?";                          my $uri = $row->{hname} . ':' . $row->{sname} . '#' . $row->{backupnum} . ' ' . $row->{filepath};
200                          my $uri = 'file:///' . $fid;                          if (! $skip_check && $hest_node) {
201                                    my $id = $hest_node->uri_to_id($uri);
202                          my $id = ($hest_db || $hest_node)->uri_to_id($uri);                                  next if ($id && $id == -1);
203                          next unless ($id == -1);                          }
204    
205                          # create a document object                          # create a document object
206                          my $doc = HyperEstraier::Document->new;                          my $doc = Search::Estraier::Document->new;
207    
208                          # add attributes to the document object                          # add attributes to the document object
209                          $doc->add_attr('@uri', $uri);                          $doc->add_attr('@uri', $uri);
210    
211                          foreach my $c (@{ $sth->{NAME} }) {                          foreach my $c (@{ $sth->{NAME} }) {
212                                  $doc->add_attr($c, $row->{$c}) if ($row->{$c});                                  print STDERR "attr $c = $row->{$c}\n" if ($debug > 2);
213                                    $doc->add_attr($c, $row->{$c}) if (defined($row->{$c}));
214                          }                          }
215    
216                          #$doc->add_attr('@cdate', fmt_date($row->{'date'}));                          #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
# Line 214  sub hest_update { Line 224  sub hest_update {
224                          print STDERR $doc->dump_draft,"\n" if ($debug > 1);                          print STDERR $doc->dump_draft,"\n" if ($debug > 1);
225    
226                          # register the document object to the database                          # register the document object to the database
227                          if ($hest_db) {                          $hest_node->put_doc($doc) if ($hest_node);
228                                  $hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);  
                         } elsif ($hest_node) {  
                                 $hest_node->put_doc($doc);  
                         } else {  
                                 die "not supported";  
                         }  
229                          $added++;                          $added++;
230                  }                  }
231    
232                  print " $added";                  print "$added";
                 $hest_db->sync() if ($index_path);  
233    
234                  $offset += EST_CHUNK;                  $offset += EST_CHUNK;
235    
236          } while ($results == EST_CHUNK);          } while ($results == EST_CHUNK);
237    
         if ($index_path) {  
                 print ", close";  
                 $hest_db->close();  
         }  
   
238          my $dur = (time() - $t) || 1;          my $dur = (time() - $t) || 1;
239          printf(" [%.2f/s dur: %s]\n",          printf(" [%.2f/s dur: %s]\n",
240                  ( $added / $dur ),                  ( $added / $dur ),
# Line 247  sub hest_update { Line 246  sub hest_update {
246    
247    
248  ## update index ##  ## update index ##
249  if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) {  if ( ( $opt{i} || $opt{j} ) && !$opt{c} ) {
250          # update all          # update all
251          print "force update of HyperEstraier index ";          print "force update of Hyper Estraier index ";
         print "importing existing data" unless (-e $index_path);  
252          print "by -i flag" if ($opt{i});          print "by -i flag" if ($opt{i});
253            print "by -j flag" if ($opt{j});
254          print "\n";          print "\n";
255          hest_update();          hest_update();
256  }  }
# Line 260  if (($opt{i} || ($index_path && ! -e $in Line 259  if (($opt{i} || ($index_path && ! -e $in
259  if ($opt{c}) {  if ($opt{c}) {
260          sub do_index {          sub do_index {
261                  my $index = shift || return;                  my $index = shift || return;
262                  my ($table,$col,$unique) = split(/_/, $index);                  my ($table,$col,$unique) = split(/:/, $index);
263                  $unique ||= '';                  $unique ||= '';
264                  $index =~ s/,/_/g;                  $index =~ s/\W+/_/g;
265                    print "$index on $table($col)" . ( $unique ? "u" : "" ) . " ";
266                  $dbh->do(qq{ create $unique index $index on $table($col) });                  $dbh->do(qq{ create $unique index $index on $table($col) });
267          }          }
268    
269          print "creating tables...\n";          print "creating tables...\n";
270          
271          $dbh->do(qq{          $dbh->do( qq{
272                  create table hosts (                  create table hosts (
273                          ID      SERIAL          PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
274                          name    VARCHAR(30)     NOT NULL,                          name    VARCHAR(30)     NOT NULL,
275                          IP      VARCHAR(15)                          IP      VARCHAR(15)
276                  );                              );            
277          });  
                 
         $dbh->do(qq{  
278                  create table shares (                  create table shares (
279                          ID      SERIAL          PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
280                          hostID  INTEGER         NOT NULL references hosts(id),                          hostID  INTEGER         NOT NULL references hosts(id),
281                          name    VARCHAR(30)     NOT NULL,                          name    VARCHAR(30)     NOT NULL,
282                          share   VARCHAR(200)    NOT NULL,                          share   VARCHAR(200)    NOT NULL
                         localpath VARCHAR(200)        
283                  );                              );            
         });  
284    
         $dbh->do(qq{  
285                  create table dvds (                  create table dvds (
286                          ID      SERIAL          PRIMARY KEY,                          ID      SERIAL          PRIMARY KEY,
287                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
288                          name    VARCHAR(255)    NOT NULL,                          name    VARCHAR(255)    NOT NULL,
289                          mjesto  VARCHAR(255)                          mjesto  VARCHAR(255)
290                  );                  );
291          });  
           
         $dbh->do(qq{  
292                  create table backups (                  create table backups (
293                            id      serial,
294                          hostID  INTEGER         NOT NULL references hosts(id),                          hostID  INTEGER         NOT NULL references hosts(id),
295                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
296                          date    integer         NOT NULL,                          date    integer         NOT NULL,
297                          type    CHAR(4)         not null,                          type    CHAR(4)         not null,
298                          shareID integer         not null references shares(id),                          shareID integer         not null references shares(id),
299                          size    integer         not null,                          size    bigint          not null,
300                          PRIMARY KEY(hostID, num, shareID)                          inc_size bigint         not null default -1,
301                            inc_deleted boolean     default false,
302                            parts   integer         not null default 0,
303                            PRIMARY KEY(id)
304                  );                              );            
         });  
   
         #do_index('backups_hostid,num_unique');  
305    
   
         $dbh->do(qq{      
306                  create table files (                  create table files (
307                          ID      SERIAL          PRIMARY KEY,                            ID              SERIAL,
308                          shareID INTEGER         NOT NULL references shares(id),                          shareID         INTEGER NOT NULL references shares(id),
309                          backupNum  INTEGER      NOT NULL,                          backupNum       INTEGER NOT NULL,
310                          name       VARCHAR(255) NOT NULL,                          name            VARCHAR(255) NOT NULL,
311                          path       VARCHAR(255) NOT NULL,                          path            VARCHAR(255) NOT NULL,
312                          date       integer      NOT NULL,                          date            integer NOT NULL,
313                          type       INTEGER      NOT NULL,                          type            INTEGER NOT NULL,
314                          size       INTEGER      NOT NULL                          size            bigint  NOT NULL,
315                            primary key(id)
316                  );                  );
         });  
317    
318                    create table archive (
319          $dbh->do( qq{                          id              serial,
                 create table archive  
                 (  
                         id                      int not null,  
320                          dvd_nr          int not null,                          dvd_nr          int not null,
321                            total_size      bigint default -1,
322                          note            text,                          note            text,
323                          username        varchar(20) not null,                          username        varchar(20) not null,
324                          date            timestamp,                          date            timestamp default now(),
325                          primary key(id)                          primary key(id)
326                  );                        );      
         }  
         );  
327    
328          $dbh->do( qq{                  create table archive_backup (
329                  create table archive_backup                          archive_id      int not null references archive(id) on delete cascade,
330                  (                          backup_id       int not null references backups(id),
                         archive_id      int not null,  
                         backup_id       int not null,  
                         status          text,  
331                          primary key(archive_id, backup_id)                          primary key(archive_id, backup_id)
332                  );                  );
         });  
333    
334          $dbh->do( qq{                  create table archive_burned (
335                  create table workflows(                          archive_id      int references archive(id),
336                          id                      int not null,                          date            timestamp default now(),
337                          step_id         int not null,                          part            int not null default 1,
338                          start           timestamp,                          copy            int not null default 1,
339                          stop            timestamp,                          iso_size bigint default -1
                         username        varchar(20),  
                         archive_id      int not null,  
                         running         boolean default true,  
                         primary key(id)  
340                  );                  );
         });  
   
         $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)  
                 );  
         });  
   
         $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);  
         });  
341    
342          $dbh->do( qq{                  create table backup_parts (
343                  create table workflow_log                          id serial,
344                  (                          backup_id int references backups(id),
345                          workflow_id             int not null,                          part_nr int not null check (part_nr > 0),
346                          step_id                 int not null,                          tar_size bigint not null check (tar_size > 0),
347                          date                    timestamp not null,                          size bigint not null check (size > 0),
348                          status                  text,                          md5 text not null,
349                          primary key(workflow_id, step_id)                          items int not null check (items > 0),
350                            date timestamp default now(),
351                            primary key(id)
352                  );                  );
353          });          });
354    
355          $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:";  
356    
357          foreach my $index (qw(          foreach my $index (qw(
358                  hosts_name                  hosts:name
359                  backups_hostID                  backups:hostID
360                  backups_num                  backups:num
361                  shares_hostID                  backups:shareID
362                  shares_name                  shares:hostID
363                  files_shareID                  shares:name
364                  files_path                  files:shareID
365                  files_name                  files:path
366                  files_date                  files:name
367                  files_size                  files:date
368                    files:size
369                    archive:dvd_nr
370                    archive_burned:archive_id
371                    backup_parts:backup_id,part_nr
372          )) {          )) {
                 print " $index";  
373                  do_index($index);                  do_index($index);
374          }          }
375    
376            print " creating sequence: ";
377            foreach my $seq (qw/dvd_nr/) {
378                    print "$seq ";
379                    $dbh->do( qq{ CREATE SEQUENCE $seq } );
380            }
381    
382            print " creating triggers ";
383            $dbh->do( <<__END_OF_TRIGGER__ );
384    
385    create or replace function backup_parts_check() returns trigger as '
386    declare
387            b_parts integer;
388            b_counted integer;
389            b_id    integer;
390    begin
391            -- raise notice ''old/new parts %/% backup_id %/%'', old.parts, new.parts, old.id, new.id;
392            if (TG_OP=''UPDATE'') then
393                    b_id := new.id;
394                    b_parts := new.parts;
395            elsif (TG_OP = ''INSERT'') then
396                    b_id := new.id;
397                    b_parts := new.parts;
398            end if;
399            b_counted := (select count(*) from backup_parts where backup_id = b_id);
400            -- raise notice ''backup % parts %'', b_id, b_parts;
401            if ( b_parts != b_counted ) then
402                    raise exception ''Update of backup % aborted, requested % parts and there are really % parts'', b_id, b_parts, b_counted;
403            end if;
404            return null;
405    end;
406    ' language plpgsql;
407    
408    create trigger do_backup_parts_check
409            after insert or update or delete on backups
410            for each row execute procedure backup_parts_check();
411    
412    create or replace function backup_backup_parts_check() returns trigger as '
413    declare
414            b_id            integer;
415            my_part_nr      integer;
416            calc_part       integer;
417    begin
418            if (TG_OP = ''INSERT'') then
419                    -- raise notice ''trigger: % backup_id %'', TG_OP, new.backup_id;
420                    b_id = new.backup_id;
421                    my_part_nr = new.part_nr;
422                    execute ''update backups set parts = parts + 1 where id = '' || b_id;
423            elsif (TG_OP = ''DELETE'') then
424                    -- raise notice ''trigger: % backup_id %'', TG_OP, old.backup_id;
425                    b_id = old.backup_id;
426                    my_part_nr = old.part_nr;
427                    execute ''update backups set parts = parts - 1 where id = '' || b_id;
428            end if;
429            calc_part := (select count(part_nr) from backup_parts where backup_id = b_id);
430            if ( my_part_nr != calc_part ) then
431                    raise exception ''Update of backup_parts with backup_id % aborted, requested part_nr is % and calulated next is %'', b_id, my_part_nr, calc_part;
432            end if;
433            return null;
434    end;
435    ' language plpgsql;
436    
437    create trigger do_backup_backup_parts_check
438            after insert or update or delete on backup_parts
439            for each row execute procedure backup_backup_parts_check();
440    
441    __END_OF_TRIGGER__
442    
443          print "...\n";          print "...\n";
444    
445          $dbh->commit;          $dbh->commit;
# Line 477  WHERE hostID=? AND num=? AND shareid=? Line 483  WHERE hostID=? AND num=? AND shareid=?
483    
484  $sth->{insert_backups} = $dbh->prepare(qq{  $sth->{insert_backups} = $dbh->prepare(qq{
485  INSERT INTO backups (hostID, num, date, type, shareid, size)  INSERT INTO backups (hostID, num, date, type, shareid, size)
486  VALUES (?,?,?,?,?,?)  VALUES (?,?,?,?,?,-1)
487    });
488    
489    $sth->{update_backups_size} = $dbh->prepare(qq{
490    UPDATE backups SET size = ?
491    WHERE hostID = ? and num = ? and date = ? and type =? and shareid = ?
492  });  });
493    
494  $sth->{insert_files} = $dbh->prepare(qq{  $sth->{insert_files} = $dbh->prepare(qq{
# Line 486  INSERT INTO files Line 497  INSERT INTO files
497          VALUES (?,?,?,?,?,?,?)          VALUES (?,?,?,?,?,?,?)
498  });  });
499    
500  foreach my $host_key (keys %{$hosts}) {  my @hosts = keys %{$hosts};
501    my $host_nr = 0;
502    
503    foreach my $host_key (@hosts) {
504    
505          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";
506    
# Line 501  foreach my $host_key (keys %{$hosts}) { Line 515  foreach my $host_key (keys %{$hosts}) {
515                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);
516          }          }
517    
518          print "host ".$hosts->{$host_key}->{'host'}.": ";          $host_nr++;
   
519          # get backups for a host          # get backups for a host
520          my @backups = $bpc->BackupInfoRead($hostname);          my @backups = $bpc->BackupInfoRead($hostname);
521          my $incs = scalar @backups;          my $incs = scalar @backups;
         print  "$incs increments\n";  
522    
523            my $host_header = sprintf("host %s [%d/%d]: %d increments\n",
524                    $hosts->{$host_key}->{'host'},
525                    $host_nr,
526                    ($#hosts + 1),
527                    $incs
528            );
529            print $host_header unless ($opt{q});
530    
531          my $inc_nr = 0;          my $inc_nr = 0;
532          $beenThere = {};          $beenThere = {};
533    
# Line 519  foreach my $host_key (keys %{$hosts}) { Line 539  foreach my $host_key (keys %{$hosts}) {
539                  my $backupNum = $backup->{'num'};                  my $backupNum = $backup->{'num'};
540                  my @backupShares = ();                  my @backupShares = ();
541    
542                  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",
543                          $hosts->{$host_key}->{'host'},                          $hosts->{$host_key}->{'host'},
544                          $inc_nr, $incs, $backupNum,                          $inc_nr, $incs, $backupNum,
545                          $backup->{type} || '?',                          $backup->{type} || '?',
# Line 527  foreach my $host_key (keys %{$hosts}) { Line 547  foreach my $host_key (keys %{$hosts}) {
547                          strftime($t_fmt,localtime($backup->{startTime})),                          strftime($t_fmt,localtime($backup->{startTime})),
548                          fmt_time($backup->{endTime} - $backup->{startTime})                          fmt_time($backup->{endTime} - $backup->{startTime})
549                  );                  );
550                    print $share_header unless ($opt{q});
551    
552                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
553                  foreach my $share ($files->shareList($backupNum)) {                  foreach my $share ($files->shareList($backupNum)) {
# Line 540  foreach my $host_key (keys %{$hosts}) { Line 561  foreach my $host_key (keys %{$hosts}) {
561                          # skip if allready in database!                          # skip if allready in database!
562                          next if ($count > 0);                          next if ($count > 0);
563    
564                            # dump host and share header for -q
565                            if ($opt{q}) {
566                                    if ($host_header) {
567                                            print $host_header;
568                                            $host_header = undef;
569                                    }
570                                    print $share_header;
571                            }
572    
573                          # dump some log                          # dump some log
574                          print curr_time," ", $share;                          print curr_time," ", $share;
575    
                         my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);  
   
576                          $sth->{insert_backups}->execute(                          $sth->{insert_backups}->execute(
577                                  $hostID,                                  $hostID,
578                                  $backupNum,                                  $backupNum,
579                                  $backup->{'endTime'},                                  $backup->{'endTime'},
580                                  substr($backup->{'type'},0,4),                                  substr($backup->{'type'},0,4),
581                                  $shareID,                                  $shareID,
                                 $size,  
582                          );                          );
583    
584                          print " commit";                          my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
585                          $dbh->commit();  
586                            eval {
587                                    $sth->{update_backups_size}->execute(
588                                            $size,
589                                            $hostID,
590                                            $backupNum,
591                                            $backup->{'endTime'},
592                                            substr($backup->{'type'},0,4),
593                                            $shareID,
594                                    );
595                                    print " commit";
596                                    $dbh->commit();
597                            };
598                            if ($@) {
599                                    print " rollback";
600                                    $dbh->rollback();
601                            }
602    
603                          my $dur = (time() - $t) || 1;                          my $dur = (time() - $t) || 1;
604                          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 594  sub getShareID() { Line 637  sub getShareID() {
637    
638          $sth->{insert_share} ||= $dbh->prepare(qq{          $sth->{insert_share} ||= $dbh->prepare(qq{
639                  INSERT INTO shares                  INSERT INTO shares
640                          (hostID,name,share,localpath)                          (hostID,name,share)
641                  VALUES (?,?,?,?)                  VALUES (?,?,?)
642          });          });
643    
644          my $drop_down = $hostname . '/' . $share;          my $drop_down = $hostname . '/' . $share;
645          $drop_down =~ s#//+#/#g;          $drop_down =~ s#//+#/#g;
646    
647          $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);          $sth->{insert_share}->execute($hostID,$share, $drop_down);
648          return $dbh->last_insert_id(undef,undef,'shares',undef);          return $dbh->last_insert_id(undef,undef,'shares',undef);
649  }  }
650    
# Line 618  sub found_in_db { Line 661  sub found_in_db {
661                  SELECT 1 FROM files                  SELECT 1 FROM files
662                  WHERE shareID = ? and                  WHERE shareID = ? and
663                          path = ? and                          path = ? and
664                          date = ? and                          size = ? and
665                          size = ?                          ( date = ? or date = ? or date = ? )
666                  LIMIT 1                  LIMIT 1
667          });          });
668    
669          my @param = ($shareID,$path,$date,$size);          my @param = ($shareID,$path,$size,$date, $date-$dst_offset, $date+$dst_offset);
670          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
671          my $rows = $sth->{file_in_db}->rows;          my $rows = $sth->{file_in_db}->rows;
672          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 716  sub recurseDir($$$$$$$$) {
716                                  $filesInBackup->{$path_key}->{'size'}                                  $filesInBackup->{$path_key}->{'size'}
717                          ));                          ));
718    
719                            my $key_dst_prev = join(" ", (
720                                    $shareID,
721                                    $dir,
722                                    $path_key,
723                                    $filesInBackup->{$path_key}->{'mtime'} - $dst_offset,
724                                    $filesInBackup->{$path_key}->{'size'}
725                            ));
726    
727                            my $key_dst_next = join(" ", (
728                                    $shareID,
729                                    $dir,
730                                    $path_key,
731                                    $filesInBackup->{$path_key}->{'mtime'} + $dst_offset,
732                                    $filesInBackup->{$path_key}->{'size'}
733                            ));
734    
735                          my $found;                          my $found;
736                          if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) {                          if (
737                                    ! defined($beenThere->{$key}) &&
738                                    ! defined($beenThere->{$key_dst_prev}) &&
739                                    ! defined($beenThere->{$key_dst_next}) &&
740                                    ! ($found = found_in_db($key, @data))
741                            ) {
742                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
743    
744                                  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.329

  ViewVC Help
Powered by ViewVC 1.1.26