1 |
#!/usr/local/bin/perl |
#!/usr/bin/perl |
2 |
|
|
3 |
use strict; |
use strict; |
4 |
#use lib "__INSTALLDIR__/lib"; |
use lib "__INSTALLDIR__/lib"; |
|
# FIXME |
|
|
use lib "/data/backuppc/lib"; |
|
5 |
|
|
6 |
use DBI; |
use DBI; |
7 |
use BackupPC::Lib; |
use BackupPC::Lib; |
9 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
10 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
11 |
use Term::Menus; |
use Term::Menus; |
12 |
|
use File::Which; |
13 |
|
|
14 |
use Data::Dumper; |
use Data::Dumper; |
15 |
|
|
16 |
my $debug = 0; |
my $debug = 0; |
17 |
$|=1; |
$|=1; |
18 |
|
|
|
my $start_t = time(); |
|
|
|
|
|
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
|
|
|
|
19 |
# don't check for user |
# don't check for user |
20 |
my $bpc = BackupPC::Lib->new(undef, undef, 1) || die; |
my $bpc = BackupPC::Lib->new(undef, undef, 1) || die; |
21 |
my %Conf = $bpc->Conf(); |
my %Conf = $bpc->Conf(); |
22 |
%BackupPC::SearchLib::Conf = %Conf; |
%BackupPC::SearchLib::Conf = %Conf; |
23 |
|
|
24 |
|
my $cdrecord = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n"; |
25 |
|
my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n"; |
26 |
|
|
27 |
|
|
28 |
|
my $bin; |
29 |
|
foreach my $c (qw/mkisofs eject/, $cdrecord) { |
30 |
|
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
31 |
|
} |
32 |
|
|
33 |
|
my $start_t = time(); |
34 |
|
|
35 |
|
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
36 |
|
|
37 |
|
|
38 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
39 |
my $user = $Conf{SearchUser} || ''; |
my $user = $Conf{SearchUser} || ''; |
40 |
|
|
67 |
sub dumpArchive2XML($$$) |
sub dumpArchive2XML($$$) |
68 |
{ |
{ |
69 |
my ($dbh, $dvd_nr, $filename) = @_; |
my ($dbh, $dvd_nr, $filename) = @_; |
70 |
my %archive; |
my %archive; |
71 |
my $output = new IO::File(">$filename"); |
|
72 |
|
my $output = new IO::File('> '.$filename.'.tmp'); |
73 |
my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1); |
my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1); |
74 |
|
|
75 |
|
print "Dumping file list for DVD $dvd_nr, countdown:"; |
76 |
|
|
77 |
$writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"'); |
$writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"'); |
78 |
|
|
79 |
my $files_sql = q{ |
my $files_sql = q{ |
111 |
|
|
112 |
my $row_h; |
my $row_h; |
113 |
foreach my $hide (qw/id note/) { |
foreach my $hide (qw/id note/) { |
114 |
$row_h->{$hide} = $row->{$hide} || die "no $hide"; |
$row_h->{$hide} = $row->{$hide} || ''; |
115 |
delete $row->{$hide}; |
delete $row->{$hide}; |
116 |
} |
} |
117 |
|
|
124 |
my $sth_backups = $dbh->prepare( $backups_sql ); |
my $sth_backups = $dbh->prepare( $backups_sql ); |
125 |
$sth_backups->execute( $row_h->{'id'} ); |
$sth_backups->execute( $row_h->{'id'} ); |
126 |
|
|
127 |
|
my $total_backups = $sth_backups->rows; |
128 |
|
my $curr_backup = 0; |
129 |
|
my $last_pcnt = 0; |
130 |
|
|
131 |
while (my $row = $sth_backups->fetchrow_hashref()) { |
while (my $row = $sth_backups->fetchrow_hashref()) { |
132 |
|
|
133 |
|
$curr_backup++; |
134 |
|
my $pcnt = int(($curr_backup * 10) / $total_backups); |
135 |
|
if ($pcnt > $last_pcnt) { |
136 |
|
print " ",(10 - $pcnt); |
137 |
|
$last_pcnt = $pcnt; |
138 |
|
} |
139 |
|
|
140 |
my $sth_files = $dbh->prepare( $files_sql); |
my $sth_files = $dbh->prepare( $files_sql); |
141 |
$sth_files->execute($row->{'backup_id'}); |
$sth_files->execute($row->{'backup_id'}); |
142 |
|
|
160 |
|
|
161 |
$writer->endTag("archive"); |
$writer->endTag("archive"); |
162 |
$writer->end(); |
$writer->end(); |
163 |
|
|
164 |
|
rename $filename.'.tmp', $filename || die "can't rename $filename: $!"; |
165 |
|
|
166 |
|
print "\n"; |
167 |
} |
} |
168 |
|
|
169 |
|
|
248 |
archive_id, |
archive_id, |
249 |
hosts.name as host, |
hosts.name as host, |
250 |
shares.name as share, |
shares.name as share, |
251 |
backups.num as num |
backups.num as num, |
252 |
|
backups.parts as parts |
253 |
from archive_backup |
from archive_backup |
254 |
join archive on archive_id = archive.id |
join archive on archive_id = archive.id |
255 |
join backups on backup_id = backups.id |
join backups on backup_id = backups.id |
260 |
|
|
261 |
my $sth_archive_burned = $dbh->prepare( qq{ |
my $sth_archive_burned = $dbh->prepare( qq{ |
262 |
insert into archive_burned |
insert into archive_burned |
263 |
(archive_id, iso_size) |
(archive_id, iso_size, part, copy) |
264 |
values ( (select id from archive where dvd_nr =?), ?) |
values ( (select id from archive where dvd_nr =?), ?, ?, ?) |
265 |
}); |
}); |
266 |
|
|
267 |
foreach my $arc (@archives_to_burn) { |
foreach my $arc (@archives_to_burn) { |
276 |
|
|
277 |
print "Working on DVD #$dvd_nr in $tmp_dir\n"; |
print "Working on DVD #$dvd_nr in $tmp_dir\n"; |
278 |
|
|
279 |
my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}"; |
my $part_path = ''; |
280 |
$list_file .= '.list'; |
my $part_nr = 1; |
281 |
$iso_file .= '.iso'; |
my $parts = 0; |
282 |
$xml_file .= '.xml'; |
|
|
|
|
|
if (-e $iso_file) { |
|
|
print "ISO $iso_file allready exists\n"; |
|
|
goto BURN; |
|
|
} |
|
|
|
|
283 |
$sth_archive_backup->execute($dvd_nr); |
$sth_archive_backup->execute($dvd_nr); |
284 |
|
|
285 |
open(my $list, "> $list_file") || skip "can't open $list_file: $!"; |
my $disk_name = 'unknown disk'; |
286 |
|
my $iso_size = 0; |
287 |
|
|
288 |
my $inc = 0; |
do { |
289 |
|
|
290 |
while (my $row = $sth_archive_backup->fetchrow_hashref) { |
# do we need to re-execute query for multi-part increments? |
291 |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
$sth_archive_backup->execute($dvd_nr) if ($parts > 1); |
|
skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file"); |
|
|
print $list "$tar_dir/$tar_file\n"; |
|
|
$inc++; |
|
292 |
|
|
293 |
} |
my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row"; |
294 |
|
|
|
# add file list and note in xml |
|
|
dumpArchive2XML($dbh, $dvd_nr, $xml_file); |
|
|
print $list "$xml_file\n"; |
|
|
|
|
|
# add css file for archive |
|
|
my $css_file = $Conf{CgiImageDir} . '/archive.css'; |
|
|
if (-r $css_file) { |
|
|
print $list "$css_file\n"; |
|
|
} else { |
|
|
print "WARNING: missing $css_file, not added to iso image!\n"; |
|
|
} |
|
295 |
|
|
296 |
close($list); |
$parts = $row->{'parts'} || die "no parts?"; |
297 |
|
$disk_name = $dvd_nr; |
298 |
|
|
299 |
|
if ($parts > 1) { |
300 |
|
# parts start with 0, so we have -1 here |
301 |
|
$part_path = '.' . ($part_nr - 1); |
302 |
|
$disk_name .= '.' . $part_nr; |
303 |
|
} |
304 |
|
|
305 |
|
print "Processing part $part_nr/$parts\n"; |
306 |
|
|
307 |
|
my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}"; |
308 |
|
$list_file .= $part_path . '.list'; |
309 |
|
$iso_file .= $part_path . '.iso'; |
310 |
|
$xml_file .= '.xml'; |
311 |
|
|
312 |
|
# |
313 |
|
# check if ISO file exists |
314 |
|
# |
315 |
|
|
316 |
|
if (! -e $iso_file) { |
317 |
|
|
318 |
|
open(my $list, "> $list_file") || skip "can't open $list_file: $!"; |
319 |
|
|
320 |
|
my $inc = 0; |
321 |
|
|
322 |
|
do { |
323 |
|
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
324 |
|
if (-f "$tar_dir/$tar_file") { |
325 |
|
skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file"); |
326 |
|
print $list "$tar_dir/$tar_file\n"; |
327 |
|
} else { |
328 |
|
my $len = length("$parts"); |
329 |
|
my $nr = sprintf("%0${len}d", ($part_nr - 1)); |
330 |
|
print $list "$tar_dir/$tar_file/part$nr\n"; |
331 |
|
} |
332 |
|
$inc++; |
333 |
|
} while ($row = $sth_archive_backup->fetchrow_hashref); |
334 |
|
|
335 |
|
# add file list and note in xml |
336 |
|
dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file); |
337 |
|
print $list "$xml_file\n"; |
338 |
|
|
339 |
|
# add css file for archive |
340 |
|
my $css_file = $Conf{CgiImageDir} . '/archive.css'; |
341 |
|
if (-r $css_file) { |
342 |
|
print $list "$css_file\n"; |
343 |
|
} else { |
344 |
|
print "WARNING: missing $css_file, not added to iso image!\n"; |
345 |
|
} |
346 |
|
|
347 |
|
close($list); |
348 |
|
|
349 |
|
print "Running mkisofs now for $inc increments, disk $disk_name\n"; |
350 |
|
|
351 |
|
my $cmd = $bin->{'mkisofs'} . qq{ -A BackupPC -gui -J -r -T --input-charset ISO-8859-2 -V $disk_name -o $iso_file -path-list $list_file }; |
352 |
|
|
353 |
print "Running mkisofs now for $inc increments...\n"; |
system($cmd) == 0 or skip "can't run $cmd: $?"; |
354 |
|
|
355 |
my $cmd = qq{ mkisofs -A BackupPC -gui -J -r -T --input-charset ISO-8859-2 -V $dvd_nr -o $iso_file -path-list $list_file }; |
$iso_size = (stat($iso_file))[7]; |
356 |
|
|
357 |
system($cmd) == 0 or skip "can't run $cmd: $?"; |
print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n"; |
358 |
|
|
359 |
my $size = (stat($iso_file))[7]; |
} else { |
360 |
|
print "ISO $iso_file allready exists\n"; |
361 |
|
} |
362 |
|
|
363 |
|
my $copies = $Conf{BurnMultipleCopies} || 1; |
364 |
|
|
365 |
|
foreach my $copy_nr ( 1 .. $copies ) { |
366 |
|
|
367 |
|
print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n"; |
368 |
|
|
369 |
|
system($bin->{'eject'}) == 0 or skip "can't run eject: $?"; |
370 |
|
|
371 |
|
my $wait = <STDIN>; |
372 |
|
|
373 |
|
my $cmd = $bin->{$cdrecord} . ' ' . $cdr_opts . ' ' . $iso_file; |
374 |
|
|
375 |
print "Created $iso_file [$size bytes] in ", fmt_time(time() - $t), "\n"; |
# FIXME |
376 |
|
print "## $cmd\n"; |
377 |
|
system($cmd) == 0 or skip "can't run $cmd: $?"; |
378 |
|
|
379 |
BURN: |
print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n"; |
380 |
# FIXME add call to cdrecord here! |
|
381 |
$sth_archive_burned->execute($dvd_nr, $size); |
$sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr, $copy_nr); |
382 |
print "Media burn for $dvd_nr recorded\n"; |
|
383 |
|
print "Media burn for $disk_name copy $copy_nr recorded\n"; |
384 |
|
} |
385 |
|
|
386 |
|
$part_nr++; |
387 |
|
} until ($part_nr > $parts); |
388 |
|
|
389 |
SKIP: |
SKIP: |
390 |
|
|
393 |
#my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
#my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
394 |
#print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file "; |
#print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file "; |
395 |
|
|
396 |
|
print "Recoding finished, exiting...\n"; |
397 |
|
|
398 |
$sth->finish; |
$sth->finish; |
399 |
|
$sth_archive_backup->finish; |
400 |
|
$sth_archive_burned->finish; |
401 |
|
|
402 |
$dbh->disconnect; |
$dbh->disconnect; |
403 |
|
|