/[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 256 by dpavlin, Mon Dec 12 20:59:53 2005 UTC revision 289 by dpavlin, Wed Jan 18 15:16:31 2006 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    use File::Pid;
20    
21    my $pid_path = abs_path($0);
22    $pid_path =~ s/\W+/_/g;
23    
24    my $pidfile = new File::Pid({
25            file => "/tmp/$pid_path",
26    });
27    
28    if (my $pid = $pidfile->running ) {
29            die "$0 already running: $pid\n";
30    } elsif ($pidfile->pid ne $$) {
31            $pidfile->remove;
32            $pidfile = new File::Pid;
33    }
34    
35    print STDERR "$0 using pid ",$pidfile->pid," file ",$pidfile->file,"\n";
36    $pidfile->write;
37    
38  my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";  my $bpc = BackupPC::Lib->new || die "can't create BackupPC::Lib";
39  my %Conf = $bpc->Conf();  my %Conf = $bpc->Conf();
# Line 69  sub curr_time { Line 88  sub curr_time {
88          return strftime($t_fmt,localtime());          return strftime($t_fmt,localtime());
89  }  }
90    
91    my $hsn_cache;
92    
93    sub get_backup_id($$$) {
94            my ($host, $share, $num) = @_;
95    
96            my $key = "$host $share $num";
97            return $hsn_cache->{$key} if ($hsn_cache->{$key});
98    
99            my $sth = $dbh->prepare(qq{
100                    SELECT
101                            backups.id
102                    FROM backups
103                    INNER JOIN shares       ON backups.shareID=shares.ID
104                    INNER JOIN hosts        ON backups.hostID = hosts.ID
105                    where hosts.name = ? and shares.name = ? and backups.num = ?
106            });
107            $sth->execute($host, $share, $num);
108            my ($id) = $sth->fetchrow_array;
109    
110            $hsn_cache->{"$host $share $num"} = $id;
111    
112            print STDERR "# $host $share $num == $id\n" if ($opt{d});
113    
114            return $id;
115    }
116    
117    
118  sub tar_check($$$$) {  sub tar_check($$$$) {
119          my ($host,$share,$num,$filename) = @_;          my ($host,$share,$num,$filename) = @_;
120    
121          if ($debug) {          my $t = time();
122                  print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename";          print curr_time, " check $host:$share#$num -> $filename";
123          } else {  
124                  print " check";          # depending on expected returned value this is used like:
125            # my $uncompress_size = get_gzip_size('/full/path/to.gz');
126            # my ($compress_size, $uncompress_size) = get_gzip_size('/path.gz');
127            sub get_gzip_size($) {
128                    my $filename = shift;
129                    die "file $filename problem: $!" unless (-r $filename);
130                    open(my $gzip, $bin->{gzip}." -l $filename |") || die "can't gzip -l $filename: $!";
131                    my $line = <$gzip>;
132                    chomp($line);
133                    $line = <$gzip> if ($line =~ /^\s+compressed/);
134    
135                    my ($comp, $uncomp) = (0,0);
136    
137                    if ($line =~ m/^\s+(\d+)\s+(\d+)\s+\d+\.\d+/) {
138                            if (wantarray) {
139                                    return [ $1, $2 ];
140                            } else {
141                                    return $2;
142                            }
143                    } else {
144                            die "can't find size in line: $line";
145                    }
146            }
147    
148            sub check_part {
149                    my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_;
150                    my $backup_id = get_backup_id($host, $share, $num);
151                    my $sth_md5 = $dbh->prepare(qq{
152                            select
153                                    id, tar_size, size, md5, items
154                            from backup_parts
155                            where backup_id = ? and part_nr = ?
156                    });
157    
158                    $sth_md5->execute($backup_id, $part_nr);
159    
160                    if (my $row = $sth_md5->fetchrow_hashref) {
161                            return if (
162                                    $row->{tar_size} >= $tar_size &&
163                                    $row->{size} == $size &&
164                                    $row->{md5} eq $md5 &&
165                                    $row->{items} == $items
166                            );
167                            print ", deleting invalid backup_parts $row->{id}";
168                            $dbh->do(qq{ delete from backup_parts where id = $row->{id} });
169                    }
170                    print ", inserting new";
171                    my $sth_insert = $dbh->prepare(qq{
172                            insert into backup_parts (
173                                    backup_id,
174                                    part_nr,
175                                    tar_size,
176                                    size,
177                                    md5,
178                                    items
179                            ) values (?,?,?,?,?,?)
180                    });
181    
182                    $sth_insert->execute($backup_id, $part_nr, $tar_size, $size, $md5, $items);
183                    $dbh->commit;
184          }          }
185    
186          my @tar_parts;          my @tar_parts;
187    
188          if (-d "$tar_dir/$filename") {          if (-d "$tar_dir/$filename") {
189                  print STDERR " multi-part" if ($opt{d});                  print ", multi-part";
190                  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: $!";
191                  @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);
192                  closedir($dir);                  closedir($dir);
# Line 94  sub tar_check($$$$) { Line 199  sub tar_check($$$$) {
199          my $same = 1;          my $same = 1;
200          my @tar_files;          my @tar_files;
201    
202          print " reading";          my $backup_part;
203    
204            print " reading" if ($opt{d});
205    
206          foreach my $tarfilename (@tar_parts) {          foreach my $tarfilename (@tar_parts) {
207    
208                  print STDERR " $tarfilename" if ($debug);                  print "\n\t- $tarfilename";
209    
210                  my $path = "$tar_dir/$tarfilename";                  my $path = "$tar_dir/$tarfilename";
211                  my $md5 = $path;  
212                  $md5 =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5";                  my $size = (stat( $path ))[7] || die "can't stat $path: $!";
213                  if (! -e $md5) {  
214                          print ", creating md5";                  if ($size > $Conf{MaxArchiveSize}) {
215                          system( $bin->{md5sum} . " $path > $md5") == 0 or die "can't create md5 $path: $!";                          print ", part bigger than media $size > $Conf{MaxArchiveSize}\n";
216                            return 0;
217                  }                  }
218    
219                  open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!";                  print ", $size bytes";
220    
221    
222                    open(my $fh, "gzip -cd $path |") or die "can't open $path: $!";
223                  binmode($fh);                  binmode($fh);
224                  my $tar = Archive::Tar::Streamed->new($fh);                  my $tar = Archive::Tar::Streamed->new($fh);
225    
226                  my $total_size = 0;                  my $tar_size_inarc = 0;
227                    my $items = 0;
228    
229                  while(my $entry = $tar->next) {                  while(my $entry = $tar->next) {
230                          push @tar_files, $entry->name;                          push @tar_files, $entry->name;
231                          $total_size += $entry->size;                          $items++;
232                            $tar_size_inarc += $entry->size;
233    
234                            if ($tar_size_inarc > $Conf{MaxArchiveFileSize}) {
235                                    print ", part $tarfilename is too big $tar_size_inarc > $Conf{MaxArchiveFileSize}\n";
236                                    return 0;
237                            }
238    
239                  }                  }
240    
241                  if ($total_size > $Conf{MaxArchiveFileSize}) {                  close($fh);
242                          print STDERR " part too big $total_size > $Conf{MaxArchiveFileSize} }}" if ($debug);  
243                          $same = 0;                  print ", $items items";
244                          last;  
245                  } elsif ($total_size > $Conf{MaxArchiveSize}) {                  if ($tar_size_inarc == 0 && $items == 0) {
246                          print STDERR " part bigger than media $total_size > $Conf{MaxArchiveSize} }}" if ($debug);                          print ", EMPTY tar\n";
247                          $same = 0;  
248                          last;                          my $backup_id = get_backup_id($host, $share, $num);
249    
250                            my $sth_inc_deleted = $dbh->prepare(qq{
251                                    update backups set
252                                            inc_deleted = true
253                                    where id = ?
254                            });
255                            $sth_inc_deleted->execute($backup_id);
256    
257                            $dbh->commit;
258    
259                            return 1;
260                    }
261    
262                    my $tar_size = get_gzip_size( $path );
263    
264                    # real tar size is bigger because of padding    
265                    if ($tar_size_inarc > $tar_size) {
266                            print ", size of files in tar ($tar_size_inarc) bigger than whole tar ($tar_size)!\n";
267                            return 0;
268                  }                  }
269    
270                    #
271                    # check if md5 exists, and if not, create one
272                    #
273    
274                    my $md5_path = $path;
275                    $md5_path =~ s/\.tar\.gz$/.md5/ || die "can't create md5 filename from $md5_path";
276                    if (! -e $md5_path || -z $md5_path) {
277                            print ", creating md5";
278                            system( $bin->{md5sum} . " $path > $md5_path") == 0 or die "can't create md5 $path: $!";
279                    } else {
280                            ## FIXME check if existing md5 is valid
281                    }
282    
283                    my $md5 = read_file( $md5_path ) || die "can't read md5sum file $md5_path: $!";
284                    $md5 =~ s#\s.*$##;
285    
286                    # extract part number from filename
287                    my $part_nr = 1;
288                    $part_nr = $1 if ($tarfilename =~ m#/(\d+)\.tar\.gz#);
289    
290                    #
291                    # finally, check if backup_parts table in database is valid
292                    #
293    
294                    check_part($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items);
295          }          }
296    
297          # short-cut and exit;          # short-cut and exit;
298          return $same unless($same);          return $same unless($same);
299    
300          @tar_files = sort @tar_files;          @tar_files = sort @tar_files;
301          print STDERR " ",($#tar_files + 1), " files" if ($debug);          print "\n\t",($#tar_files + 1), " tar files";
   
         print STDERR ", database" if ($debug);  
302    
303          my $sth = $dbh->prepare(qq{          my $sth = $dbh->prepare(qq{
304                  SELECT path,type                  SELECT path,type
# Line 155  sub tar_check($$$$) { Line 317  sub tar_check($$$$) {
317                  push @db_files, $path;                  push @db_files, $path;
318          }          }
319    
320          print STDERR " ",($#db_files + 1), " files, diff" if ($debug);          print " ",($#db_files + 1), " database files, diff";
321    
322          @db_files = sort @db_files;          @db_files = sort @db_files;
323    
324          if ($#tar_files != $#db_files) {          if ($#tar_files != $#db_files) {
325                  $same = 0;                  $same = 0;
326                  print STDERR " NUMBER" if ($debug);                  print " NUMBER";
327          } else {          } else {
328                  my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);                  my $diff = Algorithm::Diff->new(\@tar_files, \@db_files);
329                  while ( $diff->Next() ) {                  while ( $diff->Next() ) {
# Line 172  sub tar_check($$$$) { Line 334  sub tar_check($$$$) {
334                  }                  }
335          }          }
336    
337          print " ",($same ? 'ok' : 'DIFFERENT');          print " ",($same ? 'ok' : 'DIFFERENT'),
338          print STDERR " }} " if ($debug);                  ", dur: ",fmt_time(time() - $t), "\n";
339    
340          return $same;          return $same;
341  }  }
# Line 204  my $num_backups = $sth->rows; Line 366  my $num_backups = $sth->rows;
366  my $curr_backup = 1;  my $curr_backup = 1;
367    
368  while (my $row = $sth->fetchrow_hashref) {  while (my $row = $sth->fetchrow_hashref) {
369    
370            $curr_backup++;
371    
372          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});          my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
373    
374          # this will return -1 if file doesn't exist          # this will return -1 if file doesn't exist
# Line 211  while (my $row = $sth->fetchrow_hashref) Line 376  while (my $row = $sth->fetchrow_hashref)
376    
377          print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});          print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d});
378    
379          if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} == $size) {          if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} >= $size) {
380                  if ($check) {                  if ($check) {
381                          tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;                          tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next;
382                  } else {                  } else {
# Line 219  while (my $row = $sth->fetchrow_hashref) Line 384  while (my $row = $sth->fetchrow_hashref)
384                  }                  }
385          }          }
386    
387          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++;  
388    
389          my $t = time();          my $t = time();
390    

Legend:
Removed from v.256  
changed lines
  Added in v.289

  ViewVC Help
Powered by ViewVC 1.1.26