correct calculation of free space
[BackupPC.git] / bin / BackupPC_burnArchiveCLI
index fc18584..f8845d4 100755 (executable)
@@ -11,6 +11,7 @@ use POSIX qw/strftime/;
 use Term::Menus;
 use File::Which;
 use File::Path;
+use Filesys::Df;
 
 use Data::Dumper;
 
@@ -193,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'}),
                );
 }
 
@@ -332,166 +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 $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
-       die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
-
-       my $tmp_dir = "/$iso_dir/$dvd_nr";
+my $copies = $Conf{BurnMultipleCopies} || 1;
 
-       my $t = time();
+foreach my $copy_nr ( 1 .. $copies ) {
 
-       print "Working on DVD #$dvd_nr in $tmp_dir\n";
+       foreach my $arc (@archives_to_burn) {
+               exit if ($arc eq ']quit[');
 
-       $sth_archive_backup_parts->execute($dvd_nr);
+               my $dvd_nr = $1 if ($arc =~ m/DVD #(\d+)/);
+               die "BUG: can't find dvd_nr in $arc\n" unless ($dvd_nr);
 
-       $sth_archive_backup_check->execute($dvd_nr);
+               my $tmp_dir = "/$iso_dir/$dvd_nr";
 
-       my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);
+               my $t = time();
 
-       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;
-       }
+               print "Working on DVD #$dvd_nr in $tmp_dir\n";
 
-       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 );
-       }
+               $sth_archive_backup_parts->execute($dvd_nr);
 
-       if ($prompt_for_delete) {
-               next if delete_dvd( $dvd_nr );
-       }
+               $sth_archive_backup_check->execute($dvd_nr);
 
-       my @volumes;
-       my $v;  # emtpy volume
-       my $v_size = 0;
+               my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);
 
-       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 = {};
+               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;
                }
-               $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!";
+
+               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 );
                }
-               push @{ $v->{parts} }, $p;
-       }
-       push @volumes, $v if ($v);
 
-       #warn "# volumes: ",Dumper(\@volumes),"\n";
+               if ($prompt_for_delete) {
+                       next if delete_dvd( $dvd_nr );
+               }
 
-       my $volumes = $#volumes + 1;
-       my $volume_nr = 1;
+               my @volumes;
+               my $v;  # emtpy volume
+               my $v_size = 0;
 
-       foreach my $v (@volumes) {
+               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);
 
-               #print Dumper($v);
+               #warn "# volumes: ",Dumper(\@volumes)," total size: ", fmt_mb($v_size), "\n";
 
-               my $iso_size = 0;
-               my $disk_name = $dvd_nr;
-               # suffix added to multi-volume archives
-               my $volume_suffix = '';
+               # check available disk space
 
-               if ($volumes > 1) {
-                       $volume_suffix = '_' . $volume_nr;
-                       $disk_name .= ' ' . $volume_nr . '/' . $volumes;
+               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;
                }
 
-               print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n";
+               my $volumes = $#volumes + 1;
+               my $volume_nr = 1;
+
+               foreach my $v (@volumes) {
 
-               my $iso_file = my $xml_file = my $stage =
-                       "${iso_dir}/${dvd_nr}";
+                       #print Dumper($v);
 
-               $iso_file .= $volume_suffix . '.iso';
-               $xml_file .= '.xml';
+                       my $iso_size = 0;
+                       my $disk_name = $dvd_nr;
+                       # suffix added to multi-volume archives
+                       my $volume_suffix = '';
 
-               $stage .= $volume_suffix . '.stage';
+                       if ($volumes > 1) {
+                               $volume_suffix = '_' . $volume_nr;
+                               $disk_name .= ' ' . $volume_nr . '/' . $volumes;
+                       }
 
-               my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
+                       print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes [", fmt_mb($v->{size}), "]\n";
 
-               #
-               # check if ISO file exists
-               #
+                       my $iso_file = my $xml_file = my $stage =
+                               "${iso_dir}/${dvd_nr}";
 
-               if (! -e $iso_file) {
+                       $iso_file .= $volume_suffix . '.iso';
+                       $xml_file .= '.xml';
 
-                       # create stage directory
-                       if (-e $stage) {
-                               rmtree($stage) || die "can't remove $stage: $!";
-                       }
-                       mkpath($stage);
+                       $stage .= $volume_suffix . '.stage';
 
-                       # open file for md5sums
-                       open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
+                       my $md5_file = "${stage}/${dvd_nr}${volume_suffix}.md5";
 
-                       my $parts_on_this_volume = 0;
+                       #
+                       # check if ISO file exists
+                       #
 
-                       foreach my $p (@{ $v->{parts} }) {
-                               my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
-                               my $rel_path = $tar_file;
+                       if (! -e $iso_file) {
 
-                               if (-d "$tar_dir/$rel_path") {
-                                       mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");
-                                       $rel_path .= '/' . $p->{part_nr};
+                               # create stage directory
+                               if (-e $stage) {
+                                       rmtree($stage) || die "can't remove $stage: $!";
                                }
-                               $rel_path .= '.tar.gz';
+                               mkpath($stage);
 
-                               skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");
+                               # open file for md5sums
+                               open(my $md5, "> $md5_file") || skip "can't open $md5_file: $!";
 
-                               add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
+                               my $parts_on_this_volume = 0;
 
-                               my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
-                               chomp($md5sum);
-                               print $md5 "$md5sum  $rel_path\n" || die "can't write md5sum: $!";
+                               foreach my $p (@{ $v->{parts} }) {
+                                       my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
+                                       my $rel_path = $tar_file;
 
-                               $parts_on_this_volume++;
-                       }
+                                       if (-d "$tar_dir/$rel_path") {
+                                               mkpath("$stage/$rel_path") unless (-d "$stage/$rel_path");
+                                               $rel_path .= '/' . $p->{part_nr};
+                                       }
+                                       $rel_path .= '.tar.gz';
 
-                       # add file list and note in xml
-                       dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
+                                       skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");
 
-                       add_symlink($xml_file, "$stage/${dvd_nr}.xml");
+                                       add_symlink("$tar_dir/$rel_path", "$stage/$rel_path");
 
-                       # 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";
-                       }
+                                       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++;
+                               }
 
-                       print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
+                               # add file list and note in xml
+                               dumpArchive2XML($dbh, $dvd_nr, $xml_file) unless (-f $xml_file);
 
-                       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 };
+                               add_symlink($xml_file, "$stage/${dvd_nr}.xml");
 
-                       system($cmd) == 0 or skip "can't run $cmd: $?";
+                               # 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";
+                               }
 
-                       rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
+                               print "Running mkisofs now for $parts_on_this_volume increments, disk $disk_name\n";
 
-                       $iso_size = (stat($iso_file))[7];
+                               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 };
 
-                       print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
+                               system($cmd) == 0 or skip "can't run $cmd: $?";
 
-                       rmtree($stage) || warn "can't remove stage directory $stage: $!";
+                               rename $iso_file.'.tmp', $iso_file || skip "can't rename $iso_file: $!";
 
-               } else {
-                       print "ISO $iso_file allready exists\n";
-               }
+                               $iso_size = (stat($iso_file))[7];
 
-               my $copies = $Conf{BurnMultipleCopies} || 1;
+                               print "Created $iso_file [$iso_size bytes] in ", fmt_time(time() - $t), "\n";
 
-               foreach my $copy_nr ( 1 .. $copies ) {
+                               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";
 
@@ -510,9 +529,19 @@ foreach my $arc (@archives_to_burn) {
                        $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++;
                }
 
-               $volume_nr++;
        }
 
 SKIP: