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 |
|
|
43 |
my $tar_dir = $Conf{InstallDir}.'/'; |
my $tar_dir = $Conf{InstallDir}.'/'; |
44 |
$tar_dir .= $Conf{GzipTempDir} || die "GzipTempDir isn't defined in configuration"; |
$tar_dir .= $Conf{GzipTempDir} || die "GzipTempDir isn't defined in configuration"; |
45 |
|
|
46 |
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); |
47 |
|
|
48 |
my $iso_dir = $Conf{InstallDir}.'/'; |
my $iso_dir = $Conf{InstallDir}.'/'; |
49 |
$iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration"; |
$iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration"; |
64 |
return strftime($t_fmt,localtime()); |
return strftime($t_fmt,localtime()); |
65 |
} |
} |
66 |
|
|
67 |
|
sub dumpArchive2XML($$$) |
68 |
|
{ |
69 |
|
my ($dbh, $dvd_nr, $filename) = @_; |
70 |
|
my %archive; |
71 |
|
my $output = new IO::File(">$filename"); |
72 |
|
my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1); |
73 |
|
|
74 |
|
$writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"'); |
75 |
|
|
76 |
|
my $files_sql = q{ |
77 |
|
SELECT |
78 |
|
files.path AS path, |
79 |
|
files.date AS date, |
80 |
|
files.type AS type, |
81 |
|
files.size AS size |
82 |
|
FROM files, backups, shares |
83 |
|
WHERE files.backupnum=backups.num AND |
84 |
|
files.shareid=shares.id AND |
85 |
|
shares.hostid=backups.hostid AND |
86 |
|
backups.id=? |
87 |
|
}; |
88 |
|
|
89 |
|
my $backups_sql = q{ |
90 |
|
SELECT |
91 |
|
backups.id AS backup_id, |
92 |
|
hosts.name AS host, |
93 |
|
backups.num AS num, |
94 |
|
backups.date AS date, |
95 |
|
shares.name AS share, |
96 |
|
backups.size AS backup_size, |
97 |
|
backups.inc_size AS compress_size |
98 |
|
FROM backups, archive_backup, hosts, shares |
99 |
|
WHERE archive_backup.backup_id = backups.id |
100 |
|
AND hosts.id=backups.hostid |
101 |
|
AND shares.id=backups.shareid |
102 |
|
AND archive_backup.archive_id = ? |
103 |
|
}; |
104 |
|
|
105 |
|
my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?"); |
106 |
|
$sth->execute($dvd_nr); |
107 |
|
my $row = $sth->fetchrow_hashref(); |
108 |
|
|
109 |
|
my $row_h; |
110 |
|
foreach my $hide (qw/id note/) { |
111 |
|
$row_h->{$hide} = $row->{$hide} || ''; |
112 |
|
delete $row->{$hide}; |
113 |
|
} |
114 |
|
|
115 |
|
$writer->startTag("archive", %{$row}); |
116 |
|
|
117 |
|
$writer->startTag("note"); |
118 |
|
$writer->characters( $row_h->{'note'} ); |
119 |
|
$writer->endTag("note"); |
120 |
|
|
121 |
|
my $sth_backups = $dbh->prepare( $backups_sql ); |
122 |
|
$sth_backups->execute( $row_h->{'id'} ); |
123 |
|
|
124 |
|
while (my $row = $sth_backups->fetchrow_hashref()) { |
125 |
|
|
126 |
|
my $sth_files = $dbh->prepare( $files_sql); |
127 |
|
$sth_files->execute($row->{'backup_id'}); |
128 |
|
|
129 |
|
delete($row->{'backup_id'}); |
130 |
|
$row->{'date'} = strftime($t_fmt,gmtime($row->{'date'})); |
131 |
|
|
132 |
|
$writer->startTag( "backup", %{$row} ); |
133 |
|
|
134 |
|
while (my $row = $sth_files->fetchrow_hashref()) { |
135 |
|
# convert filetype to string |
136 |
|
$row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'}); |
137 |
|
$row->{'date'} = strftime($t_fmt,gmtime($row->{'date'})); |
138 |
|
my $path = $row->{'path'}; delete $row->{'path'}; |
139 |
|
|
140 |
|
$writer->startTag("file", %{$row}); |
141 |
|
$writer->characters( $path ); |
142 |
|
$writer->endTag("file"); |
143 |
|
} |
144 |
|
$writer->endTag("backup"); |
145 |
|
} |
146 |
|
|
147 |
|
$writer->endTag("archive"); |
148 |
|
$writer->end(); |
149 |
|
|
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
#----- main |
#----- main |
155 |
|
|
156 |
my $sth = $dbh->prepare( qq{ |
my $sth = $dbh->prepare( qq{ |
228 |
my $sth_archive_backup = $dbh->prepare( qq{ |
my $sth_archive_backup = $dbh->prepare( qq{ |
229 |
select |
select |
230 |
backup_id, |
backup_id, |
231 |
|
archive_id, |
232 |
hosts.name as host, |
hosts.name as host, |
233 |
shares.name as share, |
shares.name as share, |
234 |
backups.num as num |
backups.num as num, |
235 |
|
backups.parts as parts |
236 |
from archive_backup |
from archive_backup |
237 |
join archive on archive_id = archive.id |
join archive on archive_id = archive.id |
238 |
join backups on backup_id = backups.id |
join backups on backup_id = backups.id |
241 |
where archive.dvd_nr = ? |
where archive.dvd_nr = ? |
242 |
}); |
}); |
243 |
|
|
244 |
|
my $sth_archive_burned = $dbh->prepare( qq{ |
245 |
|
insert into archive_burned |
246 |
|
(archive_id, iso_size, part, copy) |
247 |
|
values ( (select id from archive where dvd_nr =?), ?, ?, ?) |
248 |
|
}); |
249 |
|
|
250 |
foreach my $arc (@archives_to_burn) { |
foreach my $arc (@archives_to_burn) { |
251 |
exit if ($arc eq ']quit['); |
exit if ($arc eq ']quit['); |
252 |
|
|
259 |
|
|
260 |
print "Working on DVD #$dvd_nr in $tmp_dir\n"; |
print "Working on DVD #$dvd_nr in $tmp_dir\n"; |
261 |
|
|
262 |
my $iso_file = "${iso_dir}/${dvd_nr}.iso"; |
my $part_path = ''; |
263 |
|
my $part_nr = 1; |
264 |
|
my $parts = 0; |
265 |
|
|
266 |
|
$sth_archive_backup->execute($dvd_nr); |
267 |
|
|
268 |
skip "ISO $iso_file allready exists" if (-e $iso_file); |
my $disk_name = 'unknown disk'; |
269 |
|
my $iso_size = 0; |
270 |
|
|
271 |
$sth_archive_backup->execute($dvd_nr); |
do { |
272 |
|
|
273 |
print "Running mkisofs now...\n"; |
# do we need to re-execute query for multi-part increments? |
274 |
|
$sth_archive_backup->execute($dvd_nr) if ($parts > 1); |
275 |
|
|
276 |
my $cmd = qq{ mkisofs -A BackupPC -gui -J -r -T --input-charset ISO-8859-2 -V $dvd_nr -o $iso_file -path-list - }; |
my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row"; |
277 |
|
|
|
open(my $mkisofs, "| $cmd 2>&1") || skip "can't run $cmd: $!"; |
|
278 |
|
|
279 |
while (my $row = $sth_archive_backup->fetchrow_hashref) { |
$parts = $row->{'parts'} || die "no parts?"; |
280 |
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
$disk_name = $dvd_nr; |
|
skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file"); |
|
|
print $mkisofs "$tar_dir/$tar_file\n"; |
|
|
} |
|
281 |
|
|
282 |
while(! -e $iso_file) { |
if ($parts > 1) { |
283 |
sleep 1; |
# parts start with 0, so we have -1 here |
284 |
} |
$part_path = '.' . ($part_nr - 1); |
285 |
|
$disk_name .= '.' . $part_nr; |
286 |
|
} |
287 |
|
|
288 |
|
print "Processing part $part_nr/$parts\n"; |
289 |
|
|
290 |
print "Created $iso_file in ", fmt_time(time() - $t), "\n"; |
my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}"; |
291 |
|
$list_file .= $part_path . '.list'; |
292 |
|
$iso_file .= $part_path . '.iso'; |
293 |
|
$xml_file .= '.xml'; |
294 |
|
|
295 |
|
# |
296 |
|
# check if ISO file exists |
297 |
|
# |
298 |
|
|
299 |
|
if (! -e $iso_file) { |
300 |
|
|
301 |
|
open(my $list, "> $list_file") || skip "can't open $list_file: $!"; |
302 |
|
|
303 |
|
my $inc = 0; |
304 |
|
|
305 |
|
do { |
306 |
|
my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
307 |
|
if (-f "$tar_dir/$tar_file") { |
308 |
|
skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file"); |
309 |
|
print $list "$tar_dir/$tar_file\n"; |
310 |
|
} else { |
311 |
|
my $len = length("$parts"); |
312 |
|
my $nr = sprintf("%0${len}d", ($part_nr - 1)); |
313 |
|
print $list "$tar_dir/$tar_file/part$nr\n"; |
314 |
|
} |
315 |
|
$inc++; |
316 |
|
} while ($row = $sth_archive_backup->fetchrow_hashref); |
317 |
|
|
318 |
|
# add file list and note in xml |
319 |
|
dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file); |
320 |
|
print $list "$xml_file\n"; |
321 |
|
|
322 |
|
# add css file for archive |
323 |
|
my $css_file = $Conf{CgiImageDir} . '/archive.css'; |
324 |
|
if (-r $css_file) { |
325 |
|
print $list "$css_file\n"; |
326 |
|
} else { |
327 |
|
print "WARNING: missing $css_file, not added to iso image!\n"; |
328 |
|
} |
329 |
|
|
330 |
|
close($list); |
331 |
|
|
332 |
|
print "Running mkisofs now for $inc increments, disk $disk_name\n"; |
333 |
|
|
334 |
|
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 }; |
335 |
|
|
336 |
|
system($cmd) == 0 or skip "can't run $cmd: $?"; |
337 |
|
|
338 |
|
$iso_size = (stat($iso_file))[7]; |
339 |
|
|
340 |
|
print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n"; |
341 |
|
|
342 |
|
} else { |
343 |
|
print "ISO $iso_file allready exists\n"; |
344 |
|
} |
345 |
|
|
346 |
|
my $copies = $Conf{BurnMultipleCopies} || 1; |
347 |
|
|
348 |
|
foreach my $copy_nr ( 1 .. $copies ) { |
349 |
|
|
350 |
|
print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n"; |
351 |
|
|
352 |
|
system($bin->{'eject'}) == 0 or skip "can't run eject: $?"; |
353 |
|
|
354 |
|
my $wait = <STDIN>; |
355 |
|
|
356 |
|
my $cmd = $bin->{$cdrecord} . ' ' . $cdr_opts . ' ' . $iso_file; |
357 |
|
|
358 |
|
# FIXME |
359 |
|
print "## $cmd\n"; |
360 |
|
system($cmd) == 0 or skip "can't run $cmd: $?"; |
361 |
|
|
362 |
|
print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n"; |
363 |
|
|
364 |
|
$sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr, $copy_nr); |
365 |
|
|
366 |
|
print "Media burn for $disk_name copy $copy_nr recorded\n"; |
367 |
|
} |
368 |
|
|
369 |
|
$part_nr++; |
370 |
|
} until ($part_nr > $parts); |
371 |
|
|
372 |
SKIP: |
SKIP: |
373 |
|
|
376 |
#my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
#my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
377 |
#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 "; |
378 |
|
|
379 |
|
print "Recoding finished, exiting...\n"; |
380 |
|
|
381 |
$sth->finish; |
$sth->finish; |
382 |
|
$sth_archive_backup->finish; |
383 |
|
$sth_archive_burned->finish; |
384 |
|
|
385 |
$dbh->disconnect; |
$dbh->disconnect; |
386 |
|
|