10 |
use Data::Dumper; |
use Data::Dumper; |
11 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
12 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
|
use BackupPC::SearchLib; |
|
13 |
use Cwd qw/abs_path/; |
use Cwd qw/abs_path/; |
14 |
use File::Which; |
use File::Which; |
15 |
use Archive::Tar; |
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"; |
39 |
|
my %Conf = $bpc->Conf(); |
40 |
|
|
41 |
|
use BackupPC::SearchLib; |
42 |
|
%BackupPC::SearchLib::Conf = %Conf; |
43 |
|
|
44 |
my $path = abs_path($0); |
my $path = abs_path($0); |
45 |
$path =~ s#/[^/]+$#/#; |
$path =~ s#/[^/]+$#/#; |
48 |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
die "can't find $tarIncCreate: $!\n" unless (-x $tarIncCreate); |
49 |
|
|
50 |
my $bin; |
my $bin; |
51 |
foreach my $c (qw/gzip split/) { |
foreach my $c (qw/gzip md5sum/) { |
52 |
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
53 |
} |
} |
54 |
|
|
64 |
|
|
65 |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
66 |
|
|
|
my $hosts; |
|
|
my $bpc = BackupPC::Lib->new || die; |
|
|
my %Conf = $bpc->Conf(); |
|
|
my $TopDir = $bpc->TopDir(); |
|
|
my $beenThere = {}; |
|
|
|
|
67 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
68 |
my $user = $Conf{SearchUser} || ''; |
my $user = $Conf{SearchUser} || ''; |
69 |
|
|
88 |
return strftime($t_fmt,localtime()); |
return strftime($t_fmt,localtime()); |
89 |
} |
} |
90 |
|
|
91 |
sub tar_join($) { |
my $hsn_cache; |
|
my $filename = shift; |
|
92 |
|
|
93 |
my $in = my $out = $filename; |
sub get_backup_id($$$) { |
94 |
$out .= '.tmp'; |
my ($host, $share, $num) = @_; |
95 |
|
|
96 |
# FIXME I should really order parts manually! |
my $key = "$host $share $num"; |
97 |
system("cat $in/part* > $out && rm -Rf $in && mv $out $in") == 0 or die "can't join $in: $?"; |
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 |
if (-d $filename) { |
sub check_part { |
149 |
print STDERR ", joining"; |
my ($host, $share, $num, $part_nr, $tar_size, $size, $md5, $items) = @_; |
150 |
tar_join($filename); |
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; |
187 |
|
|
188 |
|
if (-d "$tar_dir/$filename") { |
189 |
|
print ", multi-part"; |
190 |
|
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); |
192 |
|
closedir($dir); |
193 |
|
} else { |
194 |
|
push @tar_parts, "${filename}.tar.gz"; |
195 |
} |
} |
196 |
|
|
197 |
my $tar = Archive::Tar->new; |
print " [parts: ",join(", ", @tar_parts),"]" if ($opt{d}); |
198 |
my $comp = 0; |
|
199 |
$comp = 1 if ($filename =~ m/\.(gz|tgz)$/); |
my $same = 1; |
200 |
print STDERR ", opening" if ($debug); |
my @tar_files; |
201 |
$tar->read($filename, $comp) or die "can't open $filename: $!"; |
|
202 |
|
my $backup_part; |
203 |
|
|
204 |
|
print " reading" if ($opt{d}); |
205 |
|
|
206 |
|
foreach my $tarfilename (@tar_parts) { |
207 |
|
|
208 |
|
print "\n\t- $tarfilename"; |
209 |
|
|
210 |
|
my $path = "$tar_dir/$tarfilename"; |
211 |
|
|
212 |
|
my $size = (stat( $path ))[7] || die "can't stat $path: $!"; |
213 |
|
|
214 |
|
if ($size > $Conf{MaxArchiveSize}) { |
215 |
|
print ", part bigger than media $size > $Conf{MaxArchiveSize}\n"; |
216 |
|
return 0; |
217 |
|
} |
218 |
|
|
219 |
|
print ", $size bytes"; |
220 |
|
|
221 |
|
|
222 |
|
open(my $fh, "gzip -cd $path |") or die "can't open $path: $!"; |
223 |
|
binmode($fh); |
224 |
|
my $tar = Archive::Tar::Streamed->new($fh); |
225 |
|
|
226 |
|
my $tar_size_inarc = 0; |
227 |
|
my $items = 0; |
228 |
|
|
229 |
|
while(my $entry = $tar->next) { |
230 |
|
push @tar_files, $entry->name; |
231 |
|
$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 |
print STDERR ", tar" if ($debug); |
} |
240 |
my @tar_files = sort $tar->list_files(); |
|
241 |
print STDERR " ",($#tar_files + 1), " files" if ($debug); |
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 |
print STDERR ", database" if ($debug); |
$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; |
298 |
|
return $same unless($same); |
299 |
|
|
300 |
|
@tar_files = sort @tar_files; |
301 |
|
print "\n\t",($#tar_files + 1), " tar files"; |
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 |
|
|
|
my $same = 1; |
|
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 STDERR " ",($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 |
} |
} |
361 |
|
|
362 |
} ); |
} ); |
363 |
|
|
|
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 = ? }); |
|
|
|
|
|
%BackupPC::SearchLib::Conf = %Conf; |
|
|
|
|
364 |
$sth->execute(); |
$sth->execute(); |
365 |
my $num_backups = $sth->rows; |
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) { |
|
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
|
|
|
|
|
# this will return -1 if file doesn't exist |
|
|
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
|
369 |
|
|
|
print curr_time, " $curr_backup/$num_backups ", $row->{'host'}, ":", $row->{'share'}, " #", $row->{'num'}, " -> $tar_file"; |
|
370 |
$curr_backup++; |
$curr_backup++; |
371 |
|
|
372 |
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 > 45) { |
|
373 |
|
|
374 |
my $max_size = $Conf{'MaxArchiveSize'} || die "problem with MaxArchieSize parametar"; |
# this will return -1 if file doesn't exist |
375 |
$max_size *= 1024; # convert to bytes |
my $size = BackupPC::SearchLib::get_tgz_size_by_name($tar_file); |
376 |
|
|
377 |
my $parts = int( ($size + $max_size - 1) / $max_size ); |
print "# size: $size backup.size: ", $row->{inc_size},"\n" if ($opt{d}); |
378 |
|
|
379 |
if (-d "$tar_dir/$tar_file" && $parts != $row->{'parts'}) { |
if ( $row->{'inc_size'} != -1 && $size != -1 && $row->{'inc_size'} >= $size) { |
380 |
print " join"; |
if ($check) { |
381 |
tar_join("$tar_dir/$tar_file"); |
tar_check($row->{'host'}, $row->{'share'}, $row->{'num'}, $tar_file) && next; |
382 |
|
} else { |
383 |
|
next; |
384 |
} |
} |
385 |
|
} |
386 |
|
|
387 |
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: $!"; |
|
388 |
|
|
389 |
my $suffix_len = length("$parts"); |
my $t = time(); |
|
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: $!"; |
|
|
} |
|
|
|
|
|
$sth_inc_size->execute($size, $parts, $row->{'backup_id'}); |
|
|
$sth_inc_deleted->execute(0, $row->{'backup_id'}); |
|
390 |
|
|
391 |
printf(" %1.2f MB", ($size / 1024 / 1024)); |
# re-create archive? |
392 |
|
my $cmd = qq{ $tarIncCreate -h "$row->{'host'}" -s "$row->{'share'}" -n $row->{'num'} -f }; |
393 |
|
print STDERR "## $cmd\n" if ($debug); |
394 |
|
|
395 |
} else { |
if (system($cmd) != 0) { |
396 |
$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"; |
|
397 |
} |
} |
398 |
|
|
399 |
print ", dur: ",fmt_time(time() - $t), "\n"; |
print ", dur: ",fmt_time(time() - $t), "\n"; |
400 |
|
|
401 |
$dbh->commit; |
$dbh->commit; |