/[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 15 by dpavlin, Mon Jul 11 00:07:25 2005 UTC revision 35 by dpavlin, Sun Jul 31 21:03:36 2005 UTC
# Line 9  use Data::Dumper; Line 9  use Data::Dumper;
9  use Getopt::Std;  use Getopt::Std;
10  use constant BPC_FTYPE_DIR => 5;  use constant BPC_FTYPE_DIR => 5;
11    
12    my $debug = 0;
13  $|=1;  $|=1;
14    
15  my $hosts;  my $hosts;
# Line 23  my $dbh = DBI->connect($dsn, "", "", { R Line 24  my $dbh = DBI->connect($dsn, "", "", { R
24    
25  my %opt;  my %opt;
26    
27  if ( !getopts("cdm:", \%opt ) ) {  if ( !getopts("cdm:v", \%opt ) ) {
28          print STDERR <<EOF;          print STDERR <<EOF;
29  usage: $0 [-c|-d] [-m num]  usage: $0 [-c|-d] [-m num]
30    
# Line 81  if ($opt{c}) { Line 82  if ($opt{c}) {
82                  create table files (                  create table files (
83                          ID      INTEGER         NOT NULL PRIMARY KEY,                            ID      INTEGER         NOT NULL PRIMARY KEY,  
84                          shareID INTEGER         NOT NULL references shares(id),                          shareID INTEGER         NOT NULL references shares(id),
85                          backupNum  INTEGER      NOT NULL references backups(id),                          backupNum  INTEGER      NOT NULL references backups(num),
86                          name       VARCHAR(255) NOT NULL,                          name       VARCHAR(255) NOT NULL,
87                          path       VARCHAR(255) NOT NULL,                          path       VARCHAR(255) NOT NULL,
88                          fullpath   VARCHAR(255) NOT NULL,                          fullpath   VARCHAR(255) NOT NULL,
# Line 122  if ($opt{d}) { Line 123  if ($opt{d}) {
123          print " done...\n";          print " done...\n";
124  }  }
125    
126    if ($opt{v}) {
127            print "Debug level at $opt{v}\n";
128            $debug = $opt{v};
129    }
130    
131  #################################INSERT VALUES#############################  #################################INSERT VALUES#############################
132    
133  # get hosts  # get hosts
# Line 192  foreach my $host_key (keys %{$hosts}) { Line 198  foreach my $host_key (keys %{$hosts}) {
198                  my ($broj) = $sth->{backups_broj}->fetchrow_array();                  my ($broj) = $sth->{backups_broj}->fetchrow_array();
199                  next if ($broj > 0);                  next if ($broj > 0);
200    
201                  $sth->{insert_backups}->execute(                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
                         $hostID,  
                         $backupNum,  
                         $backup->{'endTime'},  
                         $backup->{'type'}  
                 );  
                 $dbh->commit();  
   
                 my $files = BackupPC::View->new($bpc, $hostname, \@backups);  
202                  foreach my $share ($files->shareList($backupNum)) {                  foreach my $share ($files->shareList($backupNum)) {
203    
204                          print "\t$share";                          print "\t$share";
205                          $shareID = getShareID($share, $hostID, $hostname);                          $shareID = getShareID($share, $hostID, $hostname);
206                                    
207                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, \@backups, $backupNum, $share, "", $shareID);                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
208                          print " $nf/$f files $nd/$d dirs\n";                          print " $nf/$f files $nd/$d dirs\n";
209                          $dbh->commit();                          $dbh->commit();
210                  }                  }
211    
212                    $sth->{insert_backups}->execute(
213                            $hostID,
214                            $backupNum,
215                            $backup->{'endTime'},
216                            $backup->{'type'}
217                    );
218                    $dbh->commit();
219    
220          }          }
221  }  }
222  undef $sth;  undef $sth;
# Line 236  sub getShareID() { Line 243  sub getShareID() {
243                  VALUES (?,?,?,?)                  VALUES (?,?,?,?)
244          });          });
245    
246          $sth->{insert_share}->execute($hostID,$share, $hostname . $share,undef);          my $drop_down = $hostname . '/' . $share;
247            $drop_down =~ s#//+#/#g;
248    
249            $sth->{insert_share}->execute($hostID,$share, $drop_down ,undef);
250          return $dbh->func('last_insert_rowid');                  return $dbh->func('last_insert_rowid');        
251  }  }
252    
# Line 266  sub found_in_db { Line 276  sub found_in_db {
276  ####################################################  ####################################################
277  sub recurseDir($$$$$$$$) {  sub recurseDir($$$$$$$$) {
278    
279          my ($bpc, $hostname, $backups, $backupNum, $share, $dir, $shareID) = @_;          my ($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID) = @_;
280    
281          my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0);          print STDERR "recurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1);
282    
283          my $files = BackupPC::View->new($bpc, $hostname, $backups);                      my ($nr_files, $new_files, $nr_dirs, $new_dirs) = (0,0,0,0);
         my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);  
284    
285          # first, add all the entries in current directory          { # scope
286          foreach my $path_key (keys %{$filesInBackup}) {                  my @stack;
                 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'}  
                 );  
287    
288                  my $key = join(" ", (                  my $filesInBackup = $files->dirAttrib($backupNum, $share, $dir);
                         $shareID,  
                         $dir,  
                         $path_key,  
                         $filesInBackup->{$path_key}->{'mtime'},  
                         $filesInBackup->{$path_key}->{'size'}  
                 ));  
289    
290                    # first, add all the entries in current directory
291                    foreach my $path_key (keys %{$filesInBackup}) {
292                            my @data = (
293                                    $shareID,
294                                    $backupNum,
295                                    $path_key,
296                                    $filesInBackup->{$path_key}->{'relPath'},
297                                    $filesInBackup->{$path_key}->{'fullPath'},
298            #                       $filesInBackup->{$path_key}->{'sharePathM'},
299                                    $filesInBackup->{$path_key}->{'mtime'},
300                                    $filesInBackup->{$path_key}->{'type'},
301                                    $filesInBackup->{$path_key}->{'size'}
302                            );
303    
304                            my $key = join(" ", (
305                                    $shareID,
306                                    $dir,
307                                    $path_key,
308                                    $filesInBackup->{$path_key}->{'mtime'},
309                                    $filesInBackup->{$path_key}->{'size'}
310                            ));
311    
312    
313                            if (! $beenThere->{$key} && ! found_in_db(@data)) {
314                                    print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
315                                    $sth->{'insert_files'}->execute(@data);
316                                    if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
317                                            $new_dirs++;
318                                            print STDERR " dir\n" if ($debug >= 2);
319                                    } else {
320                                            $new_files++;
321                                            print STDERR " file\n" if ($debug >= 2);
322                                    }
323                            }
324                            $beenThere->{$key}++;
325    
                 if (! $beenThere->{$key} && ! found_in_db(@data)) {  
                         $sth->{'insert_files'}->execute(@data);  
 #                       print STDERR "$key\n";  
326                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
327                                  $new_dirs++;                                  $nr_dirs++;
328    
329                                    my $full_path = $dir . '/' . $path_key;
330                                    push @stack, $full_path;
331                                    print STDERR "### store to stack: $full_path\n" if ($debug >= 3);
332    
333    #                               my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID) unless ($beenThere->{$key});
334    #
335    #                               $nr_files += $f;
336    #                               $new_files += $nf;
337    #                               $nr_dirs += $d;
338    #                               $new_dirs += $nd;
339    
340                          } else {                          } else {
341                                  $new_files++;                                  $nr_files++;
342                          }                          }
343                  }                  }
                 $beenThere->{$key}++;  
   
                 if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {  
                         $nr_dirs++;  
344    
345                          my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $backups, $backupNum, $share, $path_key, $shareID);                  print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);
346    
347                    while ( my $dir = shift @stack ) {
348                            my ($f,$nf,$d,$nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, $dir, $shareID);
349                            print STDERR "# $dir f: $f nf: $nf d: $d nd: $nd\n" if ($debug >= 1);
350                          $nr_files += $f;                          $nr_files += $f;
351                          $new_files += $nf;                          $new_files += $nf;
352                          $nr_dirs += $d;                          $nr_dirs += $d;
353                          $new_dirs += $nd;                          $new_dirs += $nd;
   
                 } else {  
                         $nr_files++;  
354                  }                  }
355          }          }
356    

Legend:
Removed from v.15  
changed lines
  Added in v.35

  ViewVC Help
Powered by ViewVC 1.1.26