/[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 230 by dpavlin, Tue Oct 25 09:30:52 2005 UTC revision 282 by dpavlin, Wed Dec 14 15:09:17 2005 UTC
# Line 15  use File::Which; Line 15  use File::Which;
15  use Archive::Tar::Streamed;  use Archive::Tar::Streamed;
16  use Algorithm::Diff;  use Algorithm::Diff;
17  use Getopt::Std;  use Getopt::Std;
18    use File::Slurp;
19    
20  my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";  my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";
21  my %Conf = $bpc->Conf();  my %Conf = $bpc->Conf();
# Line 22  my %Conf = $bpc->Conf(); Line 23  my %Conf = $bpc->Conf();
23  use BackupPC::SearchLib;  use BackupPC::SearchLib;
24  %BackupPC::SearchLib::Conf = %Conf;  %BackupPC::SearchLib::Conf = %Conf;
25    
 # cludge: minimum .tar.gz size  
 my $MIN_TAR_SIZE = 80;  
   
26  my $path = abs_path($0);  my $path = abs_path($0);
27  $path =~ s#/[^/]+$#/#;  $path =~ s#/[^/]+$#/#;
28  my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';  my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
# Line 32  my $tarIncCreate = $path .= 'BackupPC_ta Line 30  my $tarIncCreate = $path .= 'BackupPC_ta
30  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);  die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate);
31    
32  my $bin;  my $bin;
33  foreach my $c (qw/gzip split/) {  foreach my $c (qw/gzip md5sum/) {
34          $bin->{$c} = which($c) || die "$0 needs $c, install it\n";          $bin->{$c} = which($c) || die "$0 needs $c, install it\n";
35  }  }
36    
# Line 72  sub curr_time { Line 70  sub curr_time {
70          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
71  }  }
72    
73  sub tar_join($) {  my $hsn_cache;
74          my $filename = shift;  
75    sub get_backup_id($$$) {
76            my ($host, $share, $num) = @_;
77    
78            my $key = "$host $share $num";
79            return $hsn_cache->{$key} if ($hsn_cache->{$key});
80    
81          my $in = my $out = $filename;          my $sth = $dbh->prepare(qq{
82          $out .= '.tmp';                  SELECT
83                            backups.id
84                    FROM backups
85                    INNER JOIN shares       ON backups.shareID=shares.ID
86                    INNER JOIN hosts        ON backups.hostID = hosts.ID
87                    where hosts.name = ? and shares.name = ? and backups.num = ?
88            });
89            $sth->execute($host, $share, $num);
90            my ($id) = $sth->fetchrow_array;
91    
92          # FIXME I should really order parts manually!          $hsn_cache->{"$host $share $num"} = $id;
         system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?";  
93    
94            print STDERR "# $host $share $num == $id\n" if ($opt{d});
95    
96            return $id;
97  }  }
98    
99    
100  sub tar_check($$$$) {  sub tar_check($$$$) {
101          my ($host,$share,$num,$filename) = @_;          my ($host,$share,$num,$filename) = @_;
102    
103          if ($debug) {          my $t = time();
104                  print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";          print curr_time, " check $host:$share#$num -> $filename";
105          } else {  
106                  print " check";          # depending on expected returned value this is used like:
107            # my $uncompress_size = get_gzip_size('/full/path/to.gz');
108            # my ($compress_size, $uncompress_size) = get_gzip_size('/path.gz');
109            sub get_gzip_size($) {
110                    my $filename = shift;
111                    die "file $filename problem: $!" unless (-r $filename);
112                    open(my $gzip, $bin->{gzip}." -l $filename |") || die "can't gzip -l $filename: $!";
113                    my $line = <$gzip>;
114                    chomp($line);
115                    $line = <$gzip> if ($line =~ /^\s+compressed/);
116    
117                    my ($comp, $uncomp) = (0,0);
118    
119                    if ($line =~ m/^\s+(\d+)\s+(\d+)\s+\d+\.\d+/) {
120                            if (wantarray) {
121                                    return [ $1, $2 ];
122                            } else {
123                                    return $2;
124                            }
125                    } else {
126                            die "can't find size in line: $line";
127                    }
128            }
129    
130            sub check_part {
131                    my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_;
132                    my $backup_id = get_backup_id($host, $share, $num);
133                    my $sth_md5 = $dbh->prepare(qq{
134                            select
135                                    id, tar_size, size, md5, items
136                            from backup_parts
137                            where backup_id = ? and part_nr = ?
138                    });
139    
140                    $sth_md5->execute($backup_id, $part_nr);
141    
142                    if (my $row = $sth_md5->fetchrow_hashref) {
143                            return if (
144                                    $row->{tar_size} >= $tar_size &&
145                                    $row->{size} == $size &&
146                                    $row->{md5} eq $md5 &&
147                                    $row->{items} == $items
148                            );
149                            print ", deleting invalid backup_parts $row->{id}";
150                            $dbh->do(qq{ delete from backup_parts where id = $row->{id} });
151                    }
152                    print ", inserting new";
153                    my $sth_insert = $dbh->prepare(qq{
154                            insert into backup_parts (
155                                    backup_id,
156                                    part_nr,
157                                    tar_size,
158                                    size,
159                                    md5,
160                                    items
161                            ) values (?,?,?,?,?,?)
162                    });
163    
164                    $sth_insert->execute($backup_id, $part_nr, $tar_size, $size, $md5, $items);
165                    $dbh->commit;
166          }          }
167    
168          if (-d $filename) {          my @tar_parts;
169                  print STDERR ", joining";  
170                  tar_join($filename);          if (-d "$tar_dir/$filename") {
171                    print ", multi-part";
172                    opendir(my $dir, "$tar_dir/$filename") || die "can't readdir $tar_dir/$filename: $!";
173                    @tar_parts = map { my $p = $_; $p =~ s#^#${filename}/#; $p } grep { !/^\./ && !/md5/ && -f "$tar_dir/$filename/$_" } readdir($dir);
174                    closedir($dir);
175            } else {
176                    push @tar_parts, "${filename}.tar.gz";
177          }          }
178    
179          print STDERR ", opening" if ($debug);          print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d});
         open(my $fh, "gzip -cd $filename |") or die "can't open $filename: $!";  
         binmode($fh);  
         my $tar = Archive::Tar::Streamed->new($fh);  
180    
181          print STDERR ", tar" if ($debug);          my $same = 1;
182          my @tar_files;          my @tar_files;
183          while(my $entry = $tar->next) {  
184                  push @tar_files, $entry->name;          my $backup_part;
185    
186            print " reading" if ($opt{d});
187    
188            foreach my $tarfilename (@tar_parts) {
189    
190                    print "\n\t- $tarfilename";
191    
192                    my $path = "$tar_dir/$tarfilename";
193    
194                    my $size = (stat( $path ))[7] || die "can't stat $path: $!";
195    
196                    if ($size > $Conf{MaxArchiveSize}) {
197                            print ", part bigger than media $size > $Conf{MaxArchiveSize}\n";
198                            return 0;
199                    }
200    
201                    print ", $size bytes";
202    
203    
204                    open(my $fh, "gzip -cd $path |") or die "can't open $path: $!";
205                    binmode($fh);
206                    my $tar = Archive::Tar::Streamed->new($fh);
207    
208                    my $tar_size_inarc = 0;
209                    my $items = 0;
210    
211                    while(my $entry = $tar->next) {
212                            push @tar_files, $entry->name;
213                            $items++;
214                            $tar_size_inarc += $entry->size;
215    
216                            if ($tar_size_inarc > $Conf{MaxArchiveFileSize}) {
217                                    print ", part $tarfilename is too big $tar_size_inarc > $Conf{MaxArchiveFileSize}\n";
218                                    return 0;
219                            }
220    
221                    }
222    
223                    close($fh);
224    
225                    print ", $items items";
226    
227                    if ($tar_size_inarc == 0 && $items == 0) {
228                            print ", EMPTY tar\n";
229    
230                            my $backup_id = get_backup_id($host, $share, $num);
231    
232                            my $sth_inc_deleted = $dbh->prepare(qq{
233                                    update backups set
234                                            inc_deleted = true
235                                    where id = ?
236                            });
237                            $sth_inc_deleted->execute($backup_id);
238    
239                            $dbh->commit;
240    
241                            return 1;
242                    }
243    
244                    my $tar_size = get_gzip_size( $path );
245    
246                    # real tar size is bigger because of padding    
247                    if ($tar_size_inarc > $tar_size) {
248                            print ", size of files in tar ($tar_size_inarc) bigger than whole tar ($tar_size)!\n";
249                            return 0;
250                    }
251    
252                    #
253                    # check if md5 exists, and if not, create one
254                    #
255    
256                    my $md5_path = $path;
257                    $md5_path =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5_path";
258                    if (! -e $md5_path || -z $md5_path) {
259                            print ", creating md5";
260                            system( $bin->{md5sum} . " $path > $md5_path") == 0 or die "can't create md5 $path: $!";
261                    } else {
262                            ## FIXME check if existing md5 is valid
263                    }
264    
265                    my $md5 = read_file( $md5_path ) || die "can't read md5sum file $md5_path: $!";
266                    $md5 =~ s#\s.*$##;
267    
268                    # extract part number from filename
269                    my $part_nr = 1;
270                    $part_nr = $1 if ($tarfilename =~ m#/(\d+)\.tar\.gz#);
271    
272                    #
273                    # finally, check if backup_parts table in database is valid
274                    #
275    
276                    check_part($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items);
277          }          }
         @tar_files = sort @tar_files;  
         print STDERR " ",($#tar_files + 1), " files" if ($debug);  
278    
279          print STDERR ", database" if ($debug);          # short-cut and exit;
280            return $same unless($same);
281    
282            @tar_files = sort @tar_files;
283            print "\n\t",($#tar_files + 1), " tar files";
284    
285          my $sth = $dbh->prepare(qq{          my $sth = $dbh->prepare(qq{
286                  SELECT path,type                  SELECT path,type
# Line 129  sub tar_check($$$$) { Line 299  sub tar_check($$$$) {
299                  push @db_files, $path;                  push @db_files, $path;
300          }          }
301    
302          print STDERR " ",($#db_files + 1), " files, diff" if ($debug);          print " ",($#db_files + 1), " database files, diff";
303    
304          @db_files = sort @db_files;          @db_files = sort @db_files;
305    
         my $same = 1;  
306          if ($#tar_files != $#db_files) {          if ($#tar_files != $#db_files) {
307                  $same = 0;                  $same = 0;
308                  print STDERR " NUMBER" if ($debug);                  print " NUMBER";
309          } else {          } else {
310                  my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);                  my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
311                  while ( $diff->Next() ) {                  while ( $diff->Next() ) {
# Line 147  sub tar_check($$$$) { Line 316  sub tar_check($$$$) {
316                  }                  }
317          }          }
318    
319          print " ",($same ? 'ok' : 'DIFFERENT');          print " ",($same ? 'ok' : 'DIFFERENT'),
320          print STDERR " }} " if ($debug);                  ", dur: ",fmt_time(time() - $t), "\n";
321    
322          return $same;          return $same;
323  }  }
# Line 174  order by backups.date Line 343  order by backups.date
343    
344  } );  } );
345    
 my $sth_inc_size = $dbh->prepare(qq{ update backups set inc_size = ?, parts = ? where id = ? });  
 my $sth_inc_deleted = $dbh->prepare(qq{ update backups set inc_deleted = ? where id = ? });  
   
   
346  $sth->execute();  $sth->execute();
347  my $num_backups = $sth->rows;  my $num_backups = $sth->rows;
348  my $curr_backup = 1;  my $curr_backup = 1;
349    
350  while (my $row = $sth->fetchrow_hashref) {  while (my $row = $sth->fetchrow_hashref) {
         my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});  
351    
         # this will return -1 if file doesn't exist  
         my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);  
   
         print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";  
352          $curr_backup++;          $curr_backup++;
353    
354          my $t = time();          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
   
         # re-create archive?  
         if ($row->{'inc_size'} == -1 || $size == -1 ||  
                 $row->{'inc_size'} != $size ||  
                 $check && ! tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, "$tar_dir/$tar_file")  
         ) {  
                 my $cmd = qq{rm -Rf $tar_dir/$tar_file && $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} | $bin->{'gzip'} $Conf{GzipLevel} > ${tar_dir}/${tar_file}.tmp};  
                 print STDERR "## $cmd\n" if ($debug);  
   
                 system($cmd) == 0 or die "failed: $?";  
   
                 rename("${tar_dir}/${tar_file}.tmp", "$tar_dir/$tar_file") or die "can't rename $tar_dir/$tar_file: $!";  
   
                 $size = (stat( "$tar_dir/$tar_file" ))[7];  
         }  
   
         if ($size > $MIN_TAR_SIZE) {  
   
                 my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchiveSize parametar";  
                 $max_size *= 1024;      # convert to bytes  
   
                 my $max_file_size = $Conf{'MaxArchiveFileSize'} || die "problem with MaxArchiveFileSize parametar";  
                 $max_file_size *= 1024; # bytes  
   
                 if ($max_file_size > $max_size) {  
                         warn "MaxArchiveFileSize ($max_file_size) is bigger than MaxArchiveSize ($max_size)\n";  
                         $max_file_size = $max_size;  
                 }  
355    
356                  # maximum file size on ISO image is 4Gb          # this will return -1 if file doesn't exist
357                  # this will require Linux kernel 2.6.8 or newer          my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file);
                 if ( $max_size > $max_file_size ) {  
                         $max_size = $max_file_size;  
                 }  
358    
359                  my $parts = int( ($size + $max_size - 1) / $max_size );          print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
360    
361                  if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) {          if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} >= $size) {
362                          print " join";                  if ($check) {
363                          tar_join("$tar_dir/$tar_file");                          tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;
364                    } else {
365                            next;
366                  }                  }
367            }
368    
369                  if ($size > $max_size && ! -d "$tar_dir/$tar_file") {          print curr_time, " creating $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file";
                         print " split/$parts";  
                         my $in = my $out = "$tar_dir/$tar_file";  
                         $out .= '.tmp';  
                         rename $in, $out || die "can't rename $in: $!";  
                         mkdir $in || die "can't mkdir $in: $!";  
   
                         my $suffix_len = length("$parts");  
                         system("$bin->{'split'} -d -b $max_size -a $suffix_len $out $in/part") == 0 or die "can't split $out: $?";  
                         unlink $out || die "can't unlink $out: $!";  
                 }  
370    
371                  $sth_inc_size->execute($size, $parts, $row->{'backup_id'});          my $t = time();
                 $sth_inc_deleted->execute(0, $row->{'backup_id'});  
372    
373                  printf(" %1.2f MB", ($size / 1024 / 1024));          # re-create archive?
374            my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f };
375            print STDERR "## $cmd\n" if ($debug);
376    
377          } else {          if (system($cmd) != 0) {
378                  $sth_inc_deleted->execute(1, $row->{'backup_id'});                  print STDERR " FAILED";
                 unlink "$tar_dir/$tar_file" || die "can't delete $tar_dir/$tar_file: $!\n";  
                 print " EMPTY";  
379          }          }
380    
381          print ", dur: ",fmt_time(time() - $t), "\n";          print ", dur: ",fmt_time(time() - $t), "\n";
382    
383          $dbh->commit;          $dbh->commit;

Legend:
Removed from v.230  
changed lines
  Added in v.282

  ViewVC Help
Powered by ViewVC 1.1.26