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 |
|
# 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 { |
sub check_part { |
131 |
my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_; |
my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_; |
132 |
my $backup_id = get_backup_id($host, $share, $num); |
my $backup_id = get_backup_id($host, $share, $num); |
146 |
$row->{md5} eq $md5 && |
$row->{md5} eq $md5 && |
147 |
$row->{items} == $items |
$row->{items} == $items |
148 |
); |
); |
149 |
print STDERR "# deleting invalid row $row->{id}\n" if ($opt{d}); |
print ", deleting invalid backup_parts $row->{id}"; |
150 |
$dbh->do(qq{ delete from backup_parts where id = $row->{id} }); |
$dbh->do(qq{ delete from backup_parts where id = $row->{id} }); |
151 |
} |
} |
152 |
print STDERR "# inserting new backup_part row\n"; |
print ", inserting new"; |
153 |
my $sth_insert = $dbh->prepare(qq{ |
my $sth_insert = $dbh->prepare(qq{ |
154 |
insert into backup_parts ( |
insert into backup_parts ( |
155 |
backup_id, |
backup_id, |
165 |
$dbh->commit; |
$dbh->commit; |
166 |
} |
} |
167 |
|
|
|
if ($debug) { |
|
|
print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename"; |
|
|
} else { |
|
|
print " check $filename"; |
|
|
} |
|
|
|
|
168 |
my @tar_parts; |
my @tar_parts; |
169 |
|
|
170 |
if (-d "$tar_dir/$filename") { |
if (-d "$tar_dir/$filename") { |
171 |
print STDERR " multi-part" if ($opt{d}); |
print ", multi-part"; |
172 |
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: $!"; |
173 |
@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); |
174 |
closedir($dir); |
closedir($dir); |
187 |
|
|
188 |
foreach my $tarfilename (@tar_parts) { |
foreach my $tarfilename (@tar_parts) { |
189 |
|
|
190 |
print STDERR " $tarfilename" if ($debug); |
print "\n\t- $tarfilename"; |
191 |
|
|
192 |
|
my $path = "$tar_dir/$tarfilename"; |
193 |
|
|
194 |
my $size = (stat( "$tar_dir/$tarfilename" ))[7] || die "can't stat $tar_dir/$tarfilename"; |
my $size = (stat( $path ))[7] || die "can't stat $path: $!"; |
195 |
|
|
196 |
if ($size > $Conf{MaxArchiveSize}) { |
if ($size > $Conf{MaxArchiveSize}) { |
197 |
print STDERR " part bigger than media $size > $Conf{MaxArchiveSize} }}" if ($debug); |
print ", part bigger than media $size > $Conf{MaxArchiveSize}\n"; |
198 |
$same = 0; |
return 0; |
|
last; |
|
199 |
} |
} |
200 |
|
|
201 |
my $path = "$tar_dir/$tarfilename"; |
print ", $size bytes"; |
202 |
|
|
203 |
open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!"; |
|
204 |
|
open(my $fh, "gzip -cd $path |") or die "can't open $path: $!"; |
205 |
binmode($fh); |
binmode($fh); |
206 |
my $tar = Archive::Tar::Streamed->new($fh); |
my $tar = Archive::Tar::Streamed->new($fh); |
207 |
|
|
208 |
my $tar_size = 0; |
my $tar_size_inarc = 0; |
209 |
my $items = 0; |
my $items = 0; |
210 |
|
|
211 |
while(my $entry = $tar->next) { |
while(my $entry = $tar->next) { |
212 |
push @tar_files, $entry->name; |
push @tar_files, $entry->name; |
213 |
$items++; |
$items++; |
214 |
$tar_size += $entry->size; |
$tar_size_inarc += $entry->size; |
215 |
|
|
216 |
if ($tar_size > $Conf{MaxArchiveFileSize}) { |
if ($tar_size_inarc > $Conf{MaxArchiveFileSize}) { |
217 |
print STDERR " part too big $tar_size > $Conf{MaxArchiveFileSize} }}" if ($debug); |
print ", part $tarfilename is too big $tar_size_inarc > $Conf{MaxArchiveFileSize}\n"; |
218 |
$same = 0; |
return 0; |
|
last; |
|
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 bigger than whole tar!\n"; |
249 |
|
return 0; |
250 |
|
} |
251 |
|
|
252 |
# |
# |
253 |
# check if md5 exists, and if not, create one |
# check if md5 exists, and if not, create one |
254 |
# |
# |
258 |
if (! -e $md5_path || -z $md5_path) { |
if (! -e $md5_path || -z $md5_path) { |
259 |
print ", creating md5"; |
print ", creating md5"; |
260 |
system( $bin->{md5sum} . " $path > $md5_path") == 0 or die "can't create md5 $path: $!"; |
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: $!"; |
my $md5 = read_file( $md5_path ) || die "can't read md5sum file $md5_path: $!"; |
280 |
return $same unless($same); |
return $same unless($same); |
281 |
|
|
282 |
@tar_files = sort @tar_files; |
@tar_files = sort @tar_files; |
283 |
print STDERR " ",($#tar_files + 1), " files" if ($debug); |
print "\n\t",($#tar_files + 1), " tar files"; |
|
|
|
|
print STDERR ", database" if ($debug); |
|
284 |
|
|
285 |
my $sth = $dbh->prepare(qq{ |
my $sth = $dbh->prepare(qq{ |
286 |
SELECT path,type |
SELECT path,type |
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 |
|
|
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() ) { |
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 |
} |
} |
348 |
my $curr_backup = 1; |
my $curr_backup = 1; |
349 |
|
|
350 |
while (my $row = $sth->fetchrow_hashref) { |
while (my $row = $sth->fetchrow_hashref) { |
351 |
|
|
352 |
|
$curr_backup++; |
353 |
|
|
354 |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
355 |
|
|
356 |
# this will return -1 if file doesn't exist |
# this will return -1 if file doesn't exist |
366 |
} |
} |
367 |
} |
} |
368 |
|
|
369 |
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++; |
|
370 |
|
|
371 |
my $t = time(); |
my $t = time(); |
372 |
|
|