10 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
11 |
use Term::Menus; |
use Term::Menus; |
12 |
use File::Which; |
use File::Which; |
13 |
|
use File::Path; |
14 |
|
|
15 |
use Data::Dumper; |
use Data::Dumper; |
16 |
|
|
17 |
my $debug = 0; |
my $debug = 0; |
18 |
|
# set this to 1 to prompt for DVD removal for each selected item. |
19 |
|
my $prompt_for_delete = 0; |
20 |
$|=1; |
$|=1; |
21 |
|
|
22 |
# don't check for user |
# don't check for user |
250 |
goto SKIP; |
goto SKIP; |
251 |
} |
} |
252 |
|
|
253 |
my $sth_archive_backup = $dbh->prepare( qq{ |
sub add_symlink($$) { |
254 |
|
my ($from, $to) = @_; |
255 |
|
symlink $from, $to || skip("can't symlink $from to $to: $!"); |
256 |
|
} |
257 |
|
|
258 |
|
sub delete_dvd($) { |
259 |
|
my $dvd_nr = shift || return; |
260 |
|
|
261 |
|
print "Do you want to delete DVD #$dvd_nr now? [NO/yes]: "; |
262 |
|
my $ok = <STDIN>; |
263 |
|
chomp($ok); |
264 |
|
if (lc($ok) eq 'yes') { |
265 |
|
print "Deleting DVD #$dvd_nr from database...\n"; |
266 |
|
|
267 |
|
$dbh->begin_work; |
268 |
|
|
269 |
|
my $sth_delete_dvd = $dbh->prepare( qq{ |
270 |
|
delete from archive where dvd_nr = ? |
271 |
|
} ); |
272 |
|
$sth_delete_dvd->execute( $dvd_nr ); |
273 |
|
$dbh->do( qq{ |
274 |
|
select setval('dvd_nr', (select max(dvd_nr) from archive), true) |
275 |
|
} ); |
276 |
|
|
277 |
|
# remove files for this DVD |
278 |
|
map { |
279 |
|
print "\tremoving $_\n"; |
280 |
|
unlink($_) || die "can't rm $_: $!"; |
281 |
|
} glob ( "/$iso_dir/$dvd_nr.*" ); |
282 |
|
|
283 |
|
$dbh->commit; |
284 |
|
|
285 |
|
return 1; |
286 |
|
} else { |
287 |
|
return 0; |
288 |
|
} |
289 |
|
} |
290 |
|
|
291 |
|
my $sth_archive_backup_parts = $dbh->prepare( qq{ |
292 |
select |
select |
293 |
backup_id, |
archive_backup.backup_id, |
294 |
archive_id, |
archive_id, |
295 |
hosts.name as host, |
hosts.name as host, |
296 |
shares.name as share, |
shares.name as share, |
297 |
backups.num as num, |
backups.num as num, |
298 |
backups.parts as parts |
backups.parts as parts, |
299 |
|
backup_parts.part_nr as part_nr, |
300 |
|
backup_parts.size as part_size, |
301 |
|
backup_parts.md5 as md5, |
302 |
|
backup_parts.items |
303 |
from archive_backup |
from archive_backup |
304 |
join archive on archive_id = archive.id |
join archive on archive_id = archive.id |
305 |
join backups on backup_id = backups.id |
join backups on archive_backup.backup_id = backups.id |
306 |
join hosts on hostid = hosts.id |
join hosts on hostid = hosts.id |
307 |
join shares on shareid = shares.id |
join shares on shareid = shares.id |
308 |
|
join backup_parts on archive_backup.backup_id = backup_parts.backup_id |
309 |
where archive.dvd_nr = ? |
where archive.dvd_nr = ? |
310 |
|
order by archive_backup.backup_id, backup_parts.part_nr |
311 |
}); |
}); |
312 |
|
|
313 |
my $sth_archive_burned = $dbh->prepare( qq{ |
my $sth_archive_burned = $dbh->prepare( qq{ |
328 |
|
|
329 |
print "Working on DVD #$dvd_nr in $tmp_dir\n"; |
print "Working on DVD #$dvd_nr in $tmp_dir\n"; |
330 |
|
|
331 |
my $part_path = ''; |
$sth_archive_backup_parts->execute($dvd_nr); |
332 |
my $part_nr = 1; |
|
333 |
my $parts = 0; |
if ($sth_archive_backup_parts->rows == 0) { |
334 |
|
warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n"; |
335 |
$sth_archive_backup->execute($dvd_nr); |
delete_dvd( $dvd_nr ); |
336 |
|
} |
337 |
|
|
338 |
|
if ($prompt_for_delete) { |
339 |
|
next if delete_dvd( $dvd_nr ); |
340 |
|
} |
341 |
|
|
342 |
my $disk_name = 'unknown disk'; |
my @volumes; |
343 |
my $iso_size = 0; |
my $v; # emtpy volume |
344 |
|
my $v_size = 0; |
345 |
|
|
346 |
|
my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize"; |
347 |
|
while (my $row = $sth_archive_backup_parts->fetchrow_hashref) { |
348 |
|
if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) { |
349 |
|
push @volumes, $v; |
350 |
|
$v = {}; |
351 |
|
} |
352 |
|
$v->{size} += $row->{part_size}; |
353 |
|
# this part |
354 |
|
my $p = { |
355 |
|
filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}), |
356 |
|
}; |
357 |
|
foreach my $fld (qw/part_nr md5/) { |
358 |
|
$p->{$fld} = $row->{$fld} || die "missing $fld in row!"; |
359 |
|
} |
360 |
|
push @{ $v->{parts} }, $p; |
361 |
|
} |
362 |
|
push @volumes, $v if ($v); |
363 |
|
|
364 |
do { |
#warn "# volumes: ",Dumper(\@volumes),"\n"; |
365 |
|
|
366 |
# do we need to re-execute query for multi-part increments? |
my $volumes = $#volumes + 1; |
367 |
$sth_archive_backup->execute($dvd_nr) if ($parts > 1); |
my $volume_nr = 1; |
368 |
|
|
369 |
my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row"; |
foreach my $v (@volumes) { |
370 |
|
|
371 |
|
#print Dumper($v); |
372 |
|
|
373 |
$parts = $row->{'parts'} || die "no parts?"; |
my $iso_size = 0; |
374 |
$disk_name = $dvd_nr; |
my $disk_name = $dvd_nr; |
375 |
|
# suffix added to multi-volume archives |
376 |
|
my $volume_suffix = ''; |
377 |
|
|
378 |
if ($parts > 1) { |
if ($volumes > 1) { |
379 |
# parts start with 0, so we have -1 here |
$volume_suffix = '_' . $volume_nr; |
380 |
$part_path = '.' . ($part_nr - 1); |
$disk_name .= ' ' . $volume_nr . '/' . $volumes; |
|
$disk_name .= '.' . $part_nr; |
|
381 |
} |
} |
382 |
|
|
383 |
print "Processing part $part_nr/$parts\n"; |
print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n"; |
384 |
|
|
385 |
my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}"; |
my $iso_file = my $xml_file = my $stage = |
386 |
$list_file .= $part_path . '.list'; |
"${iso_dir}/${dvd_nr}"; |
387 |
$iso_file .= $part_path . '.iso'; |
|
388 |
|
$iso_file .= $volume_suffix . '.iso'; |
389 |
$xml_file .= '.xml'; |
$xml_file .= '.xml'; |
390 |
|
|
391 |
|
$stage .= $volume_suffix . '.stage'; |
392 |
|
|
393 |
|
my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5"; |
394 |
|
|
395 |
# |
# |
396 |
# check if ISO file exists |
# check if ISO file exists |
397 |
# |
# |
398 |
|
|
399 |
if (! -e $iso_file) { |
if (! -e $iso_file) { |
400 |
|
|
401 |
open(my $list, "> $list_file") || skip "can't open $list_file: $!"; |
# create stage directory |
402 |
|
if (-e $stage) { |
403 |
|
rmtree($stage) || die "can't remove $stage: $!"; |
404 |
|
} |
405 |
|
mkpath($stage); |
406 |
|
|
407 |
|
# open file for md5sums |
408 |
|
open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!"; |
409 |
|
|
410 |
|
my $parts_on_this_volume = 0; |
411 |
|
|
412 |
my $inc = 0; |
foreach my $p (@{ $v->{parts} }) { |
413 |
|
my $tar_file = $p->{filename} || die "no filename in part", Dumper($p); |
414 |
|
my $rel_path = $tar_file; |
415 |
|
|
416 |
do { |
if (-d "$tar_dir/$rel_path") { |
417 |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path"); |
418 |
if (-f "$tar_dir/$tar_file") { |
$rel_path .= '/' . $p->{part_nr}; |
|
skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file"); |
|
|
print $list "$tar_dir/$tar_file\n"; |
|
|
} else { |
|
|
my $len = length("$parts"); |
|
|
my $nr = sprintf("%0${len}d", ($part_nr - 1)); |
|
|
print $list "$tar_dir/$tar_file/part$nr\n"; |
|
419 |
} |
} |
420 |
$inc++; |
$rel_path .= '.tar.gz'; |
421 |
} while ($row = $sth_archive_backup->fetchrow_hashref); |
|
422 |
|
skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path"); |
423 |
|
|
424 |
|
add_symlink("$tar_dir/$rel_path", "$stage/$rel_path"); |
425 |
|
|
426 |
|
my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p); |
427 |
|
chomp($md5sum); |
428 |
|
print $md5 "$md5sum $rel_path\n" || die "can't write md5sum: $!"; |
429 |
|
|
430 |
|
$parts_on_this_volume++; |
431 |
|
} |
432 |
|
|
433 |
# add file list and note in xml |
# add file list and note in xml |
434 |
dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file); |
dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file); |
435 |
print $list "$xml_file\n"; |
|
436 |
|
add_symlink($xml_file, "$stage/${dvd_nr}.xml"); |
437 |
|
|
438 |
# add css file for archive |
# add css file for archive |
439 |
my $css_file = $Conf{CgiImageDir} . '/archive.css'; |
my $css_file = $Conf{CgiImageDir} . '/archive.css'; |
440 |
if (-r $css_file) { |
if (-r $css_file) { |
441 |
print $list "$css_file\n"; |
add_symlink($css_file, "$stage/archive.css"); |
442 |
} else { |
} else { |
443 |
print "WARNING: missing $css_file, not added to iso image!\n"; |
print "WARNING: missing $css_file, not added to iso image!\n"; |
444 |
} |
} |
445 |
|
|
446 |
close($list); |
print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n"; |
447 |
|
|
448 |
print "Running mkisofs now for $inc increments, disk $disk_name\n"; |
my $cmd = $bin->{'mkisofs'} . qq{ -A BackupPC -gui -J -r -T --input-charset ISO-8859-2 -V "$disk_name" -o ${iso_file}.tmp -f $stage }; |
|
|
|
|
my $cmd = $bin->{'mkisofs'} . qq{ -A BackupPC -gui -J -r -T --input-charset ISO-8859-2 -V $disk_name -o ${iso_file}.tmp -path-list $list_file }; |
|
449 |
|
|
450 |
system($cmd) == 0 or skip "can't run $cmd: $?"; |
system($cmd) == 0 or skip "can't run $cmd: $?"; |
451 |
|
|
455 |
|
|
456 |
print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n"; |
print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n"; |
457 |
|
|
458 |
|
rmtree($stage) || warn "can't remove stage directory $stage: $!"; |
459 |
|
|
460 |
} else { |
} else { |
461 |
print "ISO $iso_file allready exists\n"; |
print "ISO $iso_file allready exists\n"; |
462 |
} |
} |
479 |
|
|
480 |
print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n"; |
print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n"; |
481 |
|
|
482 |
$sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr, $copy_nr); |
$sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr); |
483 |
|
|
484 |
print "Media burn for $disk_name copy $copy_nr recorded\n"; |
print "Media burn for $disk_name copy $copy_nr recorded\n"; |
485 |
} |
} |
486 |
|
|
487 |
$part_nr++; |
$volume_nr++; |
488 |
} until ($part_nr > $parts); |
} |
489 |
|
|
490 |
SKIP: |
SKIP: |
491 |
|
|
497 |
print "Recoding finished, exiting...\n"; |
print "Recoding finished, exiting...\n"; |
498 |
|
|
499 |
$sth->finish; |
$sth->finish; |
500 |
$sth_archive_backup->finish; |
$sth_archive_backup_parts->finish; |
501 |
$sth_archive_burned->finish; |
$sth_archive_burned->finish; |
502 |
|
|
503 |
$dbh->disconnect; |
$dbh->disconnect; |