fix
[BackupPC.git] / bin / BackupPC_ASA_ArchiveStart
index bd294e4..5fb145c 100755 (executable)
@@ -49,22 +49,38 @@ use Getopt::Std;
 use BackupPC::Lib;
 
 use DBI;
 use BackupPC::Lib;
 
 use DBI;
+use Data::Dump qw(dump);
 
 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
 
 my %opts;
 
 # no options currently
 
 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
 
 my %opts;
 
 # no options currently
-if ( !getopts("", \%opts) || @ARGV < 3 ) {
+if ( !getopts("", \%opts) || @ARGV < 2 ) {
     print STDERR <<EOF;
     print STDERR <<EOF;
-usage: $0 archiveHost userName hosts[:num]...
+usage: $0 archiveHost userName [hosts[:num]...]
 EOF
     exit(1);
 }
 
 EOF
     exit(1);
 }
 
+my %Conf = $bpc->Conf();
+
+my $dbh = DBI->connect($Conf{SearchDSN}, $Conf{SearchUser}, "", { RaiseError => 1, AutoCommit => 0 });
+
+my $sth = $dbh->prepare(qq{
+       select
+               backups.id as backup_id,
+               hosts.name as host,
+               backups.num as num
+       from backups
+       join hosts on hosts.id = hostid
+       where hosts.name = ? and inc_size < 0 and size > 0 and not inc_deleted
+});
+
 my $Hosts       = $bpc->HostInfoRead();
 my $Hosts       = $bpc->HostInfoRead();
-my $ArchiveHost = $ARGV[0];
-my $UserName    = $ARGV[1];
+my $ArchiveHost = shift @ARGV;
+my $UserName    = shift @ARGV;
+my @HostFilter  = @ARGV;
 my $TopDir      = $bpc->{Conf}{TopDir};
 
 if ( !defined($Hosts->{$ArchiveHost}) ) {
 my $TopDir      = $bpc->{Conf}{TopDir};
 
 if ( !defined($Hosts->{$ArchiveHost}) ) {
@@ -73,23 +89,41 @@ if ( !defined($Hosts->{$ArchiveHost}) ) {
 }
 $bpc->ConfigRead($ArchiveHost);
 
 }
 $bpc->ConfigRead($ArchiveHost);
 
+if ( ! @HostFilter ) {
+       @HostFilter = keys %$Hosts;
+}
+
+warn "archiving hosts ",dump(@HostFilter);
+
 my(@HostList, @BackupList);
 my(@HostList, @BackupList);
-for ( my $i = 2 ; $i < @ARGV ; $i++ ) {
-    my ($host,$num) = split(/:/,$ARGV[$i],2);
+foreach ( @HostFilter ) {
+    my ($host,$num) = split(/:/,$_,2);
     if ( !defined($Hosts->{$host}) ) {
         print(STDERR "$0: host $host doesn't exist... quitting\n");
         exit(1);
     }
     my @backups = $bpc->BackupInfoRead($host);
     if ( !@backups ) {
     if ( !defined($Hosts->{$host}) ) {
         print(STDERR "$0: host $host doesn't exist... quitting\n");
         exit(1);
     }
     my @backups = $bpc->BackupInfoRead($host);
     if ( !@backups ) {
-        print(STDERR "$0: host $host doesn't have any backups... quitting\n");
-        exit(1);
+        warn "$0: host $host doesn't have any backups... skipping\n";
+       next;
+    }
+
+    $sth->execute( $host );
+    if ( $sth->rows == 0 ) {
+       warn "no backups to archive on $host\n";
+       push @HostList, $host;
+       push @BackupList, 0; # fake, but will make full-text update
+    }
+
+    while ( my $row = $sth->fetchrow_hashref ) {
+       warn "+ ", $row->{host}, " ", $row->{num}, "\n";
+       push(@HostList, $host);
+       push(@BackupList, $row->{num});
     }
     }
-    push(@HostList, $host);
-    $num ||= $backups[$#backups]{num};
-    push(@BackupList, $num);
 }
 
 }
 
+$dbh->disconnect;
+
 my $ReqFileName;
 for ( my $i = 0 ; ; $i++ ) {
     $ReqFileName="archiveReq.$$.$i";
 my $ReqFileName;
 for ( my $i = 0 ; ; $i++ ) {
     $ReqFileName="archiveReq.$$.$i";