1 |
#!/usr/bin/perl |
2 |
|
3 |
use strict; |
4 |
#use lib "__INSTALLDIR__/lib"; |
5 |
use lib "/data/backuppc-agi/lib"; |
6 |
|
7 |
use DBI; |
8 |
use BackupPC::Lib; |
9 |
use BackupPC::SearchLib; |
10 |
use Time::HiRes qw/time/; |
11 |
use POSIX qw/strftime/; |
12 |
use Term::Menus; |
13 |
use File::Which; |
14 |
use File::Path; |
15 |
|
16 |
use Data::Dumper; |
17 |
|
18 |
my $debug = 0; |
19 |
$|=1; |
20 |
|
21 |
# don't check for user |
22 |
my $bpc = BackupPC::Lib->new(undef, undef, 1) || die; |
23 |
my %Conf = $bpc->Conf(); |
24 |
%BackupPC::SearchLib::Conf = %Conf; |
25 |
|
26 |
my $conf_bin; |
27 |
|
28 |
$conf_bin->{'cdrecord'} = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n"; |
29 |
my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n"; |
30 |
$conf_bin->{'eject'} = $Conf{ejectBin} || die "Need ejectBin in config.pl\n"; |
31 |
my $eject_opts = $Conf{ejectOpts} || die "Need ejectOpts in config.pl\n"; |
32 |
$conf_bin->{'mkisofs'} = $Conf{mkisofsBin} || die "Need mkisofsBin in config.pl\n"; |
33 |
|
34 |
|
35 |
my $bin; |
36 |
foreach my $c (qw/cdrecord eject mkisofs/) { |
37 |
$bin->{$c} = which($conf_bin->{$c}) || die "$0 needs $c ($conf_bin->{$c}), install it\n"; |
38 |
} |
39 |
|
40 |
my $start_t = time(); |
41 |
|
42 |
my $t_fmt = '%Y-%m-%d %H:%M:%S'; |
43 |
|
44 |
|
45 |
my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n"; |
46 |
my $user = $Conf{SearchUser} || ''; |
47 |
|
48 |
my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 1 }); |
49 |
|
50 |
my $tar_dir = $Conf{InstallDir}.'/'; |
51 |
$tar_dir .= $Conf{GzipTempDir} || die "GzipTempDir isn't defined in configuration"; |
52 |
|
53 |
die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir); |
54 |
|
55 |
my $iso_dir = $Conf{InstallDir}.'/'; |
56 |
$iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration"; |
57 |
die "problem with $iso_dir, check ISOTempDir in configuration\n" unless (-d $iso_dir && -w $iso_dir); |
58 |
|
59 |
#---- subs ---- |
60 |
|
61 |
sub fmt_time { |
62 |
my $t = shift || return; |
63 |
my $out = ""; |
64 |
my ($ss,$mm,$hh) = gmtime($t); |
65 |
$out .= "${hh}h" if ($hh); |
66 |
$out .= sprintf("%02d:%02d", $mm,$ss); |
67 |
return $out; |
68 |
} |
69 |
|
70 |
sub curr_time { |
71 |
return strftime($t_fmt,localtime()); |
72 |
} |
73 |
|
74 |
sub dumpArchive2XML($$$) |
75 |
{ |
76 |
my ($dbh, $dvd_nr, $filename) = @_; |
77 |
my %archive; |
78 |
|
79 |
my $output = new IO::File('> '.$filename.'.tmp'); |
80 |
my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1); |
81 |
|
82 |
print "Dumping file list for DVD $dvd_nr, countdown:"; |
83 |
|
84 |
$writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"'); |
85 |
|
86 |
my $files_sql = q{ |
87 |
SELECT |
88 |
files.path AS path, |
89 |
files.date AS date, |
90 |
files.type AS type, |
91 |
files.size AS size |
92 |
FROM files, backups, shares |
93 |
WHERE files.backupnum=backups.num AND |
94 |
files.shareid=shares.id AND |
95 |
shares.hostid=backups.hostid AND |
96 |
backups.id=? |
97 |
}; |
98 |
|
99 |
my $backups_sql = q{ |
100 |
SELECT |
101 |
backups.id AS backup_id, |
102 |
hosts.name AS host, |
103 |
backups.num AS num, |
104 |
backups.date AS date, |
105 |
shares.name AS share, |
106 |
backups.size AS backup_size, |
107 |
backups.inc_size AS compress_size |
108 |
FROM backups, archive_backup, hosts, shares |
109 |
WHERE archive_backup.backup_id = backups.id |
110 |
AND hosts.id=backups.hostid |
111 |
AND shares.id=backups.shareid |
112 |
AND archive_backup.archive_id = ? |
113 |
}; |
114 |
|
115 |
my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?"); |
116 |
$sth->execute($dvd_nr); |
117 |
my $row = $sth->fetchrow_hashref(); |
118 |
|
119 |
my $row_h; |
120 |
foreach my $hide (qw/id note/) { |
121 |
$row_h->{$hide} = $row->{$hide} || ''; |
122 |
delete $row->{$hide}; |
123 |
} |
124 |
|
125 |
$writer->startTag("archive", %{$row}); |
126 |
|
127 |
$writer->startTag("note"); |
128 |
$writer->characters( $row_h->{'note'} ); |
129 |
$writer->endTag("note"); |
130 |
|
131 |
my $sth_backups = $dbh->prepare( $backups_sql ); |
132 |
$sth_backups->execute( $row_h->{'id'} ); |
133 |
|
134 |
my $total_backups = $sth_backups->rows; |
135 |
my $curr_backup = 0; |
136 |
my $last_pcnt = 0; |
137 |
|
138 |
while (my $row = $sth_backups->fetchrow_hashref()) { |
139 |
|
140 |
$curr_backup++; |
141 |
my $pcnt = int(($curr_backup * 10) / $total_backups); |
142 |
if ($pcnt > $last_pcnt) { |
143 |
print " ",(10 - $pcnt); |
144 |
$last_pcnt = $pcnt; |
145 |
} |
146 |
|
147 |
my $sth_files = $dbh->prepare( $files_sql); |
148 |
$sth_files->execute($row->{'backup_id'}); |
149 |
|
150 |
delete($row->{'backup_id'}); |
151 |
$row->{'date'} = strftime($t_fmt,gmtime($row->{'date'})); |
152 |
|
153 |
$writer->startTag( "backup", %{$row} ); |
154 |
|
155 |
while (my $row = $sth_files->fetchrow_hashref()) { |
156 |
# convert filetype to string |
157 |
$row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'}); |
158 |
$row->{'date'} = strftime($t_fmt,gmtime($row->{'date'})); |
159 |
my $path = $row->{'path'}; delete $row->{'path'}; |
160 |
|
161 |
$writer->startTag("file", %{$row}); |
162 |
$writer->characters( $path ); |
163 |
$writer->endTag("file"); |
164 |
} |
165 |
$writer->endTag("backup"); |
166 |
} |
167 |
|
168 |
$writer->endTag("archive"); |
169 |
$writer->end(); |
170 |
|
171 |
rename $filename.'.tmp', $filename || die "can't rename $filename: $!"; |
172 |
|
173 |
print "\n"; |
174 |
} |
175 |
|
176 |
|
177 |
|
178 |
#----- main |
179 |
|
180 |
my $sth = $dbh->prepare( qq{ |
181 |
select |
182 |
id,dvd_nr,total_size,note,username, |
183 |
archive.date as date, |
184 |
count(archive_burned.archive_id) as copies |
185 |
from archive |
186 |
left outer join archive_burned on archive.id=archive_burned.archive_id |
187 |
group by id,dvd_nr,total_size,note,username,archive.date |
188 |
order by date asc |
189 |
} ); |
190 |
|
191 |
$sth->execute(); |
192 |
|
193 |
sub fmt_archive($) { |
194 |
my $row = shift || die; |
195 |
|
196 |
$row->{'date'} =~ s/\.\d+$//; |
197 |
$row->{'copies'} =~ s/^\s*0+\s*$/no/; |
198 |
$row->{'total_size'} /= (1024*1024); |
199 |
|
200 |
my $copies = 'copies'; |
201 |
$copies = 'copy' if ($row->{'copies'} == 1); |
202 |
|
203 |
return |
204 |
sprintf("%d by %s on %s, %s %s [%.2f Mb]", |
205 |
$row->{'dvd_nr'}, |
206 |
$row->{'username'}, |
207 |
$row->{'date'}, |
208 |
$row->{'copies'}, $copies, |
209 |
$row->{'total_size'}, |
210 |
); |
211 |
} |
212 |
|
213 |
my @burned; |
214 |
my @not_burned; |
215 |
|
216 |
while (my $row = $sth->fetchrow_hashref) { |
217 |
if ($row->{'copies'}) { |
218 |
push @burned, fmt_archive($row); |
219 |
} else { |
220 |
push @not_burned, fmt_archive($row); |
221 |
} |
222 |
} |
223 |
|
224 |
my %Menu_archive = ( |
225 |
Label => 'Menu_archive', |
226 |
|
227 |
Item_1 => { |
228 |
Text => 'DVD #]Convey[', |
229 |
Convey => [ @not_burned ], |
230 |
Default => '*', |
231 |
}, |
232 |
Item_2 => { |
233 |
Text => 'DVD #]Convey[', |
234 |
Convey => [ @burned ], |
235 |
}, |
236 |
|
237 |
Select => 'Many', |
238 |
|
239 |
Banner => "Select one or more DVD media for burning", |
240 |
); |
241 |
|
242 |
my @archives_to_burn = Menu(\%Menu_archive); |
243 |
|
244 |
print "\n"; |
245 |
|
246 |
sub skip($) { |
247 |
my $msg = shift; |
248 |
print "WARNING: $msg, skipping...\n"; |
249 |
goto SKIP; |
250 |
} |
251 |
|
252 |
sub add_symlink($$) { |
253 |
my ($from, $to) = @_; |
254 |
symlink $from, $to || skip("can't symlink $from to $to: $!"); |
255 |
} |
256 |
|
257 |
my $sth_archive_backup_parts = $dbh->prepare( qq{ |
258 |
select |
259 |
archive_backup.backup_id, |
260 |
archive_id, |
261 |
hosts.name as host, |
262 |
shares.name as share, |
263 |
backups.num as num, |
264 |
backups.parts as parts, |
265 |
backup_parts.part_nr as part_nr, |
266 |
backup_parts.size as part_size, |
267 |
backup_parts.md5 as md5, |
268 |
backup_parts.items |
269 |
from archive_backup |
270 |
join archive on archive_id = archive.id |
271 |
join backups on archive_backup.backup_id = backups.id |
272 |
join hosts on hostid = hosts.id |
273 |
join shares on shareid = shares.id |
274 |
join backup_parts on archive_backup.backup_id = backup_parts.backup_id |
275 |
where archive.dvd_nr = ? |
276 |
order by archive_backup.backup_id, backup_parts.part_nr |
277 |
}); |
278 |
|
279 |
my $sth_archive_burned = $dbh->prepare( qq{ |
280 |
insert into archive_burned |
281 |
(archive_id, iso_size, part, copy) |
282 |
values ( (select id from archive where dvd_nr =?), ?, ?, ?) |
283 |
}); |
284 |
|
285 |
foreach my $arc (@archives_to_burn) { |
286 |
exit if ($arc eq ']quit['); |
287 |
|
288 |
my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/); |
289 |
die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr); |
290 |
|
291 |
my $tmp_dir = "/$iso_dir/$dvd_nr"; |
292 |
|
293 |
my $t = time(); |
294 |
|
295 |
print "Working on DVD #$dvd_nr in $tmp_dir\n"; |
296 |
|
297 |
$sth_archive_backup_parts->execute($dvd_nr); |
298 |
|
299 |
my @volumes; |
300 |
my $v; # emtpy volume |
301 |
my $v_size = 0; |
302 |
|
303 |
my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize"; |
304 |
while (my $row = $sth_archive_backup_parts->fetchrow_hashref) { |
305 |
if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) { |
306 |
push @volumes, $v; |
307 |
$v = {}; |
308 |
} |
309 |
$v->{size} += $row->{part_size}; |
310 |
# this part |
311 |
my $p = { |
312 |
filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}), |
313 |
}; |
314 |
foreach my $fld (qw/part_nr md5/) { |
315 |
$p->{$fld} = $row->{$fld} || die "missing $fld in row!"; |
316 |
} |
317 |
push @{ $v->{parts} }, $p; |
318 |
} |
319 |
push @volumes, $v if ($v); |
320 |
|
321 |
my $volumes = $#volumes + 1; |
322 |
my $volume_nr = 1; |
323 |
|
324 |
foreach my $v (@volumes) { |
325 |
|
326 |
print Dumper($v); |
327 |
|
328 |
my $iso_size = 0; |
329 |
my $disk_name = $dvd_nr; |
330 |
# suffix added to multi-volume archives |
331 |
my $volume_suffix = $dvd_nr; |
332 |
|
333 |
if ($volumes > 1) { |
334 |
$volume_suffix = '_' . $volume_nr; |
335 |
$disk_name .= ' ' . $volume_nr . '/' . $volumes; |
336 |
} |
337 |
|
338 |
print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n"; |
339 |
|
340 |
my $iso_file = my $xml_file = my $stage = |
341 |
"${iso_dir}/${dvd_nr}"; |
342 |
|
343 |
$iso_file .= $volume_suffix . '.iso'; |
344 |
$xml_file .= '.xml'; |
345 |
|
346 |
$stage .= $volume_suffix . '.stage'; |
347 |
|
348 |
my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5"; |
349 |
|
350 |
# |
351 |
# check if ISO file exists |
352 |
# |
353 |
|
354 |
if (! -e $iso_file) { |
355 |
|
356 |
# create stage directory |
357 |
if (-e $stage) { |
358 |
rmtree($stage) || die "can't remove $stage: $!"; |
359 |
} |
360 |
mkpath($stage); |
361 |
|
362 |
# open file for md5sums |
363 |
open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!"; |
364 |
|
365 |
my $parts_on_this_volume = 0; |
366 |
|
367 |
foreach my $p (@{ $v->{parts} }) { |
368 |
my $tar_file = $p->{filename} || die "no filename in part", Dumper($p); |
369 |
my $rel_path = $tar_file; |
370 |
|
371 |
if (-d "$tar_dir/$rel_path") { |
372 |
$rel_path .= '/' . $p->{part_nr}; |
373 |
mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path"); |
374 |
} |
375 |
$rel_path .= '.tar.gz'; |
376 |
|
377 |
skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path"); |
378 |
|
379 |
add_symlink("$tar_dir/$rel_path", "$stage/$rel_path"); |
380 |
|
381 |
my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p); |
382 |
chomp($md5sum); |
383 |
print $md5 "$md5sum $rel_path\n" || die "can't write md5sum: $!"; |
384 |
|
385 |
$parts_on_this_volume++; |
386 |
} |
387 |
|
388 |
# add file list and note in xml |
389 |
dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file); |
390 |
|
391 |
add_symlink($xml_file, "$stage/${dvd_nr}.xml"); |
392 |
|
393 |
# add css file for archive |
394 |
my $css_file = $Conf{CgiImageDir} . '/archive.css'; |
395 |
if (-r $css_file) { |
396 |
add_symlink($css_file, "$stage/archive.css"); |
397 |
} else { |
398 |
print "WARNING: missing $css_file, not added to iso image!\n"; |
399 |
} |
400 |
|
401 |
print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n"; |
402 |
|
403 |
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 }; |
404 |
|
405 |
system($cmd) == 0 or skip "can't run $cmd: $?"; |
406 |
|
407 |
rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!"; |
408 |
|
409 |
$iso_size = (stat($iso_file))[7]; |
410 |
|
411 |
print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n"; |
412 |
|
413 |
# FIXME |
414 |
#rmtree($stage) || warn "can't remove stage directory $stage: $!"; |
415 |
|
416 |
} else { |
417 |
print "ISO $iso_file allready exists\n"; |
418 |
} |
419 |
|
420 |
my $copies = $Conf{BurnMultipleCopies} || 1; |
421 |
|
422 |
foreach my $copy_nr ( 1 .. $copies ) { |
423 |
|
424 |
print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n"; |
425 |
|
426 |
system($bin->{'eject'}.' '.$eject_opts) == 0 or skip "can't run eject: $?"; |
427 |
|
428 |
my $wait = <STDIN>; |
429 |
|
430 |
my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file; |
431 |
|
432 |
# FIXME |
433 |
print "## $cmd\n"; |
434 |
system($cmd) == 0 or skip "can't run $cmd: $?"; |
435 |
|
436 |
print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n"; |
437 |
|
438 |
$sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr); |
439 |
|
440 |
print "Media burn for $disk_name copy $copy_nr recorded\n"; |
441 |
} |
442 |
|
443 |
$volume_nr++; |
444 |
} |
445 |
|
446 |
SKIP: |
447 |
|
448 |
} |
449 |
|
450 |
#my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}); |
451 |
#print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file "; |
452 |
|
453 |
print "Recoding finished, exiting...\n"; |
454 |
|
455 |
$sth->finish; |
456 |
$sth_archive_backup_parts->finish; |
457 |
$sth_archive_burned->finish; |
458 |
|
459 |
$dbh->disconnect; |
460 |
|