/[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 66 by dpavlin, Mon Aug 22 00:09:59 2005 UTC revision 81 by dpavlin, Sun Aug 28 08:40:06 2005 UTC
# Line 45  my $dbh = DBI->connect($dsn, $user, "", Line 45  my $dbh = DBI->connect($dsn, $user, "",
45    
46  my %opt;  my %opt;
47    
48  if ( !getopts("cdm:v:", \%opt ) ) {  if ( !getopts("cdm:v:i", \%opt ) ) {
49          print STDERR <<EOF;          print STDERR <<EOF;
50  usage: $0 [-c|-d] [-m num] [-v|-v level]  usage: $0 [-c|-d] [-m num] [-v|-v level] [-i]
51    
52  Options:  Options:
53          -c      create database on first use          -c      create database on first use
54          -d      delete database before import          -d      delete database before import
55          -m num  import just num increments for one host          -m num  import just num increments for one host
56          -v num  set verbosity (debug) level (default $debug)          -v num  set verbosity (debug) level (default $debug)
57            -i      update HyperEstraier full text index
58  EOF  EOF
59          exit 1;          exit 1;
60  }  }
61    
62    if ($opt{v}) {
63            print "Debug level at $opt{v}\n";
64            $debug = $opt{v};
65    }
66    
67    ## update index ##
68    if ($opt{i}) {
69    
70            my $index_dir = '/var/tmp/casket';
71    
72            print "updating HyperEstraier index $index_dir...";
73    
74            use HyperEstraier;
75            my $db = HyperEstraier::Database->new();
76            $db->open($index_dir, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
77    
78            my $sth = $dbh->prepare(qq{
79                    SELECT
80                            files.id                        AS fid,
81                            hosts.name                      AS hname,
82                            shares.name                     AS sname,
83                            shares.share                    AS sharename,
84                            files.backupNum                 AS backupNum,
85                            files.name                      AS filename,
86                            files.path                      AS filepath,
87                            files.date                      AS date,
88                            files.type                      AS filetype,
89                            files.size                      AS size,
90                            files.shareid                   AS shareid
91                    FROM files
92                            INNER JOIN shares       ON files.shareID=shares.ID
93                            INNER JOIN hosts        ON hosts.ID = shares.hostID
94                            INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID
95            });
96    
97            $sth->execute();
98    
99            my $dot = int($sth->rows / 15);
100    
101            print $sth->rows, " files ($dot/#) ";
102    
103            sub fmt_date {
104                    my $t = shift || return;
105                    my $iso = BackupPC::Lib::timeStamp($t);
106                    $iso =~ s/\s/T/;
107                    return $iso;
108            }
109    
110            my $i = 0;
111            my $max = int($sth->rows / $dot);
112    
113            while (my $row = $sth->fetchrow_hashref()) {
114    
115                    # create a document object
116                    my $doc = HyperEstraier::Document->new;
117    
118                    # add attributes to the document object
119                    $doc->add_attr('@uri', 'file:///' . $row->{'fid'});
120    
121                    foreach my $c (qw/fid hname sname sharename backupNum filename filepath shareid/) {
122                            $doc->add_attr($c, $row->{$c}) if ($row->{$c});
123                    }
124    
125                    $doc->add_attr('date', fmt_date($row->{'date'}));
126    
127                    # add the body text to the document object
128                    my $path = $row->{'filepath'};
129                    $doc->add_text($path);
130                    $path =~ s/(.)/$1 /g;
131                    $doc->add_hidden_text($path);
132    
133                    print STDERR $doc->dump_draft,"\n" if ($debug > 1);
134    
135                    # register the document object to the database
136                    $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
137    
138                    $i++;
139                    if ($i % $dot == 0) {
140                            print "$max ";
141                            $max--;
142                    }
143    
144            }
145    
146            print "sync";
147            $db->sync();
148            print " close\n";
149            $db->close();
150    
151            exit;
152    }
153    
154  ###################################create tables############################3  ###################################create tables############################3
155    
156  if ($opt{c}) {  if ($opt{c}) {
# Line 160  if ($opt{d}) { Line 253  if ($opt{d}) {
253          $dbh->commit;          $dbh->commit;
254  }  }
255    
 if ($opt{v}) {  
         print "Debug level at $opt{v}\n";  
         $debug = $opt{v};  
 }  
   
256  #################################INSERT VALUES#############################  #################################INSERT VALUES#############################
257    
258  # get hosts  # get hosts
# Line 223  foreach my $host_key (keys %{$hosts}) { Line 311  foreach my $host_key (keys %{$hosts}) {
311                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);
312          }          }
313    
314          print("host ".$hosts->{$host_key}->{'host'}.": ");          print "host ".$hosts->{$host_key}->{'host'}.": ";
315    
316          # get backups for a host          # get backups for a host
317          my @backups = $bpc->BackupInfoRead($hostname);          my @backups = $bpc->BackupInfoRead($hostname);
318          print scalar @backups, " increments\n";          my $incs = scalar @backups;
319            print  "$incs increments\n";
320    
321          my $inc_nr = 0;          my $inc_nr = 0;
322            $beenThere = {};
323    
324          foreach my $backup (@backups) {          foreach my $backup (@backups) {
325    
# Line 239  foreach my $host_key (keys %{$hosts}) { Line 329  foreach my $host_key (keys %{$hosts}) {
329                  my $backupNum = $backup->{'num'};                  my $backupNum = $backup->{'num'};
330                  my @backupShares = ();                  my @backupShares = ();
331    
332                  print $hosts->{$host_key}->{'host'},                  printf("%-10s %2d/%-2d #%-2d %s %5s/%5s files (date: %s dur: %s)\n",
333                          "\t#$backupNum\t", $backup->{type} || '?', " ",                          $hosts->{$host_key}->{'host'},
334                          $backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?',                          $inc_nr, $incs, $backupNum,
335                          " files (date: ",                          $backup->{type} || '?',
336                            $backup->{nFilesNew} || '?', $backup->{nFiles} || '?',
337                          strftime($t_fmt,localtime($backup->{startTime})),                          strftime($t_fmt,localtime($backup->{startTime})),
338                          " dur: ",                          fmt_time($backup->{endTime} - $backup->{startTime})
339                          fmt_time($backup->{endTime} - $backup->{startTime}),                  );
                         ")\n";  
340    
341                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
342                  foreach my $share ($files->shareList($backupNum)) {                  foreach my $share ($files->shareList($backupNum)) {
# Line 328  sub found_in_db { Line 418  sub found_in_db {
418          my @data = @_;          my @data = @_;
419          shift @data;          shift @data;
420    
421          my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_;          my ($key, $shareID,undef,$name,$path,$date,undef,$size) = @_;
422    
423          return $beenThere->{$key} if (defined($beenThere->{$key}));          return $beenThere->{$key} if (defined($beenThere->{$key}));
424    
# Line 336  sub found_in_db { Line 426  sub found_in_db {
426                  SELECT 1 FROM files                  SELECT 1 FROM files
427                  WHERE shareID = ? and                  WHERE shareID = ? and
428                          path = ? and                          path = ? and
                         name = ? and  
429                          date = ? and                          date = ? and
430                          size = ?                          size = ?
431                    LIMIT 1
432          });          });
433    
434          my @param = ($shareID,$path,$name,$date,$size);          my @param = ($shareID,$path,$date,$size);
435          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
436          my $rows = $sth->{file_in_db}->rows;          my $rows = $sth->{file_in_db}->rows;
437          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);
438    
439          $beenThere->{$key}++;          $beenThere->{$key}++;
440    
# Line 391  sub recurseDir($$$$$$$$) { Line 481  sub recurseDir($$$$$$$$) {
481                                  $filesInBackup->{$path_key}->{'size'}                                  $filesInBackup->{$path_key}->{'size'}
482                          ));                          ));
483    
484                            my $found;
485                          if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) {                          if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) {
486                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
487    
488                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
489                                          $new_dirs++;                                          $new_dirs++ unless ($found);
490                                          print STDERR " dir\n" if ($debug >= 2);                                          print STDERR " dir\n" if ($debug >= 2);
491                                  } else {                                  } else {
492                                          $new_files++;                                          $new_files++ unless ($found);
493                                          print STDERR " file\n" if ($debug >= 2);                                          print STDERR " file\n" if ($debug >= 2);
494                                  }                                  }
495                                  $size += $filesInBackup->{$path_key}->{'size'} || 0;                                  $size += $filesInBackup->{$path_key}->{'size'} || 0;

Legend:
Removed from v.66  
changed lines
  Added in v.81

  ViewVC Help
Powered by ViewVC 1.1.26