correct calculation of free space
[BackupPC.git] / bin / BackupPC_burnArchiveCLI
index 88eb658..f8845d4 100755 (executable)
@@ -10,10 +10,14 @@ use Time::HiRes qw/time/;
 use POSIX qw/strftime/;
 use Term::Menus;
 use File::Which;
+use File::Path;
+use Filesys::Df;
 
 use Data::Dumper;
 
 my $debug = 0;
+# set this to 1 to prompt for DVD removal for each selected item.
+my $prompt_for_delete = shift @ARGV;
 $|=1;
 
 # don't check for user
@@ -108,6 +112,8 @@ sub dumpArchive2XML($$$)
                        AND hosts.id=backups.hostid
                        AND shares.id=backups.shareid
                        AND archive_backup.archive_id = ?
+               ORDER BY
+                       hosts.name, shares.name, backups.num
        };
 
        my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?");
@@ -161,7 +167,7 @@ sub dumpArchive2XML($$$)
                        $writer->endTag("file");
                }
                $writer->endTag("backup");
-       }       
+       }
                         
        $writer->endTag("archive");
        $writer->end(); 
@@ -188,23 +194,29 @@ order by date asc
 
 $sth->execute();
 
+sub fmt_mb($) {
+       my $s = shift;
+       die "missing size" unless defined($s);
+       $s /= (1024*1024);
+       return sprintf("%.2f Mb", $s);
+}
+
 sub fmt_archive($) {
        my $row = shift || die;
 
        $row->{'date'} =~ s/\.\d+$//;
        $row->{'copies'} =~ s/^\s*0+\s*$/no/;
-       $row->{'total_size'} /= (1024*1024);
 
        my $copies = 'copies';
        $copies = 'copy' if ($row->{'copies'} == 1);
 
        return
-               sprintf("%d by %s on %s, %s %s [%.2f Mb]",
+               sprintf("%d by %s on %s, %s %s [%s]",
                        $row->{'dvd_nr'},
                        $row->{'username'},
                        $row->{'date'},
                        $row->{'copies'}, $copies,
-                       $row->{'total_size'},
+                       fmt_mb($row->{'total_size'}),
                );
 }
 
@@ -247,20 +259,78 @@ sub skip($) {
        goto SKIP;
 }
 
-my $sth_archive_backup = $dbh->prepare( qq{
+sub add_symlink($$) {
+       my ($from, $to) = @_;
+       symlink $from, $to || skip("can't symlink $from to $to: $!");
+}
+
+sub delete_dvd($) {
+       my $dvd_nr = shift || return;
+
+       print "Do you want to delete DVD #$dvd_nr now? [NO/yes]: ";
+       my $ok = <STDIN>;
+       chomp($ok);
+       if (lc($ok) eq 'yes') {
+               print "Deleting DVD #$dvd_nr from database...\n";
+
+               $dbh->begin_work;
+
+               my $sth_delete_dvd = $dbh->prepare( qq{
+                       delete from archive where dvd_nr = ?
+               } );
+               $sth_delete_dvd->execute( $dvd_nr );
+               $dbh->do( qq{
+                       select setval('dvd_nr', (select max(dvd_nr) from archive), true)
+               } );
+
+               # remove files for this DVD
+               map {
+                       if (-d $_) {
+                               print "\tremoving dir $_\n";
+                               rmtree($_) || die "can't rmtree $_: $!";
+                       } else {
+                               print "\tremoving $_\n";
+                               unlink($_) || die "can't rm $_: $!";
+                       }
+               } glob ( "/$iso_dir/$dvd_nr.*" );
+
+               $dbh->commit;
+
+               return 1;
+       } else {
+               return 0;
+       }
+}
+
+my $sth_archive_backup_parts = $dbh->prepare( qq{
        select
-               backup_id,
+               archive_backup.backup_id,
                archive_id,
                hosts.name as host,
                shares.name as share,
                backups.num as num,
-               backups.parts as parts
+               backups.parts as parts,
+               backup_parts.part_nr as part_nr,
+               backup_parts.size as part_size,
+               backup_parts.md5 as md5,
+               backup_parts.items
        from archive_backup
        join archive on archive_id = archive.id
-       join backups on backup_id = backups.id
+       join backups on archive_backup.backup_id = backups.id
        join hosts on hostid = hosts.id
        join shares on shareid = shares.id
+       join backup_parts on archive_backup.backup_id = backup_parts.backup_id
        where archive.dvd_nr = ?
+       order by archive_backup.backup_id, backup_parts.part_nr
+});
+
+my $sth_archive_backup_check = $dbh->prepare( qq{
+               SELECT
+                       sum(backups.parts)
+               FROM backups
+               JOIN archive_backup on archive_backup.backup_id = backups.id
+               JOIN archive on archive_id = archive.id
+               WHERE dvd_nr = ?
 });
 
 my $sth_archive_burned = $dbh->prepare( qq{
@@ -269,107 +339,178 @@ my $sth_archive_burned = $dbh->prepare( qq{
        values ( (select id from archive where dvd_nr =?), ?, ?, ?)
 });
 
-foreach my $arc (@archives_to_burn) {
-       exit if ($arc eq ']quit[');
+my $copies = $Conf{BurnMultipleCopies} || 1;
 
-       my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
-       die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
+foreach my $copy_nr ( 1 .. $copies ) {
 
-       my $tmp_dir = "/$iso_dir/$dvd_nr";
+       foreach my $arc (@archives_to_burn) {
+               exit if ($arc eq ']quit[');
 
-       my $t = time();
+               my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
+               die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
 
-       print "Working on DVD #$dvd_nr in $tmp_dir\n";
+               my $tmp_dir = "/$iso_dir/$dvd_nr";
 
-       my $part_path = '';
-       my $part_nr = 1;
-       my $parts = 0;
-               
-       $sth_archive_backup->execute($dvd_nr);
+               my $t = time();
 
-       my $disk_name = 'unknown disk';
-       my $iso_size = 0;
+               print "Working on DVD #$dvd_nr in $tmp_dir\n";
 
-       do {
+               $sth_archive_backup_parts->execute($dvd_nr);
 
-               # do we need to re-execute query for multi-part increments?
-               $sth_archive_backup->execute($dvd_nr) if ($parts > 1);
+               $sth_archive_backup_check->execute($dvd_nr);
 
-               my $row = $sth_archive_backup->fetchrow_hashref || die "can't fetch row";
+               my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);
 
+               if ($parts_nr != $check_nr) {
+                       warn "ERROR: DVD #$dvd_nr is still not inconsistent state. Some backup parts are ",
+                               ($parts_nr < $check_nr) ? "missing ($parts_nr < $check_nr)" : "extra ($parts_nr > $check_nr)",
+                               " you should re-create this DVD after BackupPC_incPartsUpdate finish\n";
+                       delete_dvd( $dvd_nr );
+                       next;
+               }
 
-               $parts =  $row->{'parts'} || die "no parts?";
-               $disk_name = $dvd_nr;
+               if ($sth_archive_backup_parts->rows == 0) {
+                       warn "ERROR: no backup parts found for $dvd_nr. You should re-create that DVD.\n";
+                       next if delete_dvd( $dvd_nr );
+               }
 
-               if ($parts > 1) {
-                       # parts start with 0, so we have -1 here
-                       $part_path = '.' . ($part_nr - 1);
-                       $disk_name .= '.' . $part_nr;
+               if ($prompt_for_delete) {
+                       next if delete_dvd( $dvd_nr );
                }
 
-               print "Processing part $part_nr/$parts\n";
+               my @volumes;
+               my $v;  # emtpy volume
+               my $v_size = 0;
+
+               my $max_archive_size = $Conf{MaxArchiveSize} || die "no MaxArchiveSize";
+               while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {
+                       if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {
+                               push @volumes, $v;
+                               $v = {};
+                       }
+                       $v->{size} += $row->{part_size};
+                       $v_size += $row->{part_size};
+
+                       # this part
+                       my $p = {
+                               filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}),
+                       };
+                       foreach my $fld (qw/part_nr md5/) {
+                               $p->{$fld} = $row->{$fld} || die "missing $fld in row!";
+                       }
+                       push @{ $v->{parts} }, $p;
+               }
+               push @volumes, $v if ($v);
 
-               my $list_file = my $iso_file = my $xml_file = "${iso_dir}/${dvd_nr}";
-               $list_file .= $part_path . '.list';
-               $iso_file .= $part_path . '.iso';
-               $xml_file .= '.xml';
+               #warn "# volumes: ",Dumper(\@volumes)," total size: ", fmt_mb($v_size), "\n";
 
-               #
-               # check if ISO file exists
-               #
+               # check available disk space
 
-               if (! -e $iso_file) {
+               my $df = df($iso_dir)->{bavail} || die "can't get free space on $iso_dir";
+               $df *= 1024;    # calulate space in bytes
+               if ($df < $v_size) {
+                       warn "ABORTED: not enough disk space to create ISO ! [need ", fmt_mb($v_size), " have ", fmt_mb( $df ), " on $iso_dir]\n";
+                       next;
+               }
 
-                       open(my $list, "> $list_file") || skip "can't open $list_file: $!";
+               my $volumes = $#volumes + 1;
+               my $volume_nr = 1;
 
-                       my $inc = 0;
+               foreach my $v (@volumes) {
 
-                       do {
-                               my $tar_file = BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'});
-                               if (-f "$tar_dir/$tar_file") {
-                                       skip "can't find increment $tar_file: $!" unless (-r "$tar_dir/$tar_file");
-                                       print $list "$tar_dir/$tar_file\n";
-                               } else {
-                                       my $len = length("$parts");
-                                       my $nr = sprintf("%0${len}d", ($part_nr - 1));
-                                       print $list "$tar_dir/$tar_file/part$nr\n";
-                               }
-                               $inc++;
-                       } while ($row = $sth_archive_backup->fetchrow_hashref);
+                       #print Dumper($v);
 
-                       # add file list and note in xml
-                       dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
-                       print $list "$xml_file\n";
+                       my $iso_size = 0;
+                       my $disk_name = $dvd_nr;
+                       # suffix added to multi-volume archives
+                       my $volume_suffix = '';
 
-                       # add css file for archive
-                       my $css_file = $Conf{CgiImageDir} . '/archive.css';
-                       if (-r $css_file) {
-                               print $list "$css_file\n";
-                       } else {
-                               print "WARNING: missing $css_file, not added to iso image!\n";
+                       if ($volumes > 1) {
+                               $volume_suffix = '_' . $volume_nr;
+                               $disk_name .= ' ' . $volume_nr . '/' . $volumes;
                        }
 
-                       close($list);
+                       print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes [", fmt_mb($v->{size}), "]\n";
 
-                       print "Running mkisofs now for $inc increments, disk $disk_name\n";
+                       my $iso_file = my $xml_file = my $stage =
+                               "${iso_dir}/${dvd_nr}";
 
-                       my $cmd = $bin->{'mkisofs'} . qq{ -A BackupPC -gui -J -r -T --input-charset ISO-8859-2 -V $disk_name -o ${iso_file}.tmp -path-list $list_file };
+                       $iso_file .= $volume_suffix . '.iso';
+                       $xml_file .= '.xml';
 
-                       system($cmd) == 0 or skip "can't run $cmd: $?";
+                       $stage .= $volume_suffix . '.stage';
 
-                       rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
+                       my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
 
-                       $iso_size = (stat($iso_file))[7];
+                       #
+                       # check if ISO file exists
+                       #
 
-                       print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
+                       if (! -e $iso_file) {
 
-               } else {
-                       print "ISO $iso_file allready exists\n";
-               }
+                               # create stage directory
+                               if (-e $stage) {
+                                       rmtree($stage) || die "can't remove $stage: $!";
+                               }
+                               mkpath($stage);
 
-               my $copies = $Conf{BurnMultipleCopies} || 1;
+                               # open file for md5sums
+                               open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
 
-               foreach my $copy_nr ( 1 .. $copies ) {
+                               my $parts_on_this_volume = 0;
+
+                               foreach my $p (@{ $v->{parts} }) {
+                                       my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
+                                       my $rel_path = $tar_file;
+
+                                       if (-d "$tar_dir/$rel_path") {
+                                               mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");
+                                               $rel_path .= '/' . $p->{part_nr};
+                                       }
+                                       $rel_path .= '.tar.gz';
+
+                                       skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");
+
+                                       add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
+
+                                       my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
+                                       chomp($md5sum);
+                                       print $md5 "$md5sum  $rel_path\n" || die "can't write md5sum: $!";
+
+                                       $parts_on_this_volume++;
+                               }
+
+                               # add file list and note in xml
+                               dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
+
+                               add_symlink($xml_file, "$stage/${dvd_nr}.xml");
+
+                               # add css file for archive
+                               my $css_file = $Conf{CgiImageDir} . '/archive.css';
+                               if (-r $css_file) {
+                                       add_symlink($css_file, "$stage/archive.css");
+                               } else {
+                                       print "WARNING: missing $css_file, not added to iso image!\n";
+                               }
+
+                               print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
+
+                               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 };
+
+                               system($cmd) == 0 or skip "can't run $cmd: $?";
+
+                               rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
+
+                               $iso_size = (stat($iso_file))[7];
+
+                               print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
+
+                               rmtree($stage) || warn "can't remove stage directory $stage: $!";
+
+                       } else {
+                               $iso_size = (stat($iso_file))[7];
+                               print "ISO $iso_file allready exists [$iso_size bytes]\n";
+                       }
 
                        print "\nREADY TO BURN MEDIA $disk_name copy $copy_nr\n\nPlease insert blank media and press ENTER\n\n";
 
@@ -385,13 +526,23 @@ foreach my $arc (@archives_to_burn) {
 
                        print "\n\nPLEASE REMOVE DVD MEDIA AND LABEL IT WITH $disk_name\n\n";
 
-                       $sth_archive_burned->execute($dvd_nr, $iso_size, $part_nr, $copy_nr);
+                       $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);
 
                        print "Media burn for $disk_name copy $copy_nr recorded\n";
+
+                       if ($copy_nr >= $copies) {
+                               print STDERR "erasing temporary files, have $copy_nr copies (> $copies)\n";
+                               foreach my $f (( $xml_file, $iso_file )) {
+                                       print STDERR "\t$f ";
+                                       unlink $f || die "can't remove $f: $!";
+                                       print STDERR "removed\n";
+                               }
+                       }
+
+                       $volume_nr++;
                }
 
-               $part_nr++;
-       } until ($part_nr > $parts);
+       }
 
 SKIP:
 
@@ -403,7 +554,9 @@ SKIP:
 print "Recoding finished, exiting...\n";
 
 $sth->finish;
-$sth_archive_backup->finish;
+$sth_archive_backup_parts->finish;
+$sth_archive_burned->finish;
+$sth_archive_backup_check->finish;
 $sth_archive_burned->finish;
 
 $dbh->disconnect;