4 use lib "__INSTALLDIR__/lib";
8 use BackupPC::SearchLib;
9 use Time::HiRes qw/time/;
10 use POSIX qw/strftime/;
18 # set this to 1 to prompt for DVD removal for each selected item.
19 my $prompt_for_delete = 0;
22 # don't check for user
23 my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
24 my %Conf = $bpc->Conf();
25 %BackupPC::SearchLib::Conf = %Conf;
29 $conf_bin->{'cdrecord'} = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";
30 my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";
31 $conf_bin->{'eject'} = $Conf{ejectBin} || die "Need ejectBin in config.pl\n";
32 my $eject_opts = $Conf{ejectOpts} || die "Need ejectOpts in config.pl\n";
33 $conf_bin->{'mkisofs'} = $Conf{mkisofsBin} || die "Need mkisofsBin in config.pl\n";
37 foreach my $c (qw/cdrecord eject mkisofs/) {
38 $bin->{$c} = which($conf_bin->{$c}) || die "$0 needs $c ($conf_bin->{$c}), install it\n";
43 my $t_fmt = '%Y-%m-%d %H:%M:%S';
46 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
47 my $user = $Conf{SearchUser} || '';
49 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 1 });
51 my $tar_dir = $Conf{InstallDir}.'/';
52 $tar_dir .= $Conf{GzipTempDir} || die "GzipTempDir isn't defined in configuration";
54 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir);
56 my $iso_dir = $Conf{InstallDir}.'/';
57 $iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration";
58 die "problem with $iso_dir, check ISOTempDir in configuration\n" unless (-d $iso_dir && -w $iso_dir);
63 my $t = shift || return;
65 my ($ss,$mm,$hh) = gmtime($t);
66 $out .= "${hh}h" if ($hh);
67 $out .= sprintf("%02d:%02d", $mm,$ss);
72 return strftime($t_fmt,localtime());
75 sub dumpArchive2XML($$$)
77 my ($dbh, $dvd_nr, $filename) = @_;
80 my $output = new IO::File('> '.$filename.'.tmp');
81 my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);
83 print "Dumping file list for DVD $dvd_nr, countdown:";
85 $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');
93 FROM files, backups, shares
94 WHERE files.backupnum=backups.num AND
95 files.shareid=shares.id AND
96 shares.hostid=backups.hostid AND
102 backups.id AS backup_id,
105 backups.date AS date,
106 shares.name AS share,
107 backups.size AS backup_size,
108 backups.inc_size AS compress_size
109 FROM backups, archive_backup, hosts, shares
110 WHERE archive_backup.backup_id = backups.id
111 AND hosts.id=backups.hostid
112 AND shares.id=backups.shareid
113 AND archive_backup.archive_id = ?
116 my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?");
117 $sth->execute($dvd_nr);
118 my $row = $sth->fetchrow_hashref();
121 foreach my $hide (qw/id note/) {
122 $row_h->{$hide} = $row->{$hide} || '';
123 delete $row->{$hide};
126 $writer->startTag("archive", %{$row});
128 $writer->startTag("note");
129 $writer->characters( $row_h->{'note'} );
130 $writer->endTag("note");
132 my $sth_backups = $dbh->prepare( $backups_sql );
133 $sth_backups->execute( $row_h->{'id'} );
135 my $total_backups = $sth_backups->rows;
139 while (my $row = $sth_backups->fetchrow_hashref()) {
142 my $pcnt = int(($curr_backup * 10) / $total_backups);
143 if ($pcnt > $last_pcnt) {
144 print " ",(10 - $pcnt);
148 my $sth_files = $dbh->prepare( $files_sql);
149 $sth_files->execute($row->{'backup_id'});
151 delete($row->{'backup_id'});
152 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
154 $writer->startTag( "backup", %{$row} );
156 while (my $row = $sth_files->fetchrow_hashref()) {
157 # convert filetype to string
158 $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'});
159 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
160 my $path = $row->{'path'}; delete $row->{'path'};
162 $writer->startTag("file", %{$row});
163 $writer->characters( $path );
164 $writer->endTag("file");
166 $writer->endTag("backup");
169 $writer->endTag("archive");
172 rename $filename.'.tmp', $filename || die "can't rename $filename: $!";
181 my $sth = $dbh->prepare( qq{
183 id,dvd_nr,total_size,note,username,
184 archive.date as date,
185 count(archive_burned.archive_id) as copies
187 left outer join archive_burned on archive.id=archive_burned.archive_id
188 group by id,dvd_nr,total_size,note,username,archive.date
195 my $row = shift || die;
197 $row->{'date'} =~ s/\.\d+$//;
198 $row->{'copies'} =~ s/^\s*0+\s*$/no/;
199 $row->{'total_size'} /= (1024*1024);
201 my $copies = 'copies';
202 $copies = 'copy' if ($row->{'copies'} == 1);
205 sprintf("%d by %s on %s, %s %s [%.2f Mb]",
209 $row->{'copies'}, $copies,
210 $row->{'total_size'},
217 while (my $row = $sth->fetchrow_hashref) {
218 if ($row->{'copies'}) {
219 push @burned, fmt_archive($row);
221 push @not_burned, fmt_archive($row);
226 Label => 'Menu_archive',
229 Text => 'DVD #]Convey[',
230 Convey => [ @not_burned ],
234 Text => 'DVD #]Convey[',
235 Convey => [ @burned ],
240 Banner => "Select one or more DVD media for burning",
243 my @archives_to_burn = Menu(\%Menu_archive);
249 print "WARNING: $msg, skipping...\n";
253 sub add_symlink($$) {
254 my ($from, $to) = @_;
255 symlink $from, $to || skip("can't symlink $from to $to: $!");
259 my $dvd_nr = shift || return;
261 print "Do you want to delete DVD #$dvd_nr now? [NO/yes]: ";
264 if (lc($ok) eq 'yes') {
265 print "Deleting DVD #$dvd_nr from database...\n";
269 my $sth_delete_dvd = $dbh->prepare( qq{
270 delete from archive where dvd_nr = ?
272 $sth_delete_dvd->execute( $dvd_nr );
274 select setval('dvd_nr', (select max(dvd_nr) from archive), true)
277 # remove files for this DVD
279 print "\tremoving $_\n";
280 unlink($_) || die "can't rm $_: $!";
281 } glob ( "/$iso_dir/$dvd_nr.*" );
291 my $sth_archive_backup_parts = $dbh->prepare( qq{
293 archive_backup.backup_id,
296 shares.name as share,
298 backups.parts as parts,
299 backup_parts.part_nr as part_nr,
300 backup_parts.size as part_size,
301 backup_parts.md5 as md5,
304 join archive on archive_id = archive.id
305 join backups on archive_backup.backup_id = backups.id
306 join hosts on hostid = hosts.id
307 join shares on shareid = shares.id
308 join backup_parts on archive_backup.backup_id = backup_parts.backup_id
309 where archive.dvd_nr = ?
310 order by archive_backup.backup_id, backup_parts.part_nr
313 my $sth_archive_burned = $dbh->prepare( qq{
314 insert into archive_burned
315 (archive_id, iso_size, part, copy)
316 values ( (select id from archive where dvd_nr =?), ?, ?, ?)
319 foreach my $arc (@archives_to_burn) {
320 exit if ($arc eq ']quit[');
322 my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
323 die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
325 my $tmp_dir = "/$iso_dir/$dvd_nr";
329 print "Working on DVD #$dvd_nr in $tmp_dir\n";
331 $sth_archive_backup_parts->execute($dvd_nr);
333 if ($sth_archive_backup_parts->rows == 0) {
334 warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n";
335 delete_dvd( $dvd_nr );
338 if ($prompt_for_delete) {
339 next if delete_dvd( $dvd_nr );
343 my $v; # emtpy volume
346 my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
347 while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {
348 if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {
352 $v->{size} += $row->{part_size};
355 filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}),
357 foreach my $fld (qw/part_nr md5/) {
358 $p->{$fld} = $row->{$fld} || die "missing $fld in row!";
360 push @{ $v->{parts} }, $p;
362 push @volumes, $v if ($v);
364 #warn "# volumes: ",Dumper(\@volumes),"\n";
366 my $volumes = $#volumes + 1;
369 foreach my $v (@volumes) {
374 my $disk_name = $dvd_nr;
375 # suffix added to multi-volume archives
376 my $volume_suffix = '';
379 $volume_suffix = '_' . $volume_nr;
380 $disk_name .= ' ' . $volume_nr . '/' . $volumes;
383 print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n";
385 my $iso_file = my $xml_file = my $stage =
386 "${iso_dir}/${dvd_nr}";
388 $iso_file .= $volume_suffix . '.iso';
391 $stage .= $volume_suffix . '.stage';
393 my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
396 # check if ISO file exists
399 if (! -e $iso_file) {
401 # create stage directory
403 rmtree($stage) || die "can't remove $stage: $!";
407 # open file for md5sums
408 open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
410 my $parts_on_this_volume = 0;
412 foreach my $p (@{ $v->{parts} }) {
413 my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
414 my $rel_path = $tar_file;
416 if (-d "$tar_dir/$rel_path") {
417 mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");
418 $rel_path .= '/' . $p->{part_nr};
420 $rel_path .= '.tar.gz';
422 skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");
424 add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
426 my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
428 print $md5 "$md5sum $rel_path\n" || die "can't write md5sum: $!";
430 $parts_on_this_volume++;
433 # add file list and note in xml
434 dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
436 add_symlink($xml_file, "$stage/${dvd_nr}.xml");
438 # add css file for archive
439 my $css_file = $Conf{CgiImageDir} . '/archive.css';
441 add_symlink($css_file, "$stage/archive.css");
443 print "WARNING: missing $css_file, not added to iso image!\n";
446 print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
448 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 };
450 system($cmd) == 0 or skip "can't run $cmd: $?";
452 rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
454 $iso_size = (stat($iso_file))[7];
456 print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
458 rmtree($stage) || warn "can't remove stage directory $stage: $!";
461 print "ISO $iso_file allready exists\n";
464 my $copies = $Conf{BurnMultipleCopies} || 1;
466 foreach my $copy_nr ( 1 .. $copies ) {
468 print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
470 system($bin->{'eject'}.' '.$eject_opts) == 0 or skip "can't run eject: $?";
474 my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file;
478 system($cmd) == 0 or skip "can't run $cmd: $?";
480 print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
482 $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);
484 print "Media burn for $disk_name copy $copy_nr recorded\n";
494 #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
495 #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
497 print "Recoding finished, exiting...\n";
500 $sth_archive_backup_parts->finish;
501 $sth_archive_burned->finish;