/[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 52 by dpavlin, Sat Aug 20 16:45:02 2005 UTC revision 74 by dpavlin, Fri Aug 26 17:18:27 2005 UTC
# Line 95  if ($opt{c}) { Line 95  if ($opt{c}) {
95                          num     INTEGER         NOT NULL,                          num     INTEGER         NOT NULL,
96                          date    integer         NOT NULL,                          date    integer         NOT NULL,
97                          type    CHAR(4)         not null,                          type    CHAR(4)         not null,
98                          PRIMARY KEY(hostID, num)                          shareID integer         not null references shares(id),
99                            size    integer         not null,
100                            PRIMARY KEY(hostID, num, shareID)
101                  );                              );            
102          });          });
103    
104          do_index('backups_hostid,num_unique');          #do_index('backups_hostid,num_unique');
105    
106          $dbh->do(qq{          $dbh->do(qq{
107                  create table dvds (                  create table dvds (
# Line 117  if ($opt{c}) { Line 119  if ($opt{c}) {
119                          backupNum  INTEGER      NOT NULL,                          backupNum  INTEGER      NOT NULL,
120                          name       VARCHAR(255) NOT NULL,                          name       VARCHAR(255) NOT NULL,
121                          path       VARCHAR(255) NOT NULL,                          path       VARCHAR(255) NOT NULL,
                         fullpath   VARCHAR(255) NOT NULL,  
122                          date       integer      NOT NULL,                          date       integer      NOT NULL,
123                          type       INTEGER      NOT NULL,                          type       INTEGER      NOT NULL,
124                          size       INTEGER      NOT NULL,                          size       INTEGER      NOT NULL,
# Line 181  $sth->{hosts_by_name} = $dbh->prepare(qq Line 182  $sth->{hosts_by_name} = $dbh->prepare(qq
182  SELECT ID FROM hosts WHERE name=?  SELECT ID FROM hosts WHERE name=?
183  });  });
184    
185  $sth->{backups_broj} = $dbh->prepare(qq{  $sth->{backups_count} = $dbh->prepare(qq{
186  SELECT COUNT(*)  SELECT COUNT(*)
187  FROM backups  FROM backups
188  WHERE hostID=? AND num=?  WHERE hostID=? AND num=? AND shareid=?
189  });  });
190    
191  $sth->{insert_backups} = $dbh->prepare(qq{  $sth->{insert_backups} = $dbh->prepare(qq{
192  INSERT INTO backups (hostID, num, date, type)  INSERT INTO backups (hostID, num, date, type, shareid, size)
193  VALUES (?,?,?,?)  VALUES (?,?,?,?,?,?)
194  });  });
195    
196  $sth->{insert_files} = $dbh->prepare(qq{  $sth->{insert_files} = $dbh->prepare(qq{
197  INSERT INTO files  INSERT INTO files
198          (shareID, backupNum, name, path, fullpath, date, type, size)          (shareID, backupNum, name, path, date, type, size)
199          VALUES (?,?,?,?,?,?,?,?)          VALUES (?,?,?,?,?,?,?)
200  });  });
201    
202  sub fmt_time {  sub fmt_time {
# Line 222  foreach my $host_key (keys %{$hosts}) { Line 223  foreach my $host_key (keys %{$hosts}) {
223                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);                  $hostID = $dbh->last_insert_id(undef,undef,'hosts',undef);
224          }          }
225    
226          print("host ".$hosts->{$host_key}->{'host'}.": ");          print "host ".$hosts->{$host_key}->{'host'}.": ";
227    
228          # get backups for a host          # get backups for a host
229          my @backups = $bpc->BackupInfoRead($hostname);          my @backups = $bpc->BackupInfoRead($hostname);
230          print scalar @backups, " increments\n";          my $incs = scalar @backups;
231            print  "$incs increments\n";
232    
233          my $inc_nr = 0;          my $inc_nr = 0;
234            $beenThere = {};
235    
236          foreach my $backup (@backups) {          foreach my $backup (@backups) {
237    
# Line 238  foreach my $host_key (keys %{$hosts}) { Line 241  foreach my $host_key (keys %{$hosts}) {
241                  my $backupNum = $backup->{'num'};                  my $backupNum = $backup->{'num'};
242                  my @backupShares = ();                  my @backupShares = ();
243    
244                  print $hosts->{$host_key}->{'host'},                  printf("%-10s %2d/%-2d #%-2d %s %5s/%5s files (date: %s dur: %s)\n",
245                          "\t#$backupNum\t", $backup->{type} || '?', " ",                          $hosts->{$host_key}->{'host'},
246                          $backup->{nFilesNew} || '?', "/", $backup->{nFiles} || '?',                          $inc_nr, $incs, $backupNum,
247                          " files\n";                          $backup->{type} || '?',
248                            $backup->{nFilesNew} || '?', $backup->{nFiles} || '?',
249                  $sth->{backups_broj}->execute($hostID, $backupNum);                          strftime($t_fmt,localtime($backup->{startTime})),
250                  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'}  
251                  );                  );
                 $dbh->commit();  
252    
253                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);                  my $files = BackupPC::View->new($bpc, $hostname, \@backups, 1);
254                  foreach my $share ($files->shareList($backupNum)) {                  foreach my $share ($files->shareList($backupNum)) {
255    
256                          my $t = time();                          my $t = time();
257    
                         print strftime($t_fmt,localtime())," ", $share;  
258                          $shareID = getShareID($share, $hostID, $hostname);                          $shareID = getShareID($share, $hostID, $hostname);
259                                    
260                          my ($f, $nf, $d, $nd) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);                          $sth->{backups_count}->execute($hostID, $backupNum, $shareID);
261                            my ($count) = $sth->{backups_count}->fetchrow_array();
262                            # skip if allready in database!
263                            next if ($count > 0);
264    
265                            # dump some log
266                            print strftime($t_fmt,localtime())," ", $share;
267    
268                            my ($f, $nf, $d, $nd, $size) = recurseDir($bpc, $hostname, $files, $backupNum, $share, "", $shareID);
269    
270                            $sth->{insert_backups}->execute(
271                                    $hostID,
272                                    $backupNum,
273                                    $backup->{'endTime'},
274                                    $backup->{'type'},
275                                    $shareID,
276                                    $size,
277                            );
278    
279                            print " commit";
280                            $dbh->commit();
281    
282                          my $dur = (time() - $t) || 1;                          my $dur = (time() - $t) || 1;
283                          printf(" %d/%d files %d/%d dirs [%.2f/s dur: %s]\n",                          printf(" %d/%d files %d/%d dirs %0.2f MB [%.2f/s dur: %s]\n",
284                                  $nf, $f, $nd, $d,                                  $nf, $f, $nd, $d,
285                                    ($size / 1024 / 1024),
286                                  ( ($f+$d) / $dur ),                                  ( ($f+$d) / $dur ),
287                                  fmt_time($dur)                                  fmt_time($dur)
288                          );                          );
                         $dbh->commit();  
289                  }                  }
290    
291          }          }
# Line 315  sub found_in_db { Line 330  sub found_in_db {
330          my @data = @_;          my @data = @_;
331          shift @data;          shift @data;
332    
333          my ($key, $shareID,undef,$name,$path,undef,$date,undef,$size) = @_;          my ($key, $shareID,undef,$name,$path,$date,undef,$size) = @_;
334    
335          return $beenThere->{$key} if (defined($beenThere->{$key}));          return $beenThere->{$key} if (defined($beenThere->{$key}));
336    
# Line 323  sub found_in_db { Line 338  sub found_in_db {
338                  SELECT 1 FROM files                  SELECT 1 FROM files
339                  WHERE shareID = ? and                  WHERE shareID = ? and
340                          path = ? and                          path = ? and
                         name = ? and  
341                          date = ? and                          date = ? and
342                          size = ?                          size = ?
343                    LIMIT 1
344          });          });
345    
346          my @param = ($shareID,$path,$name,$date,$size);          my @param = ($shareID,$path,$date,$size);
347          $sth->{file_in_db}->execute(@param);          $sth->{file_in_db}->execute(@param);
348          my $rows = $sth->{file_in_db}->rows;          my $rows = $sth->{file_in_db}->rows;
349          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);
350    
351          $beenThere->{$key}++;          $beenThere->{$key}++;
352    
# Line 349  sub recurseDir($$$$$$$$) { Line 364  sub recurseDir($$$$$$$$) {
364    
365          print STDERR "\nrecurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1);          print STDERR "\nrecurse($hostname,$backupNum,$share,$dir,$shareID)\n" if ($debug >= 1);
366    
367          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);
368    
369          { # scope          { # scope
370                  my @stack;                  my @stack;
# Line 359  sub recurseDir($$$$$$$$) { Line 374  sub recurseDir($$$$$$$$) {
374    
375                  # first, add all the entries in current directory                  # first, add all the entries in current directory
376                  foreach my $path_key (keys %{$filesInBackup}) {                  foreach my $path_key (keys %{$filesInBackup}) {
377                            print STDERR "# file ",Dumper($filesInBackup->{$path_key}),"\n" if ($debug >= 3);
378                          my @data = (                          my @data = (
379                                  $shareID,                                  $shareID,
380                                  $backupNum,                                  $backupNum,
381                                  $path_key,                                  $path_key,
382                                  $filesInBackup->{$path_key}->{'relPath'},                                  $filesInBackup->{$path_key}->{'relPath'},
                                 $filesInBackup->{$path_key}->{'fullPath'},  
         #                       $filesInBackup->{$path_key}->{'sharePathM'},  
383                                  $filesInBackup->{$path_key}->{'mtime'},                                  $filesInBackup->{$path_key}->{'mtime'},
384                                  $filesInBackup->{$path_key}->{'type'},                                  $filesInBackup->{$path_key}->{'type'},
385                                  $filesInBackup->{$path_key}->{'size'}                                  $filesInBackup->{$path_key}->{'size'}
# Line 379  sub recurseDir($$$$$$$$) { Line 393  sub recurseDir($$$$$$$$) {
393                                  $filesInBackup->{$path_key}->{'size'}                                  $filesInBackup->{$path_key}->{'size'}
394                          ));                          ));
395    
396                            my $found;
397                          if (! defined($beenThere->{$key}) && ! found_in_db($key, @data)) {                          if (! defined($beenThere->{$key}) && ! ($found = found_in_db($key, @data)) ) {
398                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);                                  print STDERR "# key: $key [", $beenThere->{$key},"]" if ($debug >= 2);
399    
400                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                                  if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
401                                          $new_dirs++;                                          $new_dirs++ unless ($found);
402                                          print STDERR " dir\n" if ($debug >= 2);                                          print STDERR " dir\n" if ($debug >= 2);
403                                  } else {                                  } else {
404                                          $new_files++;                                          $new_files++ unless ($found);
405                                          print STDERR " file\n" if ($debug >= 2);                                          print STDERR " file\n" if ($debug >= 2);
406                                  }                                  }
407                                    $size += $filesInBackup->{$path_key}->{'size'} || 0;
408                          }                          }
409    
410                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {                          if ($filesInBackup->{$path_key}->{'type'} == BPC_FTYPE_DIR) {
# Line 414  sub recurseDir($$$$$$$$) { Line 429  sub recurseDir($$$$$$$$) {
429                  print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);                  print STDERR "## STACK ",join(", ", @stack),"\n" if ($debug >= 2);
430    
431                  while ( my $dir = shift @stack ) {                  while ( my $dir = shift @stack ) {
432                          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);
433                          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);
434                          $nr_files += $f;                          $nr_files += $f;
435                          $new_files += $nf;                          $new_files += $nf;
436                          $nr_dirs += $d;                          $nr_dirs += $d;
437                          $new_dirs += $nd;                          $new_dirs += $nd;
438                            $size += $s;
439                  }                  }
440          }          }
441    
442          return ($nr_files, $new_files, $nr_dirs, $new_dirs);          return ($nr_files, $new_files, $nr_dirs, $new_dirs, $size);
443  }  }
444    

Legend:
Removed from v.52  
changed lines
  Added in v.74

  ViewVC Help
Powered by ViewVC 1.1.26