- new version of burnArchive that should work both on QC and on AGI
[BackupPC.git] / bin / BackupPC_burnArchiveCLI
index 8e8f0cb..d4fc321 100755 (executable)
@@ -1,7 +1,7 @@
-#!/usr/bin/perl
+#!/usr/local/bin/perl
 
 use strict;
-use lib "__INSTALLDIR__/lib";
+use lib "/data/backuppc-agi//lib";
 
 use DBI;
 use BackupPC::Lib;
@@ -11,7 +11,8 @@ use POSIX qw/strftime/;
 use Term::Menus;
 use File::Which;
 use File::Path;
-
+use Filesys::Df;
+use Cwd qw/abs_path/;
 use Data::Dumper;
 
 my $debug = 0;
@@ -32,6 +33,9 @@ $conf_bin->{'eject'} = $Conf{ejectBin} || die "Need ejectBin in config.pl\n";
 my $eject_opts = $Conf{ejectOpts} || die "Need ejectOpts in config.pl\n";
 $conf_bin->{'mkisofs'} = $Conf{mkisofsBin} || die "Need mkisofsBin in config.pl\n";
 
+my $path = abs_path($0);
+$path =~ s#/[^/]+$#/#;
+my $tarIncCreate = $path .= 'BackupPC_tarIncCreate';
 
 my $bin;
 foreach my $c (qw/cdrecord eject mkisofs/) {
@@ -193,23 +197,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,71 +342,85 @@ 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";
 
-       $sth_archive_backup_parts->execute($dvd_nr);
+               my $t = time();
 
-       $sth_archive_backup_check->execute($dvd_nr);
+               print "Working on DVD #$dvd_nr in $tmp_dir\n";
 
-       my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);
+               $sth_archive_backup_parts->execute($dvd_nr);
 
-       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;
-       }
+               $sth_archive_backup_check->execute($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 );
-       }
+               my ($parts_nr, $check_nr) = ($sth_archive_backup_parts->rows, $sth_archive_backup_check->fetchrow_array);
 
-       if ($prompt_for_delete) {
-               next if delete_dvd( $dvd_nr );
-       }
+               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;
+               }
 
-       my @volumes;
-       my $v;  # emtpy volume
-       my $v_size = 0;
+               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 );
+               }
 
-       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 ($prompt_for_delete) {
+                       next if delete_dvd( $dvd_nr );
                }
-               $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!";
+
+               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'}),
+                               host => $row->{'host'},
+                               share => $row->{'share'},
+                               num => $row->{'num'}
+                       };
+                       foreach my $fld (qw/part_nr md5/) {
+                               $p->{$fld} = $row->{$fld} || die "missing $fld in row!";
+                       }
+                       push @{ $v->{parts} }, $p;
                }
-               push @{ $v->{parts} }, $p;
-       }
-       push @volumes, $v if ($v);
+               push @volumes, $v if ($v);
 
-       #warn "# volumes: ",Dumper(\@volumes),"\n";
+               #warn "# volumes: ",Dumper(\@volumes)," total size: ", fmt_mb($v_size), "\n";
 
-       my $volumes = $#volumes + 1;
-       my $volume_nr = 1;
+               # check available disk space
 
-       my $copies = $Conf{BurnMultipleCopies} || 1;
+               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;
+               }
 
-       foreach my $copy_nr ( 1 .. $copies ) {
+               my $volumes = $#volumes + 1;
+               my $volume_nr = 1;
 
                foreach my $v (@volumes) {
 
@@ -412,7 +436,7 @@ foreach my $arc (@archives_to_burn) {
                                $disk_name .= ' ' . $volume_nr . '/' . $volumes;
                        }
 
-                       print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n";
+                       print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes [", fmt_mb($v->{size}), "]\n";
 
                        my $iso_file = my $xml_file = my $stage =
                                "${iso_dir}/${dvd_nr}";
@@ -451,8 +475,32 @@ foreach my $arc (@archives_to_burn) {
                                        }
                                        $rel_path .= '.tar.gz';
 
-                                       skip "can't find increment $rel_path: $!" unless (-r "$tar_dir/$rel_path");
+                                       
+                                       unless (-r "$tar_dir/$rel_path") {
+                                               print "WARNING: can't find increment $rel_path , trying to recreate it using BackupPC_tarIncCreate...\n";
+                                               my $host = $p->{host};
+                                               my $share = $p->{share};
+                                               my $dump = $p->{num};
+                                               my $currUser = getlogin();
+                                               my $otherUser = "";
+                                               if ($currUser eq "agi") {
+                                                       $otherUser = "backuppc-agi";
+                                               } elsif ($currUser eq "qc") {
+                                                       $otherUser = "backuppc-qc";
+                                               }
+                                               if ($otherUser ne "") {
+                                                       my $cmd = "sudo -u $otherUser ".$tarIncCreate. " -h $host -s $share -n $dump";
+                                                       print "$cmd ";
+                                                       if (system($cmd) != 0) {
+                                                               print " FAILED.\n";
+                                                       } else {
+                                                               print " done.\n";
+                                               }
+                                       }       
+                                       }
 
+                                       skip "can't find increment $rel_path, recreateing obviously did not work: $!" 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);
@@ -490,7 +538,8 @@ foreach my $arc (@archives_to_burn) {
                                rmtree($stage) || warn "can't remove stage directory $stage: $!";
 
                        } else {
-                               print "ISO $iso_file allready exists\n";
+                               $iso_size = (stat($iso_file))[7];
+                               print "ISO $iso_file already 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 +559,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: