correctly support all command-line handling
[BackupPC.git] / bin / BackupPC_ASA_ArchiveStart
index 5fb145c..f91a60f 100755 (executable)
@@ -51,6 +51,8 @@ use BackupPC::Lib;
 use DBI;
 use Data::Dump qw(dump);
 
+my $debug = $ENV{DEBUG} || 0;
+
 die("BackupPC::Lib->new failed\n") if ( !(my $bpc = BackupPC::Lib->new) );
 
 my %opts;
@@ -71,10 +73,13 @@ my $sth = $dbh->prepare(qq{
        select
                backups.id as backup_id,
                hosts.name as host,
-               backups.num as num
+               backups.num as num,
+               inc_size,
+               size,
+               inc_deleted
        from backups
        join hosts on hosts.id = hostid
-       where hosts.name = ? and inc_size < 0 and size > 0 and not inc_deleted
+       where hosts.name = ?
 });
 
 my $Hosts       = $bpc->HostInfoRead();
@@ -89,39 +94,73 @@ if ( !defined($Hosts->{$ArchiveHost}) ) {
 }
 $bpc->ConfigRead($ArchiveHost);
 
-if ( ! @HostFilter ) {
-       @HostFilter = keys %$Hosts;
-}
+my(@HostList, @BackupList);
 
-warn "archiving hosts ",dump(@HostFilter);
 
-my(@HostList, @BackupList);
+my $host_nums;
+
+foreach my $host ( keys %$Hosts ) {
+       my @backups = $bpc->BackupInfoRead($host);
+       if ( !@backups ) {
+               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
+       ];
+}
+
 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 ) {
-        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});
-    }
+       my ($host,$num) = split(/:/,$_,2);
+       if ( !defined($Hosts->{$host}) ) {
+               print(STDERR "$0: host $host doesn't exist... quitting\n");
+               exit(1);
+       }
+       if ( defined $num ) {
+               warn "+ $host $num\n";
+               push(@HostList, $host);
+               push(@BackupList, $num);
+       } else {
+               foreach my $num ( @{ $host_nums->{$host} } ) {
+                       warn "+ $host $num\n";
+                       push(@HostList, $host);
+                       push(@BackupList, $num);
+               }
+       }
 }
 
+if ( ! @HostFilter ) {
+       foreach my $host ( keys %$host_nums ) {
+               foreach my $num ( @{ $host_nums->{$host} } ) {
+                       warn "+ $host $num\n";
+                       push(@HostList, $host);
+                       push(@BackupList, $num);
+               }
+       }
+}
+
+warn "# HostList=",dump(@HostList),"\n";
+warn "# BackupList=",dump(@BackupList),"\n";
+
 $dbh->disconnect;
 
 my $ReqFileName;
@@ -138,7 +177,7 @@ my %ArchiveReq = (
     compext     => $bpc->{Conf}{ArchiveComp} eq 'none' ? ''
                     : ($bpc->{Conf}{ArchiveComp} eq 'gzip' ? '.gz' : '.bz2'),
     parfile     => $bpc->{Conf}{ArchivePar},
-    splitsize   => '0000000',
+    splitsize   => $bpc->{Conf}{ArchiveSplit} . '000000', # mb -> bytes
     host        => $ArchiveHost,
     HostList    => \@HostList,
     BackupList  => \@BackupList,