/[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 49 by dpavlin, Sat Aug 20 15:01:48 2005 UTC revision 82 by dpavlin, Sun Aug 28 09:12:54 2005 UTC
# Line 17  use constant BPC_FTYPE_DIR => 5; Line 17  use constant BPC_FTYPE_DIR => 5;
17  my $debug = 0;  my $debug = 0;
18  $|=1;  $|=1;
19    
20    my $start_t = time();
21    
22  my $pidfile = new File::Pid;  my $pidfile = new File::Pid;
23    
24  if (my $pid = $pidfile->running ) {  if (my $pid = $pidfile->running ) {
# Line 36  my %Conf = $bpc->Conf(); Line 38  my %Conf = $bpc->Conf();
38  my $TopDir = $bpc->TopDir();  my $TopDir = $bpc->TopDir();
39  my $beenThere = {};  my $beenThere = {};
40    
41  my $dsn = "dbi:SQLite:dbname=$TopDir/$Conf{SearchDB}";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
42  my $user = '';  my $user = $Conf{SearchUser} || '';
43    my $index_path = $Conf{HyperEstraierIndex};
 # DEBUG option!  
 ($dsn,$user) = qw/dbi:Pg:dbname=backuppc dpavlin/;  
44    
45  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
46    
47  my %opt;  my %opt;
48    
49  if ( !getopts("cdm:v:", \%opt ) ) {  if ( !getopts("cdm:v:i", \%opt ) ) {
50          print STDERR <<EOF;          print STDERR <<EOF;
51  usage: $0 [-c|-d] [-m num] [-v|-v level]  usage: $0 [-c|-d] [-m num] [-v|-v level] [-i]
52    
53  Options:  Options:
54          -c      create database on first use          -c      create database on first use
55          -d      delete database before import          -d      delete database before import
56          -m num  import just num increments for one host          -m num  import just num increments for one host
57          -v num  set verbosity (debug) level (default $debug)          -v num  set verbosity (debug) level (default $debug)
58            -i      update HyperEstraier full text index
59  EOF  EOF
60          exit 1;          exit 1;
61  }  }
62    
63    if ($opt{v}) {
64            print "Debug level at $opt{v}\n";
65            $debug = $opt{v};
66    }
67    
68    ## update index ##
69    if ($opt{i}) {
70    
71            print "updating HyperEstraier files ";
72            
73            my $sth = $dbh->prepare(qq{
74                    SELECT
75                            files.id                        AS fid,
76                            hosts.name                      AS hname,
77                            shares.name                     AS sname,
78                            shares.share                    AS sharename,
79                            files.backupNum                 AS backupNum,
80                            files.name                      AS filename,
81                            files.path                      AS filepath,
82                            files.date                      AS date,
83                            files.type                      AS filetype,
84                            files.size                      AS size,
85                            files.shareid                   AS shareid
86                    FROM files
87                            INNER JOIN shares       ON files.shareID=shares.ID
88                            INNER JOIN hosts        ON hosts.ID = shares.hostID
89                            INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID
90            });
91    
92            $sth->execute();
93    
94            my $dot = int($sth->rows / 15);
95    
96            print $sth->rows, " files ($dot/#) ";
97    
98            sub fmt_date {
99                    my $t = shift || return;
100                    my $iso = BackupPC::Lib::timeStamp($t);
101                    $iso =~ s/\s/T/;
102                    return $iso;
103            }
104    
105            my $i = 0;
106            my $max = int($sth->rows / $dot);
107    
108            $index_path = $TopDir . '/' . $index_path;
109            $index_path =~ s#//#/#g;
110    
111            print "index $index_path...";
112            use HyperEstraier;
113            my $db = HyperEstraier::Database->new();
114            $db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
115    
116    
117            while (my $row = $sth->fetchrow_hashref()) {
118    
119                    # create a document object
120                    my $doc = HyperEstraier::Document->new;
121    
122                    # add attributes to the document object
123                    $doc->add_attr('@uri', 'file:///' . $row->{'fid'});
124    
125                    foreach my $c (qw/fid hname sname sharename backupNum filename filepath shareid/) {
126                            $doc->add_attr($c, $row->{$c}) if ($row->{$c});
127                    }
128    
129                    $doc->add_attr('date', fmt_date($row->{'date'}));
130    
131                    # add the body text to the document object
132                    my $path = $row->{'filepath'};
133                    $doc->add_text($path);
134                    $path =~ s/(.)/$1 /g;
135                    $doc->add_hidden_text($path);
136    
137                    print STDERR $doc->dump_draft,"\n" if ($debug > 1);
138    
139                    # register the document object to the database
140                    $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
141    
142                    $i++;
143                    if ($i % $dot == 0) {
144                            print "$max ";
145                            $max--;
146                    }
147    
148            }
149    
150            print "sync";
151            $db->sync();
152            print " close\n";
153            $db->close();
154    
155            exit;
156    }
157    
158  ###################################create tables############################3  ###################################create tables############################3
159    
160  if ($opt{c}) {  if ($opt{c}) {
# Line 66  if ($opt{c}) { Line 162  if ($opt{c}) {
162                  my $index = shift || return;                  my $index = shift || return;
163                  my ($table,$col,$unique) = split(/_/, $index);                  my ($table,$col,$unique) = split(/_/, $index);
164                  $unique ||= '';                  $unique ||= '';
165                    $index =~ s/,/_/g;
166                  $dbh->do(qq{ create $unique index $index on $table($col) });                  $dbh->do(qq{ create $unique index $index on $table($col) });
167          }          }
168    
# Line 95  if ($opt{c}) { Line 192  if ($opt{c}) {
192                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
193                          date    integer         NOT NULL,                          date    integer         NOT NULL,
194                          type    CHAR(4)         not null,                          type    CHAR(4)         not null,
195                          PRIMARY KEY(hostID, num)                          shareID integer         not null references shares(id),
196                            size    integer         not null,
197                            PRIMARY KEY(hostID, num, shareID)
198                  );                              );            
199          });          });
200    
201          do_index('backups_num_unique');          #do_index('backups_hostid,num_unique');
202    
203          $dbh->do(qq{          $dbh->do(qq{
204                  create table dvds (                  create table dvds (
# Line 114  if ($opt{c}) { Line 213  if ($opt{c}) {
213                  create table files (                  create table files (
214                          ID      SERIAL          PRIMARY KEY,                            ID      SERIAL          PRIMARY KEY,  
215                          shareID INTEGER         NOT NULL references shares(id),                          shareID INTEGER         NOT NULL references shares(id),
216                          backupNum  INTEGER      NOT NULL references backups(num),                          backupNum  INTEGER      NOT NULL,
217                          name       VARCHAR(255) NOT NULL,                          name       VARCHAR(255) NOT NULL,
218                          path       VARCHAR(255) NOT NULL,                          path       VARCHAR(255) NOT NULL,
                         fullpath   VARCHAR(255) NOT NULL,  
219                          date       integer      NOT NULL,                          date       integer      NOT NULL,
220                          type       INTEGER      NOT NULL,                          type       INTEGER      NOT NULL,
221                          size       INTEGER      NOT NULL,                          size       INTEGER      NOT NULL,
# Line 156  if ($opt{d}) { Line 254  if ($opt{d}) {
254          }          }
255          print " done...\n";          print " done...\n";
256    
257          eval { $dbh->commit; };          $dbh->commit;
 }  
   
 if ($opt{v}) {  
         print "Debug level at $opt{v}\n";  
         $debug = $opt{v};  
258  }  }
259    
260  #################################INSERT VALUES#############################  #################################INSERT VALUES#############################
# Line 181  $sth->{hosts_by_name} = $dbh->prepare(qq Line 274  $sth->{hosts_by_name} = $dbh->prepare(qq
274  SELECT ID FROM hosts WHERE name=?  SELECT ID FROM hosts WHERE name=?
275  });  });
276    
277  $sth->{backups_broj} = $dbh->prepare(qq{  $sth->{backups_count} = $dbh->prepare(qq{
278  SELECT COUNT(*)  SELECT COUNT(*)
279  FROM backups  FROM backups
280  WHERE hostID=? AND num=?  WHERE hostID=? AND num=? AND shareid=?
281  });  });
282    
283  $sth->{insert_backups} = $dbh->prepare(qq{  $sth->{insert_backups} = $dbh->prepare(qq{
284  INSERT INTO backups (hostID, num, date, type)  INSERT INTO backups (hostID, num, date, type, shareid, size)
285  VALUES (?,?,?,?)  VALUES (?,?,?,?,?,?)
286  });  });
287    
288  $sth->{insert_files} = $dbh->prepare(qq{  $sth->{insert_files} = $dbh->prepare(qq{
289  INSERT INTO files  INSERT INTO files
290          (shareID, backupNum, name, path, fullpath, date, type, size)          (shareID, backupNum, name, path, date, type, size)
291          VALUES (?,?,?,?,?,?,?,?)          VALUES (?,?,?,?,?,?,?)
292  });  });
293    
294    sub fmt_time {
295            my $t = shift || return;
296            my $out = "";
297            my ($ss,$mm,$hh) = gmtime($t);
298            $out .= "${hh}h" if ($hh);
299            $out .= sprintf("%02d:%02d", $mm,$ss);
300            return $out;
301    }
302    
303  foreach my $host_key (keys %{$hosts}) {  foreach my $host_key (keys %{$hosts}) {
304    
305          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";
# Line 213  foreach my $host_key (keys %{$hosts}) { Line 315  foreach my $host_key (keys %{$hosts}) {
315                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);
316          }          }
317    
318          print("host ".$hosts->{$host_key}->{'host'}.": ");          print "host ".$hosts->{$host_key}->{'host'}.": ";
319    
320          # get backups for a host          # get backups for a host
321          my @backups = $bpc->BackupInfoRead($hostname);          my @backups = $bpc->BackupInfoRead($hostname);
322          print scalar @backups, " increments\n";          my $incs = scalar @backups;
323            print  "$incs increments\n";
324    
325          my $inc_nr = 0;          my $inc_nr = 0;
326            $beenThere = {};
327    
328          foreach my $backup (@backups) {          foreach my $backup (@backups) {
329    
# Line 229  foreach my $host_key (keys %{$hosts}) { Line 333  foreach my $host_key (keys %{$hosts}) {
333                  my $backupNum = $backup->{'num'};                  my $backupNum = $backup->{'num'};
334                  my @backupShares = ();                  my @backupShares = ();
335    
336                  print $hosts->{$host_key}->{'host'},                  printf("%-10s %2d/%-2d #%-2d %s %5s/%5s files (date: %s dur: %s)\n",
337                          "\t#$backupNum\t", $backup->{type} || '?', " ",                          $hosts->{$host_key}->{'host'},
338                          $backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?',                          $inc_nr, $incs, $backupNum,
339                          " files\n";                          $backup->{type} || '?',
340                            $backup->{nFilesNew} || '?', $backup->{nFiles} || '?',
341                  $sth->{backups_broj}->execute($hostID, $backupNum);                          strftime($t_fmt,localtime($backup->{startTime})),
342                  my ($broj) = $sth->{backups_broj}->fetchrow_array();                          fmt_time($backup->{endTime} - $backup->{startTime})
                 next if ($broj > 0);  
   
                 $sth->{insert_backups}->execute(  
                         $hostID,  
                         $backupNum,  
                         $backup->{'endTime'},  
                         $backup->{'type'}  
343                  );                  );
                 $dbh->commit();  
344    
345                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
346                  foreach my $share ($files->shareList($backupNum)) {                  foreach my $share ($files->shareList($backupNum)) {
347    
348                          my $t = time();                          my $t = time();
349    
                         print strftime($t_fmt,localtime())," ", $share;  
350                          $shareID = getShareID($share, $hostID, $hostname);                          $shareID = getShareID($share, $hostID, $hostname);
351                                    
352                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);                          $sth->{backups_count}->execute($hostID, $backupNum, $shareID);
353                          printf(" %d/%d files %d/%d dirs [%.2f/s]\n",                          my ($count) = $sth->{backups_count}->fetchrow_array();
354                                  $nf, $f, $nd, $d,                          # skip if allready in database!
355                                  ( ($f+$d) / ((time() - $t) || 1) )                          next if ($count > 0);
356    
357                            # dump some log
358                            print strftime($t_fmt,localtime())," ", $share;
359    
360                            my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
361    
362                            $sth->{insert_backups}->execute(
363                                    $hostID,
364                                    $backupNum,
365                                    $backup->{'endTime'},
366                                    $backup->{'type'},
367                                    $shareID,
368                                    $size,
369                          );                          );
370    
371                            print " commit";
372                          $dbh->commit();                          $dbh->commit();
373    
374                            my $dur = (time() - $t) || 1;
375                            printf(" %d/%d files %d/%d dirs %0.2f MB [%.2f/s dur: %s]\n",
376                                    $nf, $f, $nd, $d,
377                                    ($size / 1024 / 1024),
378                                    ( ($f+$d) / $dur ),
379                                    fmt_time($dur)
380                            );
381                  }                  }
382    
383          }          }
# Line 268  undef $sth; Line 386  undef $sth;
386  $dbh->commit();  $dbh->commit();
387  $dbh->disconnect();  $dbh->disconnect();
388    
389    print "total duration: ",fmt_time(time() - $start_t),"\n";
390    
391  $pidfile->remove;  $pidfile->remove;
392    
393  sub getShareID() {  sub getShareID() {
# Line 302  sub found_in_db { Line 422  sub found_in_db {
422          my @data = @_;          my @data = @_;
423          shift @data;          shift @data;
424    
425          my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_;          my ($key, $shareID,undef,$name,$path,$date,undef,$size) = @_;
426    
427          return $beenThere->{$key} if (defined($beenThere->{$key}));          return $beenThere->{$key} if (defined($beenThere->{$key}));
428    
# Line 310  sub found_in_db { Line 430  sub found_in_db {
430                  SELECT 1 FROM files                  SELECT 1 FROM files
431                  WHERE shareID = ? and                  WHERE shareID = ? and
432                          path = ? and                          path = ? and
                         name = ? and  
433                          date = ? and                          date = ? and
434                          size = ?                          size = ?
435                    LIMIT 1
436          });          });
437    
438          my @param = ($shareID,$path,$name,$date,$size);          my @param = ($shareID,$path,$date,$size);
439          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
440          my $rows = $sth->{file_in_db}->rows;          my $rows = $sth->{file_in_db}->rows;
441          print STDERR "## found_in_db ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3);          print STDERR "## found_in_db($shareID,$path,$date,$size) ",( $rows ? '+' : '-' ), join(" ",@param), "\n" if ($debug >= 3);
442    
443          $beenThere->{$key}++;          $beenThere->{$key}++;
444    
# Line 336  sub recurseDir($$$$$$$$) { Line 456  sub recurseDir($$$$$$$$) {
456    
457          print STDERR "\nrecurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1);          print STDERR "\nrecurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1);
458    
459          my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0);          my ($nr_files, $new_files, $nr_dirs, $new_dirs, $size) = (0,0,0,0,0);
460    
461          { # scope          { # scope
462                  my @stack;                  my @stack;
# Line 346  sub recurseDir($$$$$$$$) { Line 466  sub recurseDir($$$$$$$$) {
466    
467                  # first, add all the entries in current directory                  # first, add all the entries in current directory
468                  foreach my $path_key (keys %{$filesInBackup}) {                  foreach my $path_key (keys %{$filesInBackup}) {
469                            print STDERR "# file ",Dumper($filesInBackup->{$path_key}),"\n" if ($debug >= 3);
470                          my @data = (                          my @data = (
471                                  $shareID,                                  $shareID,
472                                  $backupNum,                                  $backupNum,
473                                  $path_key,                                  $path_key,
474                                  $filesInBackup->{$path_key}->{'relPath'},                                  $filesInBackup->{$path_key}->{'relPath'},
                                 $filesInBackup->{$path_key}->{'fullPath'},  
         #                       $filesInBackup->{$path_key}->{'sharePathM'},  
475                                  $filesInBackup->{$path_key}->{'mtime'},                                  $filesInBackup->{$path_key}->{'mtime'},
476                                  $filesInBackup->{$path_key}->{'type'},                                  $filesInBackup->{$path_key}->{'type'},
477                                  $filesInBackup->{$path_key}->{'size'}                                  $filesInBackup->{$path_key}->{'size'}
# Line 366  sub recurseDir($$$$$$$$) { Line 485  sub recurseDir($$$$$$$$) {
485                                  $filesInBackup->{$path_key}->{'size'}                                  $filesInBackup->{$path_key}->{'size'}
486                          ));                          ));
487    
488                            my $found;
489                          if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) {                          if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) {
490                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
491    
492                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
493                                          $new_dirs++;                                          $new_dirs++ unless ($found);
494                                          print STDERR " dir\n" if ($debug >= 2);                                          print STDERR " dir\n" if ($debug >= 2);
495                                  } else {                                  } else {
496                                          $new_files++;                                          $new_files++ unless ($found);
497                                          print STDERR " file\n" if ($debug >= 2);                                          print STDERR " file\n" if ($debug >= 2);
498                                  }                                  }
499                                    $size += $filesInBackup->{$path_key}->{'size'} || 0;
500                          }                          }
501    
502                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
# Line 401  sub recurseDir($$$$$$$$) { Line 521  sub recurseDir($$$$$$$$) {
521                  print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);                  print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);
522    
523                  while ( my $dir = shift @stack ) {                  while ( my $dir = shift @stack ) {
524                          my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID);                          my ($f,$nf,$d,$nd, $s) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID);
525                          print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1);                          print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1);
526                          $nr_files += $f;                          $nr_files += $f;
527                          $new_files += $nf;                          $new_files += $nf;
528                          $nr_dirs += $d;                          $nr_dirs += $d;
529                          $new_dirs += $nd;                          $new_dirs += $nd;
530                            $size += $s;
531                  }                  }
532          }          }
533    
534          return ($nr_files, $new_files, $nr_dirs, $new_dirs);          return ($nr_files, $new_files, $nr_dirs, $new_dirs, $size);
535  }  }
536    

Legend:
Removed from v.49  
changed lines
  Added in v.82

  ViewVC Help
Powered by ViewVC 1.1.26