16 |
use Algorithm::Diff; |
use Algorithm::Diff; |
17 |
use Getopt::Std; |
use Getopt::Std; |
18 |
use File::Slurp; |
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(); |
118 |
sub tar_check($$$$) { |
sub tar_check($$$$) { |
119 |
my ($host,$share,$num,$filename) = @_; |
my ($host,$share,$num,$filename) = @_; |
120 |
|
|
121 |
|
my $t = time(); |
122 |
|
print curr_time, " check $host:$share#$num -> $filename"; |
123 |
|
|
124 |
|
# 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 { |
sub check_part { |
149 |
my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_; |
my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_; |
150 |
my $backup_id = get_backup_id($host, $share, $num); |
my $backup_id = get_backup_id($host, $share, $num); |
164 |
$row->{md5} eq $md5 && |
$row->{md5} eq $md5 && |
165 |
$row->{items} == $items |
$row->{items} == $items |
166 |
); |
); |
167 |
print STDERR "# deleting invalid row $row->{id}\n" if ($opt{d}); |
print ", deleting invalid backup_parts $row->{id}"; |
168 |
$dbh->do(qq{ delete from backup_parts where id = $row->{id} }); |
$dbh->do(qq{ delete from backup_parts where id = $row->{id} }); |
169 |
} |
} |
170 |
print STDERR "# inserting new backup_part row\n"; |
print ", inserting new"; |
171 |
my $sth_insert = $dbh->prepare(qq{ |
my $sth_insert = $dbh->prepare(qq{ |
172 |
insert into backup_parts ( |
insert into backup_parts ( |
173 |
backup_id, |
backup_id, |
183 |
$dbh->commit; |
$dbh->commit; |
184 |
} |
} |
185 |
|
|
|
if ($debug) { |
|
|
print STDERR " {{ CHECK: ${host}:${share}#${num} and $filename"; |
|
|
} else { |
|
|
print " check $filename"; |
|
|
} |
|
|
|
|
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); |
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 $size = (stat( "$tar_dir/$tarfilename" ))[7] || die "can't stat $tar_dir/$tarfilename"; |
my $path = "$tar_dir/$tarfilename"; |
211 |
|
|
212 |
|
my $size = (stat( $path ))[7] || die "can't stat $path: $!"; |
213 |
|
|
214 |
if ($size > $Conf{MaxArchiveSize}) { |
if ($size > $Conf{MaxArchiveSize}) { |
215 |
print STDERR " part bigger than media $size > $Conf{MaxArchiveSize} }}" if ($debug); |
print ", part bigger than media $size > $Conf{MaxArchiveSize}\n"; |
216 |
$same = 0; |
return 0; |
|
last; |
|
217 |
} |
} |
218 |
|
|
219 |
my $path = "$tar_dir/$tarfilename"; |
print ", $size bytes"; |
220 |
|
|
221 |
open(my $fh, "gzip -cd $tar_dir/$tarfilename |") or die "can't open $tar_dir/$tarfilename: $!"; |
|
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 $tar_size = 0; |
my $tar_size_inarc = 0; |
227 |
my $items = 0; |
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 |
$items++; |
$items++; |
232 |
$tar_size += $entry->size; |
$tar_size_inarc += $entry->size; |
233 |
|
|
234 |
if ($tar_size > $Conf{MaxArchiveFileSize}) { |
if ($tar_size_inarc > $Conf{MaxArchiveFileSize}) { |
235 |
print STDERR " part too big $tar_size > $Conf{MaxArchiveFileSize} }}" if ($debug); |
print ", part $tarfilename is too big $tar_size_inarc > $Conf{MaxArchiveFileSize}\n"; |
236 |
$same = 0; |
return 0; |
|
last; |
|
237 |
} |
} |
238 |
|
|
239 |
} |
} |
240 |
|
|
241 |
|
close($fh); |
242 |
|
|
243 |
|
print ", $items items"; |
244 |
|
|
245 |
|
if ($tar_size_inarc == 0 && $items == 0) { |
246 |
|
print ", EMPTY tar\n"; |
247 |
|
|
248 |
|
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 |
# check if md5 exists, and if not, create one |
272 |
# |
# |
276 |
if (! -e $md5_path || -z $md5_path) { |
if (! -e $md5_path || -z $md5_path) { |
277 |
print ", creating md5"; |
print ", creating md5"; |
278 |
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: $!"; |
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: $!"; |
my $md5 = read_file( $md5_path ) || die "can't read md5sum file $md5_path: $!"; |
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 |
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() ) { |
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 |
} |
} |
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 |
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 |
|
|