86 |
my $in_backup_increment; |
my $in_backup_increment; |
87 |
|
|
88 |
|
|
89 |
if ( !getopts("th:n:p:r:s:b:w:vd", \%opts) ) { |
if ( !getopts("th:n:p:r:s:b:w:vdf", \%opts) ) { |
90 |
print STDERR <<EOF; |
print STDERR <<EOF; |
91 |
usage: $0 [options] |
usage: $0 [options] |
92 |
Required options: |
Required options: |
102 |
-p pathAdd new path prefix |
-p pathAdd new path prefix |
103 |
-b BLOCKS BLOCKS x 512 bytes per record (default 20; same as tar) |
-b BLOCKS BLOCKS x 512 bytes per record (default 20; same as tar) |
104 |
-w writeBufSz write buffer size (default 1048576 = 1MB) |
-w writeBufSz write buffer size (default 1048576 = 1MB) |
105 |
|
-f overwrite existing parts |
106 |
-v verbose output |
-v verbose output |
107 |
-d debug output |
-d debug output |
108 |
EOF |
EOF |
162 |
inc_size = ?, |
inc_size = ?, |
163 |
parts = ?, |
parts = ?, |
164 |
inc_deleted = false |
inc_deleted = false |
165 |
where id = ? }); |
where id = ? |
166 |
|
}); |
167 |
my $sth_backup_parts = $dbh->prepare(qq{ |
my $sth_backup_parts = $dbh->prepare(qq{ |
168 |
insert into backup_parts ( |
insert into backup_parts ( |
169 |
backup_id, |
backup_id, |
199 |
# |
# |
200 |
|
|
201 |
my $max_file_size = $Conf{'MaxArchiveFileSize'} || die "problem with MaxArchiveFileSize parametar"; |
my $max_file_size = $Conf{'MaxArchiveFileSize'} || die "problem with MaxArchiveFileSize parametar"; |
|
$max_file_size *= 1024; |
|
202 |
|
|
203 |
my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir}; |
my $tar_dir = $Conf{InstallDir}.'/'.$Conf{GzipTempDir}; |
204 |
die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir); |
die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir && -w $tar_dir); |
205 |
|
|
206 |
my $tar_file = BackupPC::SearchLib::getGzipName($Host, $ShareName, $Num) || die "can't getGzipName($Host, $ShareName, $Num)"; |
my $tar_file = BackupPC::SearchLib::getGzipName($Host, $ShareName, $Num) || die "can't getGzipName($Host, $ShareName, $Num)"; |
207 |
|
|
208 |
my $tar_path = $tar_dir . '/' . $tar_file . '.tmp'; |
my $tar_path_final = $tar_dir . '/' . $tar_file; |
209 |
|
my $tar_path = $tar_path_final . '.tmp'; |
210 |
|
|
211 |
$tar_path =~ s#//#/#g; |
$tar_path =~ s#//#/#g; |
212 |
|
|
213 |
my $sth = $dbh->prepare(qq{ |
my $sth = $dbh->prepare(qq{ |
222 |
my ($backup_id) = $sth->fetchrow_array; |
my ($backup_id) = $sth->fetchrow_array; |
223 |
$sth->finish; |
$sth->finish; |
224 |
|
|
225 |
|
|
226 |
|
# delete exising backup_parts |
227 |
|
my $sth_delete_backup_parts = $dbh->prepare(qq{ |
228 |
|
delete from backup_parts |
229 |
|
where backup_id = ? |
230 |
|
}); |
231 |
|
$sth_delete_backup_parts->execute($backup_id); |
232 |
|
|
233 |
|
|
234 |
print STDERR "backup_id: $backup_id working dir: $tar_dir, max uncompressed size $max_file_size bytes, tar $tar_file\n" if ($opts{d}); |
print STDERR "backup_id: $backup_id working dir: $tar_dir, max uncompressed size $max_file_size bytes, tar $tar_file\n" if ($opts{d}); |
235 |
|
|
236 |
|
if (-e $tar_path_final) { |
237 |
|
if ($opts{f}) { |
238 |
|
rmtree $tar_path_final || die "can't remove $tar_path_final: $!"; |
239 |
|
} else { |
240 |
|
die "$tar_path_final allready exists\n"; |
241 |
|
} |
242 |
|
} |
243 |
|
|
244 |
my $fh; |
my $fh; |
245 |
my $part = 0; |
my $part = 0; |
252 |
if ($fh) { |
if ($fh) { |
253 |
return if ($current_tar_size == 0); |
return if ($current_tar_size == 0); |
254 |
|
|
255 |
print STDERR "# closing part $part\n" if ($opts{d}); |
print STDERR "\n\t+ $part:"; |
256 |
|
|
257 |
# |
# |
258 |
# Finish with two null 512 byte headers, |
# Finish with two null 512 byte headers, |
271 |
|
|
272 |
my $size = (stat( $file . '.tar.gz' ))[7] || die "can't stat ${file}.tar.gz"; |
my $size = (stat( $file . '.tar.gz' ))[7] || die "can't stat ${file}.tar.gz"; |
273 |
|
|
274 |
|
print "$file, $size bytes, $items_in_part items"; |
275 |
|
|
276 |
$sth_backup_parts->execute( |
$sth_backup_parts->execute( |
277 |
$backup_id, |
$backup_id, |
278 |
$part, |
$part, |
282 |
$items_in_part, |
$items_in_part, |
283 |
); |
); |
284 |
|
|
285 |
$total_increment_size += int( ( $size + 1023 ) / 1024 ) * 1024; |
$total_increment_size += $size; |
286 |
|
|
287 |
if ($arg->{close}) { |
if ($arg->{close}) { |
288 |
print STDERR "# close last part\n" if ($opts{d}); |
|
289 |
|
sub move($$) { |
290 |
|
my ($from,$to) = @_; |
291 |
|
print STDERR "# rename $from -> $to\n" if ($opts{d}); |
292 |
|
rename $from, $to || die "can't move $from -> $to: $!\n"; |
293 |
|
} |
294 |
|
|
295 |
|
if ($part == 1) { |
296 |
|
print STDERR " single" if ($opts{v}); |
297 |
|
move("${tar_path}/1.tar.gz", "${tar_path_final}.tar.gz"); |
298 |
|
move("${tar_path}/1.md5", "${tar_path_final}.md5"); |
299 |
|
rmtree $tar_path or die "can't remove temporary dir $tar_path: $!"; |
300 |
|
} else { |
301 |
|
print STDERR " [last]" if ($opts{v}); |
302 |
|
move("${tar_path}", "${tar_path_final}"); |
303 |
|
|
304 |
|
# if this archive was single part, remove it |
305 |
|
foreach my $suffix (qw/.tar.gz .md5/) { |
306 |
|
my $path = $tar_path_final . $suffix; |
307 |
|
unlink $path if (-e $path); |
308 |
|
} |
309 |
|
} |
310 |
|
|
311 |
$sth_inc_size->execute( |
$sth_inc_size->execute( |
312 |
$total_increment_size, |
$total_increment_size, |
313 |
$part, |
$part, |
314 |
$backup_id |
$backup_id |
315 |
); |
); |
316 |
|
|
317 |
|
print "\n\ttotal $total_increment_size bytes"; |
318 |
|
|
319 |
return; |
return; |
320 |
} |
} |
321 |
|
|
326 |
# if this is first part, create directory |
# if this is first part, create directory |
327 |
|
|
328 |
if ($part == 1) { |
if ($part == 1) { |
329 |
if (-d $tar_path) { |
if (-e $tar_path) { |
330 |
print STDERR "# deleting existing $tar_path\n" if ($opts{d}); |
print STDERR "# deleting existing $tar_path\n" if ($opts{d}); |
331 |
rmtree($tar_path); |
rmtree($tar_path); |
332 |
} |
} |
333 |
mkdir($tar_path) || die "can't create directory $tar_path: $!"; |
mkdir($tar_path) || die "can't create directory $tar_path: $!"; |
334 |
|
|
335 |
sub abort_cleanup { |
sub abort_cleanup { |
336 |
print STDERR "ABORTED: cleanup temp dir"; |
print STDERR "ABORTED: cleanup temp dir "; |
337 |
rmtree($tar_path); |
rmtree($tar_path); |
338 |
$dbh->rollback; |
$dbh->rollback; |
339 |
exit 1; |
exit 1; |
371 |
if (seedCache($Host, $ShareName, $Num)) { |
if (seedCache($Host, $ShareName, $Num)) { |
372 |
archiveWrite($fh, '/'); |
archiveWrite($fh, '/'); |
373 |
archiveWriteHardLinks($fh); |
archiveWriteHardLinks($fh); |
374 |
|
new_tar_part( close => 1 ); |
375 |
} else { |
} else { |
376 |
print STDERR "NOTE: no files found for $Host:$ShareName, increment $Num\n" if ($opts{v}); |
print STDERR "NOTE: no files found for $Host:$ShareName, increment $Num\n" if ($opts{v}); |
377 |
$no_files = 1; |
# remove temporary files if there are no files |
378 |
} |
rmtree($tar_path); |
379 |
|
|
380 |
new_tar_part( close => 1 ); |
my $sth = $dbh->prepare(qq{ |
381 |
|
update backups set inc_size = 0, parts = 0, inc_deleted = true |
382 |
|
where id = ? |
383 |
|
}); |
384 |
|
$sth->execute($backup_id); |
385 |
|
|
|
# remove temporary files if there are no files |
|
|
if ($no_files) { |
|
|
rmtree($tar_path); |
|
|
} elsif ($part == 1) { |
|
|
warn "FIXME: if there is only one part move to parent directory and rename"; |
|
386 |
} |
} |
387 |
|
|
388 |
# |
# |
603 |
sub seedCache($$$) { |
sub seedCache($$$) { |
604 |
my ($host, $share, $dumpNo) = @_; |
my ($host, $share, $dumpNo) = @_; |
605 |
|
|
606 |
print STDERR curr_time(), "getting files for $host:$share increment $dumpNo..." if ($opts{v}); |
print STDERR curr_time(), "$host:$share #$dumpNo" if ($opts{v}); |
607 |
my $sql = q{ |
my $sql = q{ |
608 |
SELECT path,size |
SELECT path,size |
609 |
FROM files |
FROM files |
615 |
my $sth = $dbh->prepare($sql); |
my $sth = $dbh->prepare($sql); |
616 |
$sth->execute($host, $share, $dumpNo); |
$sth->execute($host, $share, $dumpNo); |
617 |
my $count = $sth->rows; |
my $count = $sth->rows; |
618 |
print STDERR " found $count items\n" if ($opts{v}); |
print STDERR " $count items, parts:" if ($opts{v}); |
619 |
while (my $row = $sth->fetchrow_arrayref) { |
while (my $row = $sth->fetchrow_arrayref) { |
620 |
#print STDERR "+ ", $row->[0],"\n"; |
#print STDERR "+ ", $row->[0],"\n"; |
621 |
$in_backup_increment->{ $row->[0] } = $row->[1]; |
$in_backup_increment->{ $row->[0] } = $row->[1]; |
663 |
# is this file too large to fit into MaxArchiveFileSize? |
# is this file too large to fit into MaxArchiveFileSize? |
664 |
|
|
665 |
if ( ($current_tar_size + tar_overhead($tarPath) + $size) > $max_file_size ) { |
if ( ($current_tar_size + tar_overhead($tarPath) + $size) > $max_file_size ) { |
666 |
print STDERR "# tar file $current_tar_size + $tar_header_length + $size > $max_file_size, splitting\n" if ($opts{d}); |
print STDERR "# tar file $current_tar_size + $tar_header_length + $size > $max_file_size, splitting\n" if ($opts{d}); |
667 |
new_tar_part(); |
new_tar_part(); |
668 |
} |
} |
669 |
|
|