4 use lib "/data/backuppc-agi//lib";
8 use BackupPC::SearchLib;
9 use Time::HiRes qw/time/;
10 use POSIX qw/strftime/;
19 # set this to 1 to prompt for DVD removal for each selected item.
20 my $prompt_for_delete = shift @ARGV;
23 # don't check for user
24 my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
25 my %Conf = $bpc->Conf();
26 %BackupPC::SearchLib::Conf = %Conf;
30 $conf_bin->{'cdrecord'} = $Conf{CDRecordBin} || die "Need CDRecordBin in config.pl\n";
31 my $cdr_opts = $Conf{CDRecordOpts} || die "Need CDRecordOpts in config.pl\n";
32 $conf_bin->{'eject'} = $Conf{ejectBin} || die "Need ejectBin in config.pl\n";
33 my $eject_opts = $Conf{ejectOpts} || die "Need ejectOpts in config.pl\n";
34 $conf_bin->{'mkisofs'} = $Conf{mkisofsBin} || die "Need mkisofsBin in config.pl\n";
36 my $path = abs_path($0);
37 $path =~ s#/[^/]+$#/#;
38 my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
41 foreach my $c (qw/cdrecord eject mkisofs/) {
42 $bin->{$c} = which($conf_bin->{$c}) || die "$0 needs $c ($conf_bin->{$c}), install it\n";
47 my $t_fmt = '%Y-%m-%d %H:%M:%S';
50 my $dsn = $Conf{SearchDSN} || die "Need SearchDSN in config.pl\n";
51 my $user = $Conf{SearchUser} || '';
53 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 1 });
55 my $tar_dir = $Conf{InstallDir}.'/';
56 $tar_dir .= $Conf{GzipTempDir} || die "GzipTempDir isn't defined in configuration";
58 die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir);
60 my $iso_dir = $Conf{InstallDir}.'/';
61 $iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration";
62 die "problem with $iso_dir, check ISOTempDir in configuration\n" unless (-d $iso_dir && -w $iso_dir);
67 my $t = shift || return;
69 my ($ss,$mm,$hh) = gmtime($t);
70 $out .= "${hh}h" if ($hh);
71 $out .= sprintf("%02d:%02d", $mm,$ss);
76 return strftime($t_fmt,localtime());
79 sub dumpArchive2XML($$$)
81 my ($dbh, $dvd_nr, $filename) = @_;
84 my $output = new IO::File('> '.$filename.'.tmp');
85 my $writer = new XML::Writer(OUTPUT=>$output, NEWLINES => 1);
87 print "Dumping file list for DVD $dvd_nr, countdown:";
89 $writer->pi('xml-stylesheet', 'href="archive.css" type="text/css"');
97 FROM files, backups, shares
98 WHERE files.backupnum=backups.num AND
99 files.shareid=shares.id AND
100 shares.hostid=backups.hostid AND
106 backups.id AS backup_id,
109 backups.date AS date,
110 shares.name AS share,
111 backups.size AS backup_size,
112 backups.inc_size AS compress_size
113 FROM backups, archive_backup, hosts, shares
114 WHERE archive_backup.backup_id = backups.id
115 AND hosts.id=backups.hostid
116 AND shares.id=backups.shareid
117 AND archive_backup.archive_id = ?
119 hosts.name, shares.name, backups.num
122 my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?");
123 $sth->execute($dvd_nr);
124 my $row = $sth->fetchrow_hashref();
127 foreach my $hide (qw/id note/) {
128 $row_h->{$hide} = $row->{$hide} || '';
129 delete $row->{$hide};
132 $writer->startTag("archive", %{$row});
134 $writer->startTag("note");
135 $writer->characters( $row_h->{'note'} );
136 $writer->endTag("note");
138 my $sth_backups = $dbh->prepare( $backups_sql );
139 $sth_backups->execute( $row_h->{'id'} );
141 my $total_backups = $sth_backups->rows;
145 while (my $row = $sth_backups->fetchrow_hashref()) {
148 my $pcnt = int(($curr_backup * 10) / $total_backups);
149 if ($pcnt > $last_pcnt) {
150 print " ",(10 - $pcnt);
154 my $sth_files = $dbh->prepare( $files_sql);
155 $sth_files->execute($row->{'backup_id'});
157 delete($row->{'backup_id'});
158 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
160 $writer->startTag( "backup", %{$row} );
162 while (my $row = $sth_files->fetchrow_hashref()) {
163 # convert filetype to string
164 $row->{'type'} = BackupPC::Attrib::fileType2Text(undef, $row->{'type'});
165 $row->{'date'} = strftime($t_fmt,gmtime($row->{'date'}));
166 my $path = $row->{'path'}; delete $row->{'path'};
168 $writer->startTag("file", %{$row});
169 $writer->characters( $path );
170 $writer->endTag("file");
172 $writer->endTag("backup");
175 $writer->endTag("archive");
178 rename $filename.'.tmp', $filename || die "can't rename $filename: $!";
187 my $sth = $dbh->prepare( qq{
189 id,dvd_nr,total_size,note,username,
190 archive.date as date,
191 count(archive_burned.archive_id) as copies
193 left outer join archive_burned on archive.id=archive_burned.archive_id
194 group by id,dvd_nr,total_size,note,username,archive.date
202 die "missing size" unless defined($s);
204 return sprintf("%.2f Mb", $s);
208 my $row = shift || die;
210 $row->{'date'} =~ s/\.\d+$//;
211 $row->{'copies'} =~ s/^\s*0+\s*$/no/;
213 my $copies = 'copies';
214 $copies = 'copy' if ($row->{'copies'} == 1);
217 sprintf("%d by %s on %s, %s %s [%s]",
221 $row->{'copies'}, $copies,
222 fmt_mb($row->{'total_size'}),
229 while (my $row = $sth->fetchrow_hashref) {
230 if ($row->{'copies'}) {
231 push @burned, fmt_archive($row);
233 push @not_burned, fmt_archive($row);
238 Label => 'Menu_archive',
241 Text => 'DVD #]Convey[',
242 Convey => [ @not_burned ],
246 Text => 'DVD #]Convey[',
247 Convey => [ @burned ],
252 Banner => "Select one or more DVD media for burning",
255 my @archives_to_burn = Menu(\%Menu_archive);
261 print "WARNING: $msg, skipping...\n";
265 sub add_symlink($$) {
266 my ($from, $to) = @_;
267 symlink $from, $to || skip("can't symlink $from to $to: $!");
271 my $dvd_nr = shift || return;
273 print "Do you want to delete DVD #$dvd_nr now? [NO/yes]: ";
276 if (lc($ok) eq 'yes') {
277 print "Deleting DVD #$dvd_nr from database...\n";
281 my $sth_delete_dvd = $dbh->prepare( qq{
282 delete from archive where dvd_nr = ?
284 $sth_delete_dvd->execute( $dvd_nr );
286 select setval('dvd_nr', (select max(dvd_nr) from archive), true)
289 # remove files for this DVD
292 print "\tremoving dir $_\n";
293 rmtree($_) || die "can't rmtree $_: $!";
295 print "\tremoving $_\n";
296 unlink($_) || die "can't rm $_: $!";
298 } glob ( "/$iso_dir/$dvd_nr.*" );
308 my $sth_archive_backup_parts = $dbh->prepare( qq{
310 archive_backup.backup_id,
313 shares.name as share,
315 backups.parts as parts,
316 backup_parts.part_nr as part_nr,
317 backup_parts.size as part_size,
318 backup_parts.md5 as md5,
321 join archive on archive_id = archive.id
322 join backups on archive_backup.backup_id = backups.id
323 join hosts on hostid = hosts.id
324 join shares on shareid = shares.id
325 join backup_parts on archive_backup.backup_id = backup_parts.backup_id
326 where archive.dvd_nr = ?
327 order by archive_backup.backup_id, backup_parts.part_nr
330 my $sth_archive_backup_check = $dbh->prepare( qq{
334 JOIN archive_backup on archive_backup.backup_id = backups.id
335 JOIN archive on archive_id = archive.id
339 my $sth_archive_burned = $dbh->prepare( qq{
340 insert into archive_burned
341 (archive_id, iso_size, part, copy)
342 values ( (select id from archive where dvd_nr =?), ?, ?, ?)
345 my $copies = $Conf{BurnMultipleCopies} || 1;
347 foreach my $copy_nr ( 1 .. $copies ) {
349 foreach my $arc (@archives_to_burn) {
350 exit if ($arc eq ']quit[');
352 my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
353 die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
355 my $tmp_dir = "/$iso_dir/$dvd_nr";
359 print "Working on DVD #$dvd_nr in $tmp_dir\n";
361 $sth_archive_backup_parts->execute($dvd_nr);
363 $sth_archive_backup_check->execute($dvd_nr);
365 my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);
367 if ($parts_nr != $check_nr) {
368 warn "ERROR: DVD #$dvd_nr is still not inconsistent state. Some backup parts are ",
369 ($parts_nr < $check_nr) ? "missing ($parts_nr < $check_nr)" : "extra ($parts_nr > $check_nr)",
370 " you should re-create this DVD after BackupPC_incPartsUpdate finish\n";
371 delete_dvd( $dvd_nr );
375 if ($sth_archive_backup_parts->rows == 0) {
376 warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n";
377 next if delete_dvd( $dvd_nr );
380 if ($prompt_for_delete) {
381 next if delete_dvd( $dvd_nr );
385 my $v; # emtpy volume
388 my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
389 while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {
390 if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {
394 $v->{size} += $row->{part_size};
395 $v_size += $row->{part_size};
399 filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}),
400 host => $row->{'host'},
401 share => $row->{'share'},
404 foreach my $fld (qw/part_nr md5/) {
405 $p->{$fld} = $row->{$fld} || die "missing $fld in row!";
407 push @{ $v->{parts} }, $p;
409 push @volumes, $v if ($v);
411 #warn "# volumes: ",Dumper(\@volumes)," total size: ", fmt_mb($v_size), "\n";
413 # check available disk space
415 my $df = df($iso_dir)->{bavail} || die "can't get free space on $iso_dir";
416 $df *= 1024; # calulate space in bytes
418 warn "ABORTED: not enough disk space to create ISO ! [need ", fmt_mb($v_size), " have ", fmt_mb( $df ), " on $iso_dir]\n";
422 my $volumes = $#volumes + 1;
425 foreach my $v (@volumes) {
430 my $disk_name = $dvd_nr;
431 # suffix added to multi-volume archives
432 my $volume_suffix = '';
435 $volume_suffix = '_' . $volume_nr;
436 $disk_name .= ' ' . $volume_nr . '/' . $volumes;
439 print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes [", fmt_mb($v->{size}), "]\n";
441 my $iso_file = my $xml_file = my $stage =
442 "${iso_dir}/${dvd_nr}";
444 $iso_file .= $volume_suffix . '.iso';
447 $stage .= $volume_suffix . '.stage';
449 my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
452 # check if ISO file exists
455 if (! -e $iso_file) {
457 # create stage directory
459 rmtree($stage) || die "can't remove $stage: $!";
463 # open file for md5sums
464 open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
466 my $parts_on_this_volume = 0;
468 foreach my $p (@{ $v->{parts} }) {
469 my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
470 my $rel_path = $tar_file;
472 if (-d "$tar_dir/$rel_path") {
473 mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");
474 $rel_path .= '/' . $p->{part_nr};
476 $rel_path .= '.tar.gz';
479 unless (-r "$tar_dir/$rel_path") {
480 print "WARNING: can't find increment $rel_path , trying to recreate it using BackupPC_tarIncCreate...\n";
481 my $host = $p->{host};
482 my $share = $p->{share};
483 my $dump = $p->{num};
484 my $currUser = getlogin();
486 if ($currUser eq "agi") {
487 $otherUser = "backuppc-agi";
488 } elsif ($currUser eq "qc") {
489 $otherUser = "backuppc-qc";
491 if ($otherUser ne "") {
492 my $cmd = "sudo -u $otherUser ".$tarIncCreate. " -h $host -s $share -n $dump";
494 if (system($cmd) != 0) {
502 skip "can't find increment $rel_path, recreateing obviously did not work: $!" unless (-r "$tar_dir/$rel_path");
504 add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
506 my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
508 print $md5 "$md5sum $rel_path\n" || die "can't write md5sum: $!";
510 $parts_on_this_volume++;
513 # add file list and note in xml
514 dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
516 add_symlink($xml_file, "$stage/${dvd_nr}.xml");
518 # add css file for archive
519 my $css_file = $Conf{CgiImageDir} . '/archive.css';
521 add_symlink($css_file, "$stage/archive.css");
523 print "WARNING: missing $css_file, not added to iso image!\n";
526 print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
528 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 };
530 system($cmd) == 0 or skip "can't run $cmd: $?";
532 rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
534 $iso_size = (stat($iso_file))[7];
536 print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
538 rmtree($stage) || warn "can't remove stage directory $stage: $!";
541 $iso_size = (stat($iso_file))[7];
542 print "ISO $iso_file already exists [$iso_size bytes]\n";
545 print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
547 system($bin->{'eject'}.' '.$eject_opts) == 0 or skip "can't run eject: $?";
551 my $cmd = $bin->{'cdrecord'} . ' ' . $cdr_opts . ' ' . $iso_file;
555 system($cmd) == 0 or skip "can't run $cmd: $?";
557 print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
559 $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);
561 print "Media burn for $disk_name copy $copy_nr recorded\n";
563 if ($copy_nr >= $copies) {
564 print STDERR "erasing temporary files, have $copy_nr copies (> $copies)\n";
565 foreach my $f (( $xml_file, $iso_file )) {
566 print STDERR "\t$f ";
567 unlink $f || die "can't remove $f: $!";
568 print STDERR "removed\n";
581 #my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
582 #print curr_time, sprintf(" %s:%s %-3d ", $row->{'host'}, $row->{'share'}, $row->{'num'}), " -> $tar_file ";
584 print "Recoding finished, exiting...\n";
587 $sth_archive_backup_parts->finish;
588 $sth_archive_burned->finish;
589 $sth_archive_backup_check->finish;
590 $sth_archive_burned->finish;