/[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 67 by dpavlin, Mon Aug 22 08:58:59 2005 UTC revision 117 by dpavlin, Sun Sep 11 13:05:06 2005 UTC
# Line 13  use File::Pid; Line 13  use File::Pid;
13  use POSIX qw/strftime/;  use POSIX qw/strftime/;
14    
15  use constant BPC_FTYPE_DIR => 5;  use constant BPC_FTYPE_DIR => 5;
16    use constant EST_CHUNK => 100000;
17    
18  my $debug = 0;  my $debug = 0;
19  $|=1;  $|=1;
# Line 41  my $beenThere = {}; Line 42  my $beenThere = {};
42  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";  my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
43  my $user = $Conf{SearchUser} || '';  my $user = $Conf{SearchUser} || '';
44    
45    my $use_hest = $Conf{HyperEstraierIndex};
46    my ($index_path, $index_node_url) = getHyperEstraier_url($use_hest);
47    
48  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });  my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 0 });
49    
50  my %opt;  my %opt;
51    
52  if ( !getopts("cdm:v:", \%opt ) ) {  if ( !getopts("cdm:v:i", \%opt ) ) {
53          print STDERR <<EOF;          print STDERR <<EOF;
54  usage: $0 [-c|-d] [-m num] [-v|-v level]  usage: $0 [-c|-d] [-m num] [-v|-v level] [-i]
55    
56  Options:  Options:
57          -c      create database on first use          -c      create database on first use
58          -d      delete database before import          -d      delete database before import
59          -m num  import just num increments for one host          -m num  import just num increments for one host
60          -v num  set verbosity (debug) level (default $debug)          -v num  set verbosity (debug) level (default $debug)
61            -i      update HyperEstraier full text index
62  EOF  EOF
63          exit 1;          exit 1;
64  }  }
65    
66  ###################################create tables############################3  if ($opt{v}) {
67            print "Debug level at $opt{v}\n";
68            $debug = $opt{v};
69    }
70    
71    #---- subs ----
72    
73    sub fmt_time {
74            my $t = shift || return;
75            my $out = "";
76            my ($ss,$mm,$hh) = gmtime($t);
77            $out .= "${hh}h" if ($hh);
78            $out .= sprintf("%02d:%02d", $mm,$ss);
79            return $out;
80    }
81    
82    sub curr_time {
83            return strftime($t_fmt,localtime());
84    }
85    
86    my $hest_db;
87    my $hest_node;
88    
89    sub signal {
90            my($sig) = @_;
91            if ($hest_db) {
92                    print "\nCaught a SIG$sig--syncing database and shutting down\n";
93                    $hest_db->sync();
94                    $hest_db->close();
95            }
96            exit(0);
97    }
98    
99    $SIG{'INT'}  = \&signal;
100    $SIG{'QUIT'} = \&signal;
101    
102    sub hest_update {
103    
104            my ($host_id, $share_id, $num) = @_;
105    
106            unless ($use_hest) {
107                    print STDERR "HyperEstraier support not enabled in configuration\n";
108                    return;
109            }
110    
111            print curr_time," updating HyperEstraier:";
112    
113            my $t = time();
114    
115            my $offset = 0;
116            my $added = 0;
117    
118            print " opening index $use_hest";
119            if ($index_path) {
120                    $hest_db = HyperEstraier::Database->new();
121                    $hest_db->open($index_path, $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
122                    print " directly";
123            } elsif ($index_node_url) {
124                    $hest_node ||= HyperEstraier::Node->new($index_node_url);
125                    $hest_node->set_auth('admin', 'admin');
126                    print " via node URL";
127            } else {
128                    die "don't know how to use HyperEstraier Index $use_hest";
129            }
130            print " increment is " . EST_CHUNK . " files:";
131    
132            my $results = 0;
133    
134            do {
135    
136                    my $where = '';
137                    my @data;
138                    if ($host_id && $share_id && $num) {
139                            $where = qq{
140                            WHERE
141                                    hosts.id = ? AND
142                                    shares.id = ? AND
143                                    files.backupnum = ?
144                            };
145                            @data = ( $host_id, $share_id, $num );
146                    }
147    
148                    my $limit = sprintf('LIMIT '.EST_CHUNK.' OFFSET %d', $offset);
149    
150                    my $sth = $dbh->prepare(qq{
151                            SELECT
152                                    files.id                        AS fid,
153                                    hosts.name                      AS hname,
154                                    shares.name                     AS sname,
155                                    -- shares.share                 AS sharename,
156                                    files.backupnum                 AS backupnum,
157                                    -- files.name                   AS filename,
158                                    files.path                      AS filepath,
159                                    files.date                      AS date,
160                                    files.type                      AS type,
161                                    files.size                      AS size,
162                                    files.shareid                   AS shareid,
163                                    backups.date                    AS backup_date
164                            FROM files
165                                    INNER JOIN shares       ON files.shareID=shares.ID
166                                    INNER JOIN hosts        ON hosts.ID = shares.hostID
167                                    INNER JOIN backups      ON backups.num = files.backupNum and backups.hostID = hosts.ID AND backups.shareID = shares.ID
168                            $where
169                            $limit
170                    });
171    
172                    $sth->execute(@data);
173                    $results = $sth->rows;
174    
175                    if ($results == 0) {
176                            print " - no new files\n";
177                            last;
178                    }
179    
180                    sub fmt_date {
181                            my $t = shift || return;
182                            my $iso = BackupPC::Lib::timeStamp($t);
183                            $iso =~ s/\s/T/;
184                            return $iso;
185                    }
186    
187                    while (my $row = $sth->fetchrow_hashref()) {
188    
189                            my $fid = $row->{'fid'} || die "no fid?";
190                            my $uri = 'file:///' . $fid;
191    
192                            my $id = ($hest_db || $hest_node)->uri_to_id($uri);
193                            next unless ($id == -1);
194    
195                            # create a document object
196                            my $doc = HyperEstraier::Document->new;
197    
198                            # add attributes to the document object
199                            $doc->add_attr('@uri', $uri);
200    
201                            foreach my $c (@{ $sth->{NAME} }) {
202                                    $doc->add_attr($c, $row->{$c}) if ($row->{$c});
203                            }
204    
205                            #$doc->add_attr('@cdate', fmt_date($row->{'date'}));
206    
207                            # add the body text to the document object
208                            my $path = $row->{'filepath'};
209                            $doc->add_text($path);
210                            $path =~ s/(.)/$1 /g;
211                            $doc->add_hidden_text($path);
212    
213                            print STDERR $doc->dump_draft,"\n" if ($debug > 1);
214    
215                            # register the document object to the database
216                            if ($hest_db) {
217                                    $hest_db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
218                            } elsif ($hest_node) {
219                                    $hest_node->put_doc($doc);
220                            } else {
221                                    die "not supported";
222                            }
223                            $added++;
224                    }
225    
226                    print " $added";
227                    $hest_db->sync() if ($index_path);
228    
229                    $offset += EST_CHUNK;
230    
231            } while ($results == EST_CHUNK);
232    
233            if ($index_path) {
234                    print ", close";
235                    $hest_db->close();
236            }
237    
238            my $dur = (time() - $t) || 1;
239            printf(" [%.2f/s dur: %s]\n",
240                    ( $added / $dur ),
241                    fmt_time($dur)
242            );
243    }
244    
245    #---- /subs ----
246    
247    
248    ## update index ##
249    if (($opt{i} || ($index_path && ! -e $index_path)) && !$opt{c}) {
250            # update all
251            print "force update of HyperEstraier index ";
252            print "importing existing data" unless (-e $index_path);
253            print "by -i flag" if ($opt{i});
254            print "\n";
255            hest_update();
256    }
257    
258    ## create tables ##
259  if ($opt{c}) {  if ($opt{c}) {
260          sub do_index {          sub do_index {
261                  my $index = shift || return;                  my $index = shift || return;
# Line 149  if ($opt{c}) { Line 345  if ($opt{c}) {
345    
346  }  }
347    
348    ## delete data before inseting ##
349  if ($opt{d}) {  if ($opt{d}) {
350          print "deleting ";          print "deleting ";
351          foreach my $table (qw(files dvds backups shares hosts)) {          foreach my $table (qw(files dvds backups shares hosts)) {
# Line 160  if ($opt{d}) { Line 357  if ($opt{d}) {
357          $dbh->commit;          $dbh->commit;
358  }  }
359    
360  if ($opt{v}) {  ## insert new values ##
         print "Debug level at $opt{v}\n";  
         $debug = $opt{v};  
 }  
   
 #################################INSERT VALUES#############################  
361    
362  # get hosts  # get hosts
363  $hosts = $bpc->HostInfoRead();  $hosts = $bpc->HostInfoRead();
# Line 199  INSERT INTO files Line 391  INSERT INTO files
391          VALUES (?,?,?,?,?,?,?)          VALUES (?,?,?,?,?,?,?)
392  });  });
393    
 sub fmt_time {  
         my $t = shift || return;  
         my $out = "";  
         my ($ss,$mm,$hh) = gmtime($t);  
         $out .= "${hh}h" if ($hh);  
         $out .= sprintf("%02d:%02d", $mm,$ss);  
         return $out;  
 }  
   
394  foreach my $host_key (keys %{$hosts}) {  foreach my $host_key (keys %{$hosts}) {
395    
396          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 263  foreach my $host_key (keys %{$hosts}) { Line 446  foreach my $host_key (keys %{$hosts}) {
446                          next if ($count > 0);                          next if ($count > 0);
447    
448                          # dump some log                          # dump some log
449                          print strftime($t_fmt,localtime())," ", $share;                          print curr_time," ", $share;
450    
451                          my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);                          my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
452    
# Line 286  foreach my $host_key (keys %{$hosts}) { Line 469  foreach my $host_key (keys %{$hosts}) {
469                                  ( ($f+$d) / $dur ),                                  ( ($f+$d) / $dur ),
470                                  fmt_time($dur)                                  fmt_time($dur)
471                          );                          );
472    
473                            hest_update($hostID, $shareID, $backupNum) if ($nf + $nd > 0);
474                  }                  }
475    
476          }          }
# Line 330  sub found_in_db { Line 515  sub found_in_db {
515          my @data = @_;          my @data = @_;
516          shift @data;          shift @data;
517    
518          my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_;          my ($key, $shareID,undef,$name,$path,$date,undef,$size) = @_;
519    
520          return $beenThere->{$key} if (defined($beenThere->{$key}));          return $beenThere->{$key} if (defined($beenThere->{$key}));
521    
# Line 340  sub found_in_db { Line 525  sub found_in_db {
525                          path = ? and                          path = ? and
526                          date = ? and                          date = ? and
527                          size = ?                          size = ?
528                    LIMIT 1
529          });          });
530    
531          my @param = ($shareID,$path,$date,$size);          my @param = ($shareID,$path,$date,$size);
532          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
533          my $rows = $sth->{file_in_db}->rows;          my $rows = $sth->{file_in_db}->rows;
534          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);
535    
536          $beenThere->{$key}++;          $beenThere->{$key}++;
537    
# Line 392  sub recurseDir($$$$$$$$) { Line 578  sub recurseDir($$$$$$$$) {
578                                  $filesInBackup->{$path_key}->{'size'}                                  $filesInBackup->{$path_key}->{'size'}
579                          ));                          ));
580    
581                            my $found;
582                          if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) {                          if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) {
583                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
584    
585                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
586                                          $new_dirs++;                                          $new_dirs++ unless ($found);
587                                          print STDERR " dir\n" if ($debug >= 2);                                          print STDERR " dir\n" if ($debug >= 2);
588                                  } else {                                  } else {
589                                          $new_files++;                                          $new_files++ unless ($found);
590                                          print STDERR " file\n" if ($debug >= 2);                                          print STDERR " file\n" if ($debug >= 2);
591                                  }                                  }
592                                  $size += $filesInBackup->{$path_key}->{'size'} || 0;                                  $size += $filesInBackup->{$path_key}->{'size'} || 0;

Legend:
Removed from v.67  
changed lines
  Added in v.117

  ViewVC Help
Powered by ViewVC 1.1.26