update SQL for new schema, cleanup
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 28 Jan 2011 17:45:28 +0000 (18:45 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 28 Jan 2011 17:45:28 +0000 (18:45 +0100)
bin/BackupPC_ASA_BurnArchiveMedia

index 768aa27..f75be34 100755 (executable)
@@ -22,6 +22,7 @@ $|=1;
 
 # don't check for user
 my $bpc = BackupPC::Lib->new(undef, undef, 1) || die;
 
 # 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;
 
 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 $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 ----
 
 
 #---- subs ----
 
@@ -117,16 +119,18 @@ sub dumpArchive2XML($$$)
                        shares.name  AS share,
                        backups.size AS backup_size,
                        backups.inc_size AS compress_size
                        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 = ?
+               FROM backups
+               JOIN backup_parts  ON backup_parts.backup_id = backups.id
+               JOIN archive_parts ON archive_parts.backup_id = backups.id
+               JOIN hosts         ON hosts.id = backups.hostid
+               JOIN shares        ON shares.id = backups.shareid
+               WHERE archive_parts.backup_id = backups.id
+                       AND archive_parts.archive_id = ?
                ORDER BY
                        hosts.name, shares.name, backups.num
        };
 
                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=?");
+       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();
 
        $sth->execute($dvd_nr);
        my $row = $sth->fetchrow_hashref();
 
@@ -313,8 +317,8 @@ sub delete_dvd($) {
 }
 
 my $sth_archive_backup_parts = $dbh->prepare( qq{
 }
 
 my $sth_archive_backup_parts = $dbh->prepare( qq{
-       select
-               archive_backup.backup_id,
+       SELECT
+               backup_parts.backup_id,
                archive_id,
                hosts.name as host,
                shares.name as share,
                archive_id,
                hosts.name as host,
                shares.name as share,
@@ -323,23 +327,24 @@ my $sth_archive_backup_parts = $dbh->prepare( qq{
                backup_parts.part_nr as part_nr,
                backup_parts.size as part_size,
                backup_parts.md5 as md5,
                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
+               backup_parts.items,
+               backup_parts.filename
+       FROM backup_parts
+       JOIN archive_parts ON backup_parts.id = backup_part_id
+       JOIN archive       ON archive_id = archive.id
+       JOIN backups       ON backup_id = backups.id
+       JOIN hosts         ON hostid = hosts.id
+       JOIN shares        ON shareid = shares.id
+       WHERE archive.dvd_nr = ?
+       ORDER BY backup_id, part_nr
 });
 
 my $sth_archive_backup_check = $dbh->prepare( qq{
                SELECT
 });
 
 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
+                       count(*)
+               FROM archive
+               JOIN archive_parts ON archive_id = archive.id
+               JOIN backup_parts  ON backup_part_id = backup_parts.id
                WHERE dvd_nr = ?
 });
 
                WHERE dvd_nr = ?
 });
 
@@ -402,15 +407,11 @@ foreach my $copy_nr ( 1 .. $copies ) {
                        $v_size += $row->{part_size};
 
                        # this part
                        $v_size += $row->{part_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);
                        push @{ $v->{parts} }, $p;
                }
                push @volumes, $v if ($v);
@@ -473,45 +474,14 @@ foreach my $copy_nr ( 1 .. $copies ) {
 
                                foreach my $p (@{ $v->{parts} }) {
                                        my $tar_file = $p->{filename} || die "no filename in part", Dumper($p);
 
                                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);
 
                                        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++;
                                }
 
                                        $parts_on_this_volume++;
                                }