fix order and burning is working again
[BackupPC.git] / bin / BackupPC_ASA_BurnArchiveMedia
index 768aa27..2643918 100755 (executable)
@@ -15,13 +15,14 @@ use File::Path;
 use Cwd qw/abs_path/;
 use Data::Dumper;
 
-my $debug = 0;
+my $debug = $ENV{DEBUG} || 1;
 # 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
 my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
+$bpc->ConfigRead('_search_archive');
 my %Conf = $bpc->Conf();
 %BackupPC::Search::Conf = %Conf;
 
@@ -52,13 +53,14 @@ my $user = $Conf{SearchUser} || '';
 
 my $dbh = DBI->connect($dsn, $user, "", { RaiseError => 1, AutoCommit => 1 });
 
-my $tar_dir = $Conf{GzipTempDir} || die "GzipTempDir isn't defined in configuration";
+my $tar_dir = $Conf{ArchiveDest} || die "ArchiveDest isn't defined in configuration";
 
-die "problem with $tar_dir, check GzipTempDir in configuration\n" unless (-d $tar_dir);
+#die "problem with $tar_dir, check GzipTempDir in configuration\n" unless -d $tar_dir;
 
-my $iso_dir = $Conf{InstallDir}.'/';
-$iso_dir .= $Conf{ISOTempDir} || die "ISOTempDir isn't defined in configuration";
-die "problem with $iso_dir, check ISOTempDir in configuration\n" unless (-d $iso_dir && -w $iso_dir);
+my $iso_dir = $Conf{ArchiveDest}.'/iso';
+if ( ! -e $iso_dir ) {
+       mkdir $iso_dir || die "can't create $iso_dir: $!";
+}
 
 #---- subs ----
 
@@ -110,23 +112,19 @@ sub dumpArchive2XML($$$)
 
        my $backups_sql = q{
                SELECT
-                       backups.id   AS backup_id,
-                       hosts.name   AS host,
-                       backups.num  AS num,
-                       backups.date AS date,
-                       shares.name  AS share,
-                       backups.size AS backup_size,
-                       backups.inc_size AS compress_size
-               FROM backups, archive_backup, hosts, shares
-               WHERE archive_backup.backup_id = backups.id
-                       AND hosts.id=backups.hostid
-                       AND shares.id=backups.shareid
-                       AND archive_backup.archive_id = ?
-               ORDER BY
-                       hosts.name, shares.name, backups.num
+                       backup_id,
+                       backup_part_id,
+                       host,
+                       num,
+                       date,
+                       share,
+                       size
+               FROM archive_backup_parts
+               WHERE archive_id = ?
+               ORDER BY backup_part_id
        };
 
-       my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date,id FROM archive WHERE dvd_nr=?");
+       my $sth = $dbh->prepare("SELECT dvd_nr, total_size, note, username, date, id FROM archive WHERE dvd_nr=?");
        $sth->execute($dvd_nr);
        my $row = $sth->fetchrow_hashref();
 
@@ -313,34 +311,21 @@ sub delete_dvd($) {
 }
 
 my $sth_archive_backup_parts = $dbh->prepare( qq{
-       select
-               archive_backup.backup_id,
+       SELECT
+               backup_id,
                archive_id,
-               hosts.name as host,
-               shares.name as share,
-               backups.num as num,
-               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 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 = ?
+               host,
+               share,
+               num,
+               part_nr,
+               parts,
+               size,
+               md5,
+               items,
+               filename
+       FROM archive_backup_parts
+       WHERE dvd_nr = ?
+       ORDER BY backup_id, part_nr
 });
 
 my $sth_archive_burned = $dbh->prepare( qq{
@@ -367,18 +352,6 @@ foreach my $copy_nr ( 1 .. $copies ) {
 
                $sth_archive_backup_parts->execute($dvd_nr);
 
-               $sth_archive_backup_check->execute($dvd_nr);
-
-               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;
-               }
-
                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 );
@@ -394,28 +367,24 @@ foreach my $copy_nr ( 1 .. $copies ) {
 
                my $max_archive_size = $Conf{ArchiveMediaSize} || die "no ArchiveMediaSize";
                while (my $row = $sth_archive_backup_parts->fetchrow_hashref) {
-                       if (($v->{size} || 0) + $row->{part_size} > $max_archive_size) {
+                       if (($v->{size} || 0) + $row->{size} > $max_archive_size) {
                                push @volumes, $v;
                                $v = {};
                        }
-                       $v->{size} += $row->{part_size};
-                       $v_size += $row->{part_size};
+                       $v->{size} += $row->{size};
+                       $v_size += $row->{size};
 
                        # this part
-                       my $p = {
-                               filename => BackupPC::Search::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!";
-                       }
+                       my $p;
+                       $p->{$_} = $row->{$_} foreach (qw(
+                               filename host share num part_nr md5
+                       )); # include minimun number of columns in XML output
+
                        push @{ $v->{parts} }, $p;
                }
                push @volumes, $v if ($v);
 
-               #warn "# volumes: ",Dumper(\@volumes)," total size: ", fmt_mb($v_size), "\n";
+               warn "# volumes: ",Dumper(\@volumes)," total size: ", fmt_mb($v_size), "\n" if $debug;
 
                # check available disk space
 
@@ -473,45 +442,14 @@ foreach my $copy_nr ( 1 .. $copies ) {
 
                                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';
-
-                                       
-                                       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");
+                                       die "ERROR: increment missing $tar_file:$!" unless -r "$tar_dir/$tar_file";
+
+                                       add_symlink("$tar_dir/$tar_file", "$stage/$tar_file");
 
                                        my $md5sum = $p->{md5} || die "no md5 in part ", Dumper($p);
                                        chomp($md5sum);
-                                       print $md5 "$md5sum  $rel_path\n" || die "can't write md5sum: $!";
+                                       print $md5 "$md5sum  $tar_file\n" || die "can't write md5sum: $!";
 
                                        $parts_on_this_volume++;
                                }
@@ -592,7 +530,6 @@ print "Recoding finished, exiting...\n";
 $sth->finish;
 $sth_archive_backup_parts->finish;
 $sth_archive_burned->finish;
-$sth_archive_backup_check->finish;
 $sth_archive_burned->finish;
 
 $dbh->disconnect;