/[BackupPC]/trunk/bin/BackupPC_incPartsUpdate
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_incPartsUpdate

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 265 by dpavlin, Tue Dec 13 00:10:48 2005 UTC revision 279 by dpavlin, Wed Dec 14 00:00:26 2005 UTC
# Line 100  sub get_backup_id($$$) { Line 100  sub get_backup_id($$$) {
100  sub tar_check($$$$) {  sub tar_check($$$$) {
101          my ($host,$share,$num,$filename) = @_;          my ($host,$share,$num,$filename) = @_;
102    
103            my $t = time();
104            print curr_time, " check $host:$share#$num -> $filename";
105    
106          sub check_part {          sub check_part {
107                  my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_;                  my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_;
108                  my $backup_id = get_backup_id($host, $share, $num);                  my $backup_id = get_backup_id($host, $share, $num);
# Line 119  sub tar_check($$$$) { Line 122  sub tar_check($$$$) {
122                                  $row->{md5} eq $md5 &&                                  $row->{md5} eq $md5 &&
123                                  $row->{items} == $items                                  $row->{items} == $items
124                          );                          );
125                          print STDERR "# deleting invalid row $row->{id}\n" if ($opt{d});                          print ", deleting invalid backup_parts $row->{id}";
126                          $dbh->do(qq{ delete from backup_parts where id = $row->{id} });                          $dbh->do(qq{ delete from backup_parts where id = $row->{id} });
127                  }                  }
128                  print STDERR "# inserting new backup_part row\n";                  print ", inserting new";
129                  my $sth_insert = $dbh->prepare(qq{                  my $sth_insert = $dbh->prepare(qq{
130                          insert into backup_parts (                          insert into backup_parts (
131                                  backup_id,                                  backup_id,
# Line 138  sub tar_check($$$$) { Line 141  sub tar_check($$$$) {
141                  $dbh->commit;                  $dbh->commit;
142          }          }
143    
         if ($debug) {  
                 print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";  
         } else {  
                 print " check";  
         }  
   
144          my @tar_parts;          my @tar_parts;
145    
146          if (-d "$tar_dir/$filename") {          if (-d "$tar_dir/$filename") {
147                  print STDERR " multi-part" if ($opt{d});                  print ", multi-part";
148                  opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!";                  opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!";
149                  @tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir);                  @tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir);
150                  closedir($dir);                  closedir($dir);
# Line 162  sub tar_check($$$$) { Line 159  sub tar_check($$$$) {
159    
160          my $backup_part;          my $backup_part;
161    
162          print " reading";          print " reading" if ($opt{d});
163    
164          foreach my $tarfilename (@tar_parts) {          foreach my $tarfilename (@tar_parts) {
165    
166                  print STDERR " $tarfilename" if ($debug);                  print "\n\t- $tarfilename";
167    
168                  my $path = "$tar_dir/$tarfilename";                  my $size = (stat( "$tar_dir/$tarfilename" ))[7] || die "can't stat $tar_dir/$tarfilename";
                 my $md5_path = $path;  
                 $md5_path =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5_path";  
                 if (! -e $md5_path) {  
                         print ", creating md5";  
                         system( $bin->{md5sum} . " $path > $md5_path") == 0 or die "can't create md5 $path: $!";  
                 }  
169    
170                  my $md5 = read_file( $md5_path ) || die "can't read md5sum file $md5_path: $!";                  if ($size > $Conf{MaxArchiveSize}) {
171                  $md5 =~ s#\s.*$##;                          print ", part bigger than media $size > $Conf{MaxArchiveSize}\n";
172                            return 0;
173                    }
174    
175                  my $part_nr = 1;                  print ", $size bytes";
                 $part_nr = $1 if ($tarfilename =~ m#/(\d+)\.tar\.gz#);  
176    
177                  my $size = (stat( "$tar_dir/$tarfilename" ))[7] || die "can't stat $tar_dir/$tarfilename";                  my $path = "$tar_dir/$tarfilename";
178    
179                  open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!";                  open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!";
180                  binmode($fh);                  binmode($fh);
# Line 195  sub tar_check($$$$) { Line 187  sub tar_check($$$$) {
187                          push @tar_files, $entry->name;                          push @tar_files, $entry->name;
188                          $items++;                          $items++;
189                          $tar_size += $entry->size;                          $tar_size += $entry->size;
190    
191                            if ($tar_size > $Conf{MaxArchiveFileSize}) {
192                                    print ", part $tarfilename is too big $tar_size > $Conf{MaxArchiveFileSize}\n";
193                                    return 0;
194                            }
195    
196                  }                  }
197    
198                  if ($tar_size > $Conf{MaxArchiveFileSize}) {                  print ", $items items";
199                          print STDERR " part too big $tar_size > $Conf{MaxArchiveFileSize} }}" if ($debug);  
200                          $same = 0;                  if ($tar_size == 0) {
201                          last;                          print ", EMPTY tar\n";
202                  } elsif ($size > $Conf{MaxArchiveSize}) {  
203                          print STDERR " part bigger than media $size > $Conf{MaxArchiveSize} }}" if ($debug);                          my $backup_id = get_backup_id($host, $share, $num);
204                          $same = 0;  
205                          last;                          my $sth_inc_deleted = $dbh->prepare(qq{
206                                    update backups set
207                                            inc_deleted = true
208                                    where id = ?
209                            });
210                            $sth_inc_deleted->execute($backup_id);
211                    }
212    
213    
214                    #
215                    # check if md5 exists, and if not, create one
216                    #
217    
218                    my $md5_path = $path;
219                    $md5_path =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5_path";
220                    if (! -e $md5_path || -z $md5_path) {
221                            print ", creating md5";
222                            system( $bin->{md5sum} . " $path > $md5_path") == 0 or die "can't create md5 $path: $!";
223                    } else {
224                            ## FIXME check if existing md5 is valid
225                  }                  }
226    
227                    my $md5 = read_file( $md5_path ) || die "can't read md5sum file $md5_path: $!";
228                    $md5 =~ s#\s.*$##;
229    
230                    # extract part number from filename
231                    my $part_nr = 1;
232                    $part_nr = $1 if ($tarfilename =~ m#/(\d+)\.tar\.gz#);
233    
234                    #
235                    # finally, check if backup_parts table in database is valid
236                    #
237    
238                  check_part($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items);                  check_part($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items);
239          }          }
240    
# Line 214  sub tar_check($$$$) { Line 242  sub tar_check($$$$) {
242          return $same unless($same);          return $same unless($same);
243    
244          @tar_files = sort @tar_files;          @tar_files = sort @tar_files;
245          print STDERR " ",($#tar_files + 1), " files" if ($debug);          print "\n\t",($#tar_files + 1), " tar files";
   
         print STDERR ", database" if ($debug);  
246    
247          my $sth = $dbh->prepare(qq{          my $sth = $dbh->prepare(qq{
248                  SELECT path,type                  SELECT path,type
# Line 235  sub tar_check($$$$) { Line 261  sub tar_check($$$$) {
261                  push @db_files, $path;                  push @db_files, $path;
262          }          }
263    
264          print STDERR " ",($#db_files + 1), " files, diff" if ($debug);          print " ",($#db_files + 1), " database files, diff";
265    
266          @db_files = sort @db_files;          @db_files = sort @db_files;
267    
268          if ($#tar_files != $#db_files) {          if ($#tar_files != $#db_files) {
269                  $same = 0;                  $same = 0;
270                  print STDERR " NUMBER" if ($debug);                  print " NUMBER";
271          } else {          } else {
272                  my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);                  my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
273                  while ( $diff->Next() ) {                  while ( $diff->Next() ) {
# Line 252  sub tar_check($$$$) { Line 278  sub tar_check($$$$) {
278                  }                  }
279          }          }
280    
281          print " ",($same ? 'ok' : 'DIFFERENT');          print " ",($same ? 'ok' : 'DIFFERENT'),
282          print STDERR " }} " if ($debug);                  ", dur: ",fmt_time(time() - $t), "\n";
283    
284          return $same;          return $same;
285  }  }
# Line 284  my $num_backups = $sth->rows; Line 310  my $num_backups = $sth->rows;
310  my $curr_backup = 1;  my $curr_backup = 1;
311    
312  while (my $row = $sth->fetchrow_hashref) {  while (my $row = $sth->fetchrow_hashref) {
313    
314            $curr_backup++;
315    
316          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
317    
318          # this will return -1 if file doesn't exist          # this will return -1 if file doesn't exist
# Line 291  while (my $row = $sth->fetchrow_hashref) Line 320  while (my $row = $sth->fetchrow_hashref)
320    
321          print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});          print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
322    
323          if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size) {          if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} >= $size) {
324                  if ($check) {                  if ($check) {
325                          tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;                          tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;
326                  } else {                  } else {
# Line 299  while (my $row = $sth->fetchrow_hashref) Line 328  while (my $row = $sth->fetchrow_hashref)
328                  }                  }
329          }          }
330    
331          print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";          print curr_time, " creating $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
         $curr_backup++;  
332    
333          my $t = time();          my $t = time();
334    

Legend:
Removed from v.265  
changed lines
  Added in v.279

  ViewVC Help
Powered by ViewVC 1.1.26