added common BackupPC::Search::host_backup_nums
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 16 Feb 2011 17:07:43 +0000 (18:07 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 16 Feb 2011 17:07:43 +0000 (18:07 +0100)
This allows to have consistant view of available increments to archive in
web interface and through BackupPC_ASA_ArchiveStart helper.

bin/BackupPC_ASA_ArchiveStart
lib/BackupPC/CGI/Archive.pm
lib/BackupPC/Search.pm

index da3ef77..e8341f5 100755 (executable)
@@ -47,8 +47,8 @@ no  utf8;
 use lib "/usr/local/BackupPC/lib";
 use Getopt::Std;
 use BackupPC::Lib;
+use BackupPC::Search;
 
-use DBI;
 use Data::Dump qw(dump);
 
 my $debug = $ENV{DEBUG} || 0;
@@ -67,7 +67,7 @@ EOF
 
 my %Conf = $bpc->Conf();
 
-my $dbh = DBI->connect($Conf{SearchDSN}, $Conf{SearchUser}, "", { RaiseError => 1, AutoCommit => 0 });
+my $dbh = BackupPC::Search::get_dbh;
 
 my $sth = $dbh->prepare(qq{
        select
@@ -106,28 +106,7 @@ foreach my $host ( keys %$Hosts ) {
                warn "$0: host $host doesn't have any backups... skipping\n";
                next;
        }
-
-       my $all_backup_numbers;
-       $all_backup_numbers->{ $_->{num} }++ foreach @backups;
-
-       $sth->execute( $host );
-       while ( my $row = $sth->fetchrow_hashref ) {
-               warn "# row ",dump($row) if $debug;
-               $all_backup_numbers->{ $row->{num} } =
-               $row->{inc_deleted}  ? 0 :
-               $row->{size}    == 0 ? 0 :
-               $row->{inc_size} > 0 ? 0 :
-               $row->{size}     > 0 ? 1 :
-               0;
-       }
-
-       warn "# $host all_backup_numbers = ",dump($all_backup_numbers),"\n";
-
-       $host_nums->{$host} = [
-               sort
-               grep { $all_backup_numbers->{$_} }
-               keys %$all_backup_numbers
-       ];
+       $host_nums->{$host} = [ BackupPC::Search::host_backup_nums( $host ) ];
 }
 
 foreach ( @HostFilter ) {
index 8259fe2..18f7ba4 100644 (file)
@@ -39,7 +39,7 @@ package BackupPC::CGI::Archive;
 use strict;
 use BackupPC::CGI::Lib qw(:all);
 use Data::Dumper;
-use DBI;
+use BackupPC::Search;
 
 sub action
 {
@@ -61,7 +61,6 @@ sub action
             my($fullDur, $incrCnt, $fullSize, $fullRate);
             my @Backups = $bpc->BackupInfoRead($host);
             my $fullCnt = $incrCnt = 0;
-            my $all_backup_numbers;
 
             for ( my $i = 0 ; $i < @Backups ; $i++ ) {
                 if ( $Backups[$i]{type} eq "full" ) {
@@ -71,7 +70,6 @@ sub action
                     $incrSizeTot = $Backups[$i]{size} / (1024 * 1024);
                 }
                 $backupnumber = $Backups[$i]{num};
-               $all_backup_numbers->{$backupnumber}++;
             }
             $fullSizeTot += $fullSize + $incrSizeTot;
             $fullSize = sprintf("%.2f", ($fullSize + $incrSizeTot) / 1000);
@@ -79,40 +77,8 @@ sub action
                $bpc->ConfigRead($archHost);
                %Conf = $bpc->Conf();
 
-use Data::Dump qw(dump);
-
-               my $dbh = DBI->connect($Conf{SearchDSN}, $Conf{SearchUser}, "", { RaiseError => 1, AutoCommit => 0 });
-               my $sth = $dbh->prepare(qq{
-                       select
-                               hosts.name as host, -- FIXME for debug
-                               backups.num as num,
-                               inc_size,
-                               size,
-                               inc_deleted
-                       from backups
-                       join hosts on hosts.id = hostid
-                       where hosts.name = ?
-               });
-               $sth->execute($host);
-               # and inc_size < 0 and size > 0 and not inc_deleted
-               while( my $row = $sth->fetchrow_hashref ) {
-warn "# row ",dump $row;
-                       $all_backup_numbers->{ $row->{num} } =
-                       $row->{inc_deleted}  ? 0 :
-                       $row->{size}    == 0 ? 0 :
-                       $row->{inc_size} > 0 ? 0 :
-                       $row->{size}     > 0 ? 1 :
-                       0;
-               }
-
-warn "# host $host all_backup_numbers = ",dump($all_backup_numbers);
-               my @backup_nums = 
-                       sort { $a <=> $b }
-                       grep { $all_backup_numbers->{$_} }
-                       keys %$all_backup_numbers;
-
                my $checkboxes;
-               foreach my $backupnumber ( @backup_nums ) {
+               foreach my $backupnumber ( BackupPC::Search::host_backup_nums($host) ) {
                        $checkboxes .= qq|
 <input type="hidden" name="fcb$checkBoxCnt" value="$host">
 <input type="checkbox" name="backup$checkBoxCnt" value="$backupnumber">$backupnumber
index ee982ff..a6f01ff 100644 (file)
@@ -354,6 +354,43 @@ sub getGzipSize($$)
        );
 }
 
+sub host_backup_nums {
+       my $host = shift;
+       my $sth = get_dbh->prepare(qq{
+               select
+                       hosts.name as host, -- FIXME for debug
+                       backups.num as num,
+                       inc_size,
+                       size,
+                       inc_deleted
+               from backups
+               join hosts on hosts.id = hostid
+               where hosts.name = ?
+       });
+       $sth->execute($host);
+       # and inc_size < 0 and size > 0 and not inc_deleted
+
+       my $all_backup_numbers;
+
+       while( my $row = $sth->fetchrow_hashref ) {
+warn "# row ",dump $row;
+               $all_backup_numbers->{ $row->{num} } =
+               $row->{inc_deleted}  ? 0 :
+               $row->{size}    == 0 ? 0 :
+               $row->{inc_size} > 0 ? 0 :
+               $row->{size}     > 0 ? 1 :
+               0;
+       }
+
+warn "# host $host all_backup_numbers = ",dump($all_backup_numbers);
+       my @backup_nums = 
+               sort { $a <=> $b }
+               grep { $all_backup_numbers->{$_} }
+               keys %$all_backup_numbers;
+
+       return @backup_nums;
+}
+
 
 sub getBackupsNotBurned($) {