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 |
|
|
19 |
|
my $cdr_opts = 'dev=/dev/hdc blank=fast -v -eject'; |
20 |
|
|
21 |
|
my $bin; |
22 |
|
foreach my $c (qw/mkisofs cdrecord eject/) { |
23 |
|
$bin->{$c} = which($c) || die "$0 needs $c, install it\n"; |
24 |
|
} |
25 |
|
|
26 |
my $start_t = time(); |
my $start_t = time(); |
27 |
|
|
28 |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
68 |
my $output = new IO::File(">$filename"); |
my $output = new IO::File(">$filename"); |
69 |
my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1); |
my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1); |
70 |
|
|
71 |
|
$writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"'); |
72 |
|
|
73 |
my $files_sql = q{ |
my $files_sql = q{ |
74 |
SELECT |
SELECT |
75 |
files.path AS path, |
files.path AS path, |
91 |
backups.date AS date, |
backups.date AS date, |
92 |
shares.name AS share, |
shares.name AS share, |
93 |
backups.size AS backup_size, |
backups.size AS backup_size, |
94 |
backups.inc_size AS compress_size |
backups.inc_size AS compress_size, |
95 |
|
backups.parts AS parts |
96 |
FROM backups, archive_backup, hosts, shares |
FROM backups, archive_backup, hosts, shares |
97 |
WHERE archive_backup.backup_id = backups.id |
WHERE archive_backup.backup_id = backups.id |
98 |
AND hosts.id=backups.hostid |
AND hosts.id=backups.hostid |
106 |
|
|
107 |
my $row_h; |
my $row_h; |
108 |
foreach my $hide (qw/id note/) { |
foreach my $hide (qw/id note/) { |
109 |
$row_h->{$hide} = $row->{$hide} || die "no $hide"; |
$row_h->{$hide} = $row->{$hide} || ''; |
110 |
delete $row->{$hide}; |
delete $row->{$hide}; |
111 |
} |
} |
112 |
|
|
125 |
$sth_files->execute($row->{'backup_id'}); |
$sth_files->execute($row->{'backup_id'}); |
126 |
|
|
127 |
delete($row->{'backup_id'}); |
delete($row->{'backup_id'}); |
128 |
|
$row->{'date'} = strftime($t_fmt,gmtime($row->{'date'})); |
129 |
|
|
130 |
$writer->startTag( "backup", %{$row} ); |
$writer->startTag( "backup", %{$row} ); |
131 |
|
|
132 |
while (my $row = $sth_files->fetchrow_hashref()) { |
while (my $row = $sth_files->fetchrow_hashref()) { |
133 |
# convert filetype to string |
# convert filetype to string |
134 |
$row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'}); |
$row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'}); |
135 |
|
$row->{'date'} = strftime($t_fmt,gmtime($row->{'date'})); |
136 |
my $path = $row->{'path'}; delete $row->{'path'}; |
my $path = $row->{'path'}; delete $row->{'path'}; |
137 |
|
|
138 |
$writer->startTag("file", %{$row}); |
$writer->startTag("file", %{$row}); |
171 |
$row->{'copies'} =~ s/^\s*0+\s*$/no/; |
$row->{'copies'} =~ s/^\s*0+\s*$/no/; |
172 |
$row->{'total_size'} /= (1024*1024); |
$row->{'total_size'} /= (1024*1024); |
173 |
|
|
174 |
my $copies = 'copies'; |
my $copies = $row->{'copies'}; |
175 |
$copies = 'copy' if ($row->{'copies'} == 1); |
if ($row->{'parts'} > 1) { |
176 |
|
$copies .= '/' . $row->{'parts'}; |
177 |
|
} |
178 |
|
|
179 |
|
my $copies_dest = 'copies'; |
180 |
|
$copies_dest = 'copy' if ($row->{'copies'} == 1); |
181 |
|
|
182 |
return |
return |
183 |
sprintf("%d by %s on %s, %s %s [%.2f Mb]", |
sprintf("%d by %s on %s, %s %s [%.2f Mb]", |
184 |
$row->{'dvd_nr'}, |
$row->{'dvd_nr'}, |
185 |
$row->{'username'}, |
$row->{'username'}, |
186 |
$row->{'date'}, |
$row->{'date'}, |
187 |
$row->{'copies'}, $copies, |
$copies, $copies_dest, |
188 |
$row->{'total_size'}, |
$row->{'total_size'}, |
189 |
); |
); |
190 |
} |
} |
234 |
archive_id, |
archive_id, |
235 |
hosts.name as host, |
hosts.name as host, |
236 |
shares.name as share, |
shares.name as share, |
237 |
backups.num as num |
backups.num as num, |
238 |
|
backups.parts as parts |
239 |
from archive_backup |
from archive_backup |
240 |
join archive on archive_id = archive.id |
join archive on archive_id = archive.id |
241 |
join backups on backup_id = backups.id |
join backups on backup_id = backups.id |
246 |
|
|
247 |
my $sth_archive_burned = $dbh->prepare( qq{ |
my $sth_archive_burned = $dbh->prepare( qq{ |
248 |
insert into archive_burned |
insert into archive_burned |
249 |
(archive_id, iso_size) |
(archive_id, iso_size, part) |
250 |
values ( (select id from archive where dvd_nr =?), ?) |
values ( (select id from archive where dvd_nr =?), ?, ?) |
251 |
}); |
}); |
252 |
|
|
253 |
foreach my $arc (@archives_to_burn) { |
foreach my $arc (@archives_to_burn) { |
262 |
|
|
263 |
print "Working on DVD #$dvd_nr in $tmp_dir\n"; |
print "Working on DVD #$dvd_nr in $tmp_dir\n"; |
264 |
|
|
265 |
my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}"; |
my $part_path = ''; |
266 |
$list_file .= '.list'; |
my $part_nr = 1; |
267 |
$iso_file .= '.iso'; |
my $parts = 0; |
268 |
$xml_file .= '.xml'; |
|
|
|
|
|
if (-e $iso_file) { |
|
|
print "ISO $iso_file allready exists\n"; |
|
|
goto BURN; |
|
|
} |
|
|
|
|
269 |
$sth_archive_backup->execute($dvd_nr); |
$sth_archive_backup->execute($dvd_nr); |
270 |
|
|
271 |
open(my $list, "> $list_file") || skip "can't open $list_file: $!"; |
my $disk_name = 'unknown disk'; |
272 |
|
my $iso_size = 0; |
273 |
|
|
274 |
my $inc = 0; |
do { |
275 |
|
|
276 |
while (my $row = $sth_archive_backup->fetchrow_hashref) { |
# do we need to re-execute query for multi-part increments? |
277 |
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++; |
|
278 |
|
|
279 |
} |
my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row"; |
280 |
|
|
281 |
|
|
282 |
|
$parts = $row->{'parts'} || die "no parts?"; |
283 |
|
$disk_name = $dvd_nr; |
284 |
|
|
285 |
|
if ($parts > 1) { |
286 |
|
# parts start with 0, so we have -1 here |
287 |
|
$part_path = '.' . ($part_nr - 1); |
288 |
|
$disk_name .= '.' . $part_nr; |
289 |
|
} |
290 |
|
|
291 |
|
print "Processing part $part_nr/$parts\n"; |
292 |
|
|
293 |
|
my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}"; |
294 |
|
$list_file .= $part_path . '.list'; |
295 |
|
$iso_file .= $part_path . '.iso'; |
296 |
|
$xml_file .= '.xml'; |
297 |
|
|
298 |
|
# |
299 |
|
# check if ISO file exists |
300 |
|
# |
301 |
|
|
302 |
# FIXME add file list in xml and txt and note file |
if (! -e $iso_file) { |
303 |
|
|
304 |
dumpArchive2XML($dbh, $dvd_nr, $xml_file); |
open(my $list, "> $list_file") || skip "can't open $list_file: $!"; |
|
print $list "$xml_file\n"; |
|
305 |
|
|
306 |
close($list); |
my $inc = 0; |
307 |
|
|
308 |
print "Running mkisofs now for $inc increments...\n"; |
do { |
309 |
|
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
310 |
|
if (-f "$tar_dir/$tar_file") { |
311 |
|
skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file"); |
312 |
|
print $list "$tar_dir/$tar_file\n"; |
313 |
|
} else { |
314 |
|
my $len = length("$parts"); |
315 |
|
my $nr = sprintf("%0${len}d", ($part_nr - 1)); |
316 |
|
print $list "$tar_dir/$tar_file/part$nr\n"; |
317 |
|
} |
318 |
|
$inc++; |
319 |
|
} while ($row = $sth_archive_backup->fetchrow_hashref); |
320 |
|
|
321 |
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 }; |
# add file list and note in xml |
322 |
|
dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file); |
323 |
|
print $list "$xml_file\n"; |
324 |
|
|
325 |
system($cmd) == 0 or skip "can't run $cmd: $?"; |
# add css file for archive |
326 |
|
my $css_file = $Conf{CgiImageDir} . '/archive.css'; |
327 |
|
if (-r $css_file) { |
328 |
|
print $list "$css_file\n"; |
329 |
|
} else { |
330 |
|
print "WARNING: missing $css_file, not added to iso image!\n"; |
331 |
|
} |
332 |
|
|
333 |
my $size = (stat($iso_file))[7]; |
close($list); |
334 |
|
|
335 |
print "Created $iso_file [$size bytes] in ", fmt_time(time() - $t), "\n"; |
print "Running mkisofs now for $inc increments, disk $disk_name\n"; |
336 |
|
|
337 |
BURN: |
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 }; |
338 |
# FIXME add call to cdrecord here! |
|
339 |
$sth_archive_burned->execute($dvd_nr, $size); |
system($cmd) == 0 or skip "can't run $cmd: $?"; |
340 |
print "Media burn for $dvd_nr recorded\n"; |
|
341 |
|
$iso_size = (stat($iso_file))[7]; |
342 |
|
|
343 |
|
print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n"; |
344 |
|
|
345 |
|
} else { |
346 |
|
print "ISO $iso_file allready exists\n"; |
347 |
|
} |
348 |
|
|
349 |
|
print "\nREADY TO BURN MEDIA $disk_name please insert blank media and press ENTER\n\n"; |
350 |
|
|
351 |
|
system($bin->{'eject'}) == 0 or skip "can't run eject: $?"; |
352 |
|
|
353 |
|
my $wait = <STDIN>; |
354 |
|
|
355 |
|
my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file; |
356 |
|
|
357 |
|
# system($cmd) == 0 or skip "can't run $cmd: $?"; |
358 |
|
print "## $cmd\n"; |
359 |
|
|
360 |
|
print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n"; |
361 |
|
|
362 |
|
$sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr); |
363 |
|
|
364 |
|
print "Media burn for $disk_name recorded\n"; |
365 |
|
|
366 |
|
$part_nr++; |
367 |
|
} until ($part_nr > $parts); |
368 |
|
|
369 |
SKIP: |
SKIP: |
370 |
|
|
373 |
#my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
#my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
374 |
#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 "; |
375 |
|
|
376 |
|
print "Recoding finished, exiting...\n"; |
377 |
|
|
378 |
$sth->finish; |
$sth->finish; |
379 |
|
$sth_archive_backup->finish; |
380 |
|
$sth_archive_burned->finish; |
381 |
|
|
382 |
$dbh->disconnect; |
$dbh->disconnect; |
383 |
|
|