1 |
#!/usr/bin/perl |
#!/usr/bin/perl |
2 |
|
|
3 |
use strict; |
use strict; |
4 |
#use lib "__INSTALLDIR__/lib"; |
use lib "__INSTALLDIR__/lib"; |
|
use lib "/data/backuppc-agi/lib"; |
|
5 |
|
|
6 |
use DBI; |
use DBI; |
7 |
use BackupPC::Lib; |
use BackupPC::Lib; |
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 = shift @ARGV; |
20 |
$|=1; |
$|=1; |
21 |
|
|
22 |
# don't check for user |
# don't check for user |
111 |
AND hosts.id=backups.hostid |
AND hosts.id=backups.hostid |
112 |
AND shares.id=backups.shareid |
AND shares.id=backups.shareid |
113 |
AND archive_backup.archive_id = ? |
AND archive_backup.archive_id = ? |
114 |
|
ORDER BY |
115 |
|
hosts.name, shares.name, backups.num |
116 |
}; |
}; |
117 |
|
|
118 |
my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?"); |
my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?"); |
166 |
$writer->endTag("file"); |
$writer->endTag("file"); |
167 |
} |
} |
168 |
$writer->endTag("backup"); |
$writer->endTag("backup"); |
169 |
} |
} |
170 |
|
|
171 |
$writer->endTag("archive"); |
$writer->endTag("archive"); |
172 |
$writer->end(); |
$writer->end(); |
257 |
symlink $from, $to || skip("can't symlink $from to $to: $!"); |
symlink $from, $to || skip("can't symlink $from to $to: $!"); |
258 |
} |
} |
259 |
|
|
260 |
|
sub delete_dvd($) { |
261 |
|
my $dvd_nr = shift || return; |
262 |
|
|
263 |
|
print "Do you want to delete DVD #$dvd_nr now? [NO/yes]: "; |
264 |
|
my $ok = <STDIN>; |
265 |
|
chomp($ok); |
266 |
|
if (lc($ok) eq 'yes') { |
267 |
|
print "Deleting DVD #$dvd_nr from database...\n"; |
268 |
|
|
269 |
|
$dbh->begin_work; |
270 |
|
|
271 |
|
my $sth_delete_dvd = $dbh->prepare( qq{ |
272 |
|
delete from archive where dvd_nr = ? |
273 |
|
} ); |
274 |
|
$sth_delete_dvd->execute( $dvd_nr ); |
275 |
|
$dbh->do( qq{ |
276 |
|
select setval('dvd_nr', (select max(dvd_nr) from archive), true) |
277 |
|
} ); |
278 |
|
|
279 |
|
# remove files for this DVD |
280 |
|
map { |
281 |
|
if (-d $_) { |
282 |
|
print "\tremoving dir $_\n"; |
283 |
|
rmtree($_) || die "can't rmtree $_: $!"; |
284 |
|
} else { |
285 |
|
print "\tremoving $_\n"; |
286 |
|
unlink($_) || die "can't rm $_: $!"; |
287 |
|
} |
288 |
|
} glob ( "/$iso_dir/$dvd_nr.*" ); |
289 |
|
|
290 |
|
$dbh->commit; |
291 |
|
|
292 |
|
return 1; |
293 |
|
} else { |
294 |
|
return 0; |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
my $sth_archive_backup_parts = $dbh->prepare( qq{ |
my $sth_archive_backup_parts = $dbh->prepare( qq{ |
299 |
select |
select |
300 |
archive_backup.backup_id, |
archive_backup.backup_id, |
317 |
order by archive_backup.backup_id, backup_parts.part_nr |
order by archive_backup.backup_id, backup_parts.part_nr |
318 |
}); |
}); |
319 |
|
|
320 |
|
my $sth_archive_backup_check = $dbh->prepare( qq{ |
321 |
|
SELECT |
322 |
|
sum(backups.parts) |
323 |
|
FROM backups |
324 |
|
JOIN archive_backup on archive_backup.backup_id = backups.id |
325 |
|
JOIN archive on archive_id = archive.id |
326 |
|
WHERE dvd_nr = ? |
327 |
|
}); |
328 |
|
|
329 |
my $sth_archive_burned = $dbh->prepare( qq{ |
my $sth_archive_burned = $dbh->prepare( qq{ |
330 |
insert into archive_burned |
insert into archive_burned |
331 |
(archive_id, iso_size, part, copy) |
(archive_id, iso_size, part, copy) |
346 |
|
|
347 |
$sth_archive_backup_parts->execute($dvd_nr); |
$sth_archive_backup_parts->execute($dvd_nr); |
348 |
|
|
349 |
|
$sth_archive_backup_check->execute($dvd_nr); |
350 |
|
|
351 |
|
my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array); |
352 |
|
|
353 |
|
if ($parts_nr != $check_nr) { |
354 |
|
warn "ERROR: DVD #$dvd_nr is still not inconsistent state. Some backup parts are ", |
355 |
|
($parts_nr < $check_nr) ? "missing ($parts_nr < $check_nr)" : "extra ($parts_nr > $check_nr)", |
356 |
|
" you should re-create this DVD after BackupPC_incPartsUpdate finish\n"; |
357 |
|
delete_dvd( $dvd_nr ); |
358 |
|
next; |
359 |
|
} |
360 |
|
|
361 |
|
if ($sth_archive_backup_parts->rows == 0) { |
362 |
|
warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n"; |
363 |
|
next if delete_dvd( $dvd_nr ); |
364 |
|
} |
365 |
|
|
366 |
|
if ($prompt_for_delete) { |
367 |
|
next if delete_dvd( $dvd_nr ); |
368 |
|
} |
369 |
|
|
370 |
my @volumes; |
my @volumes; |
371 |
my $v; # emtpy volume |
my $v; # emtpy volume |
372 |
my $v_size = 0; |
my $v_size = 0; |
389 |
} |
} |
390 |
push @volumes, $v if ($v); |
push @volumes, $v if ($v); |
391 |
|
|
392 |
|
#warn "# volumes: ",Dumper(\@volumes),"\n"; |
393 |
|
|
394 |
my $volumes = $#volumes + 1; |
my $volumes = $#volumes + 1; |
395 |
my $volume_nr = 1; |
my $volume_nr = 1; |
396 |
|
|
397 |
foreach my $v (@volumes) { |
my $copies = $Conf{BurnMultipleCopies} || 1; |
398 |
|
|
399 |
print Dumper($v); |
foreach my $copy_nr ( 1 .. $copies ) { |
400 |
|
|
401 |
my $iso_size = 0; |
foreach my $v (@volumes) { |
|
my $disk_name = $dvd_nr; |
|
|
# suffix added to multi-volume archives |
|
|
my $volume_suffix = $dvd_nr; |
|
|
|
|
|
if ($volumes > 1) { |
|
|
$volume_suffix = '_' . $volume_nr; |
|
|
$disk_name .= ' ' . $volume_nr . '/' . $volumes; |
|
|
} |
|
402 |
|
|
403 |
print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n"; |
#print Dumper($v); |
404 |
|
|
405 |
my $iso_file = my $xml_file = my $stage = |
my $iso_size = 0; |
406 |
"${iso_dir}/${dvd_nr}"; |
my $disk_name = $dvd_nr; |
407 |
|
# suffix added to multi-volume archives |
408 |
|
my $volume_suffix = ''; |
409 |
|
|
410 |
$iso_file .= $volume_suffix . '.iso'; |
if ($volumes > 1) { |
411 |
$xml_file .= '.xml'; |
$volume_suffix = '_' . $volume_nr; |
412 |
|
$disk_name .= ' ' . $volume_nr . '/' . $volumes; |
413 |
$stage .= $volume_suffix . '.stage'; |
} |
414 |
|
|
415 |
my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5"; |
print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n"; |
416 |
|
|
417 |
# |
my $iso_file = my $xml_file = my $stage = |
418 |
# check if ISO file exists |
"${iso_dir}/${dvd_nr}"; |
|
# |
|
419 |
|
|
420 |
if (! -e $iso_file) { |
$iso_file .= $volume_suffix . '.iso'; |
421 |
|
$xml_file .= '.xml'; |
422 |
|
|
423 |
# create stage directory |
$stage .= $volume_suffix . '.stage'; |
|
if (-e $stage) { |
|
|
rmtree($stage) || die "can't remove $stage: $!"; |
|
|
} |
|
|
mkpath($stage); |
|
424 |
|
|
425 |
# open file for md5sums |
my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5"; |
|
open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!"; |
|
426 |
|
|
427 |
my $parts_on_this_volume = 0; |
# |
428 |
|
# check if ISO file exists |
429 |
|
# |
430 |
|
|
431 |
foreach my $p (@{ $v->{parts} }) { |
if (! -e $iso_file) { |
|
my $tar_file = $p->{filename} || die "no filename in part", Dumper($p); |
|
|
my $rel_path = $tar_file; |
|
432 |
|
|
433 |
if (-d "$tar_dir/$rel_path") { |
# create stage directory |
434 |
$rel_path .= '/' . $p->{part_nr}; |
if (-e $stage) { |
435 |
mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path"); |
rmtree($stage) || die "can't remove $stage: $!"; |
436 |
} |
} |
437 |
$rel_path .= '.tar.gz'; |
mkpath($stage); |
438 |
|
|
439 |
skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path"); |
# open file for md5sums |
440 |
|
open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!"; |
441 |
|
|
442 |
add_symlink("$tar_dir/$rel_path", "$stage/$rel_path"); |
my $parts_on_this_volume = 0; |
443 |
|
|
444 |
my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p); |
foreach my $p (@{ $v->{parts} }) { |
445 |
chomp($md5sum); |
my $tar_file = $p->{filename} || die "no filename in part", Dumper($p); |
446 |
print $md5 "$md5sum $rel_path\n" || die "can't write md5sum: $!"; |
my $rel_path = $tar_file; |
447 |
|
|
448 |
$parts_on_this_volume++; |
if (-d "$tar_dir/$rel_path") { |
449 |
} |
mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path"); |
450 |
|
$rel_path .= '/' . $p->{part_nr}; |
451 |
|
} |
452 |
|
$rel_path .= '.tar.gz'; |
453 |
|
|
454 |
# add file list and note in xml |
skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path"); |
|
dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file); |
|
455 |
|
|
456 |
add_symlink($xml_file, "$stage/${dvd_nr}.xml"); |
add_symlink("$tar_dir/$rel_path", "$stage/$rel_path"); |
457 |
|
|
458 |
# add css file for archive |
my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p); |
459 |
my $css_file = $Conf{CgiImageDir} . '/archive.css'; |
chomp($md5sum); |
460 |
if (-r $css_file) { |
print $md5 "$md5sum $rel_path\n" || die "can't write md5sum: $!"; |
|
add_symlink($css_file, "$stage/archive.css"); |
|
|
} else { |
|
|
print "WARNING: missing $css_file, not added to iso image!\n"; |
|
|
} |
|
461 |
|
|
462 |
print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n"; |
$parts_on_this_volume++; |
463 |
|
} |
464 |
|
|
465 |
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 }; |
# add file list and note in xml |
466 |
|
dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file); |
467 |
|
|
468 |
system($cmd) == 0 or skip "can't run $cmd: $?"; |
add_symlink($xml_file, "$stage/${dvd_nr}.xml"); |
469 |
|
|
470 |
rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!"; |
# add css file for archive |
471 |
|
my $css_file = $Conf{CgiImageDir} . '/archive.css'; |
472 |
|
if (-r $css_file) { |
473 |
|
add_symlink($css_file, "$stage/archive.css"); |
474 |
|
} else { |
475 |
|
print "WARNING: missing $css_file, not added to iso image!\n"; |
476 |
|
} |
477 |
|
|
478 |
$iso_size = (stat($iso_file))[7]; |
print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n"; |
479 |
|
|
480 |
print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\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 }; |
481 |
|
|
482 |
# FIXME |
system($cmd) == 0 or skip "can't run $cmd: $?"; |
|
#rmtree($stage) || warn "can't remove stage directory $stage: $!"; |
|
483 |
|
|
484 |
} else { |
rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!"; |
485 |
print "ISO $iso_file allready exists\n"; |
|
486 |
} |
$iso_size = (stat($iso_file))[7]; |
487 |
|
|
488 |
my $copies = $Conf{BurnMultipleCopies} || 1; |
print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n"; |
489 |
|
|
490 |
foreach my $copy_nr ( 1 .. $copies ) { |
rmtree($stage) || warn "can't remove stage directory $stage: $!"; |
491 |
|
|
492 |
|
} else { |
493 |
|
print "ISO $iso_file allready exists\n"; |
494 |
|
} |
495 |
|
|
496 |
print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n"; |
print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n"; |
497 |
|
|
527 |
$sth->finish; |
$sth->finish; |
528 |
$sth_archive_backup_parts->finish; |
$sth_archive_backup_parts->finish; |
529 |
$sth_archive_burned->finish; |
$sth_archive_burned->finish; |
530 |
|
$sth_archive_backup_check->finish; |
531 |
|
$sth_archive_burned->finish; |
532 |
|
|
533 |
$dbh->disconnect; |
$dbh->disconnect; |
534 |
|
|