/[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 18 by dpavlin, Mon Jul 11 14:31:51 2005 UTC revision 37 by dpavlin, Fri Aug 19 14:57:30 2005 UTC
# Line 7  use BackupPC::Lib; Line 7  use BackupPC::Lib;
7  use BackupPC::View;  use BackupPC::View;
8  use Data::Dumper;  use Data::Dumper;
9  use Getopt::Std;  use Getopt::Std;
10    use Time::HiRes qw/time/;
11    use POSIX qw/strftime/;
12  use constant BPC_FTYPE_DIR => 5;  use constant BPC_FTYPE_DIR => 5;
13    
14    my $debug = 0;
15  $|=1;  $|=1;
16    
17    my $t_fmt = '%Y-%m-%d %H:%M:%S';
18    
19  my $hosts;  my $hosts;
20  my $bpc = BackupPC::Lib->new || die;  my $bpc = BackupPC::Lib->new || die;
21  my %Conf = $bpc->Conf();  my %Conf = $bpc->Conf();
# Line 23  my $dbh = DBI->connect($dsn, "", "", { R Line 28  my $dbh = DBI->connect($dsn, "", "", { R
28    
29  my %opt;  my %opt;
30    
31  if ( !getopts("cdm:", \%opt ) ) {  if ( !getopts("cdm:v", \%opt ) ) {
32          print STDERR <<EOF;          print STDERR <<EOF;
33  usage: $0 [-c|-d] [-m num]  usage: $0 [-c|-d] [-m num]
34    
# Line 122  if ($opt{d}) { Line 127  if ($opt{d}) {
127          print " done...\n";          print " done...\n";
128  }  }
129    
130    if ($opt{v}) {
131            print "Debug level at $opt{v}\n";
132            $debug = $opt{v};
133    }
134    
135  #################################INSERT VALUES#############################  #################################INSERT VALUES#############################
136    
137  # get hosts  # get hosts
# Line 186  foreach my $host_key (keys %{$hosts}) { Line 196  foreach my $host_key (keys %{$hosts}) {
196                  my $backupNum = $backup->{'num'};                  my $backupNum = $backup->{'num'};
197                  my @backupShares = ();                  my @backupShares = ();
198    
199                  print $hosts->{$host_key}->{'host'},"\t#$backupNum\n";                  print $hosts->{$host_key}->{'host'}, "\t#$backupNum\n";
200    
201                  $sth->{backups_broj}->execute($hostID, $backupNum);                  $sth->{backups_broj}->execute($hostID, $backupNum);
202                  my ($broj) = $sth->{backups_broj}->fetchrow_array();                  my ($broj) = $sth->{backups_broj}->fetchrow_array();
203                  next if ($broj > 0);                  next if ($broj > 0);
204    
205                    my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
206                    foreach my $share ($files->shareList($backupNum)) {
207    
208                            my $t = time();
209    
210                            print strftime($t_fmt,localtime())," ", $share;
211                            $shareID = getShareID($share, $hostID, $hostname);
212                    
213                            my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
214                            printf(" %d/%d files %d/%d dirs [%.2f/s]\n",
215                                    $nf, $f, $nd, $d,
216                                    ( ($f+$d) / ((time() - $t) || 1) )
217                            );
218                            $dbh->commit();
219                    }
220    
221                  $sth->{insert_backups}->execute(                  $sth->{insert_backups}->execute(
222                          $hostID,                          $hostID,
223                          $backupNum,                          $backupNum,
# Line 200  foreach my $host_key (keys %{$hosts}) { Line 226  foreach my $host_key (keys %{$hosts}) {
226                  );                  );
227                  $dbh->commit();                  $dbh->commit();
228    
                 my $files = BackupPC::View->new($bpc, $hostname, \@backups);  
                 foreach my $share ($files->shareList($backupNum)) {  
   
                         print "\t$share";  
                         $shareID = getShareID($share, $hostID, $hostname);  
                   
                         my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, \@backups, $backupNum, $share, "", $shareID);  
                         print " $nf/$f files $nd/$d dirs\n";  
                         $dbh->commit();  
                 }  
229          }          }
230  }  }
231  undef $sth;  undef $sth;
# Line 236  sub getShareID() { Line 252  sub getShareID() {
252                  VALUES (?,?,?,?)                  VALUES (?,?,?,?)
253          });          });
254    
255          $sth->{insert_share}->execute($hostID,$share, $hostname . $share,undef);          my $drop_down = $hostname . '/' . $share;
256            $drop_down =~ s#//+#/#g;
257    
258            $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);
259          return $dbh->func('last_insert_rowid');                  return $dbh->func('last_insert_rowid');        
260  }  }
261    
# Line 266  sub found_in_db { Line 285  sub found_in_db {
285  ####################################################  ####################################################
286  sub recurseDir($$$$$$$$) {  sub recurseDir($$$$$$$$) {
287    
288          my ($bpc, $hostname, $backups, $backupNum, $share, $dir, $shareID) = @_;          my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_;
289    
290            print STDERR "recurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1);
291    
292          my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0);          my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0);
293    
294          my $files = BackupPC::View->new($bpc, $hostname, $backups);                      { # scope
295          my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);                  my @stack;
296    
297          # first, add all the entries in current directory                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);
         foreach my $path_key (keys %{$filesInBackup}) {  
                 my @data = (  
                         $shareID,  
                         $backupNum,  
                         $path_key,  
                         $filesInBackup->{$path_key}->{'relPath'},  
                         $filesInBackup->{$path_key}->{'fullPath'},  
 #                       $filesInBackup->{$path_key}->{'sharePathM'},  
                         $filesInBackup->{$path_key}->{'mtime'},  
                         $filesInBackup->{$path_key}->{'type'},  
                         $filesInBackup->{$path_key}->{'size'}  
                 );  
   
                 my $key = join(" ", (  
                         $shareID,  
                         $dir,  
                         $path_key,  
                         $filesInBackup->{$path_key}->{'mtime'},  
                         $filesInBackup->{$path_key}->{'size'}  
                 ));  
298    
299                    # first, add all the entries in current directory
300                    foreach my $path_key (keys %{$filesInBackup}) {
301                            my @data = (
302                                    $shareID,
303                                    $backupNum,
304                                    $path_key,
305                                    $filesInBackup->{$path_key}->{'relPath'},
306                                    $filesInBackup->{$path_key}->{'fullPath'},
307            #                       $filesInBackup->{$path_key}->{'sharePathM'},
308                                    $filesInBackup->{$path_key}->{'mtime'},
309                                    $filesInBackup->{$path_key}->{'type'},
310                                    $filesInBackup->{$path_key}->{'size'}
311                            );
312    
313                            my $key = join(" ", (
314                                    $shareID,
315                                    $dir,
316                                    $path_key,
317                                    $filesInBackup->{$path_key}->{'mtime'},
318                                    $filesInBackup->{$path_key}->{'size'}
319                            ));
320    
321    
322                            if (! $beenThere->{$key} && ! found_in_db(@data)) {
323                                    print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
324                                    $sth->{'insert_files'}->execute(@data);
325                                    if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
326                                            $new_dirs++;
327                                            print STDERR " dir\n" if ($debug >= 2);
328                                    } else {
329                                            $new_files++;
330                                            print STDERR " file\n" if ($debug >= 2);
331                                    }
332                            }
333                            $beenThere->{$key}++;
334    
                 if (! $beenThere->{$key} && ! found_in_db(@data)) {  
                         $sth->{'insert_files'}->execute(@data);  
 #                       print STDERR "$key\n";  
335                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
336                                  $new_dirs++;                                  $nr_dirs++;
337    
338                                    my $full_path = $dir . '/' . $path_key;
339                                    push @stack, $full_path;
340                                    print STDERR "### store to stack: $full_path\n" if ($debug >= 3);
341    
342    #                               my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID) unless ($beenThere->{$key});
343    #
344    #                               $nr_files += $f;
345    #                               $new_files += $nf;
346    #                               $nr_dirs += $d;
347    #                               $new_dirs += $nd;
348    
349                          } else {                          } else {
350                                  $new_files++;                                  $nr_files++;
351                          }                          }
352                  }                  }
                 $beenThere->{$key}++;  
   
                 if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {  
                         $nr_dirs++;  
353    
354                          my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID);                  print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);
355    
356                    while ( my $dir = shift @stack ) {
357                            my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID);
358                            print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1);
359                          $nr_files += $f;                          $nr_files += $f;
360                          $new_files += $nf;                          $new_files += $nf;
361                          $nr_dirs += $d;                          $nr_dirs += $d;
362                          $new_dirs += $nd;                          $new_dirs += $nd;
   
                 } else {  
                         $nr_files++;  
363                  }                  }
364          }          }
365    

Legend:
Removed from v.18  
changed lines
  Added in v.37

  ViewVC Help
Powered by ViewVC 1.1.26