[Zetaback-devel] [zetaback commit] r148 - branches/classes

svn-commit at lists.omniti.com svn-commit at lists.omniti.com
Fri Nov 6 17:17:36 EST 2009


Author: mark
Date: 2009-11-06 17:17:36 -0500 (Fri, 06 Nov 2009)
New Revision: 148

Modified:
   branches/classes/zetaback.in
Log:
Class support in perform_retention

Modified: branches/classes/zetaback.in
===================================================================
--- branches/classes/zetaback.in	2009-11-06 22:17:30 UTC (rev 147)
+++ branches/classes/zetaback.in	2009-11-06 22:17:36 UTC (rev 148)
@@ -768,149 +768,159 @@
   zfs_do_backup($host, $fs, 's', $point, $store, $dumpname, $base);
 }
 
-sub perform_retention($$) {
-  my ($host, $store) = @_;
-  my $backup_info = scan_for_backups($store);
-  my $retention = config_get($host, 'retention');
+sub perform_retention($) {
+  my ($host) = @_;
   my $now = time();
 
   if ($DEBUG) {
     print "Performing retention for $host\n";
   }
 
-  foreach my $disk (sort keys %{$backup_info}) {
-    my $info = $backup_info->{$disk};
-    next unless(ref($info) eq 'HASH');
-    my %must_save;
-
+  foreach my $class (get_classes()) {
     if ($DEBUG) {
-      print "   $disk\n";
+      if ($class) {
+        print "=> Class: $class\n" if $class;
+      } else {
+        print "=> Class: (none)\n";
+      }
     }
+    my $retention = config_get($host, 'retention', $class);
+    my $store = get_store($host, $class);
+    my $backup_info = scan_for_backups($store);
+    foreach my $disk (sort keys %{$backup_info}) {
+      my $info = $backup_info->{$disk};
+      next unless(ref($info) eq 'HASH');
+      my %must_save;
 
-    # Get a list of all the full and incrementals, sorts newest to oldest
-    my @backup_points = (keys %{$info->{full}}, keys %{$info->{incremental}});
-    @backup_points = sort { $b <=> $a } @backup_points;
+      if ($DEBUG) {
+        print "   $disk\n";
+      }
 
-    # We _cannot_ throw away _all_ our backups,
-    # so save the most recent incremental and full no matter what
-    push(@{$must_save{$backup_points[0]}}, "most recent backup");
-    my @fulls = grep { exists($info->{full}->{$_}) } @backup_points;
-    push(@{$must_save{$fulls[0]}}, "most recent full");
+      # Get a list of all the full and incrementals, sorts newest to oldest
+      my @backup_points = (keys %{$info->{full}}, keys %{$info->{incremental}});
+      @backup_points = sort { $b <=> $a } @backup_points;
 
-    # Process retention policy
-    my @parts = split(/;/, $retention);
-    my %retention_map;
-    foreach (@parts) {
-      my ($period, $amount) = split(/,/);
-      if (!defined($amount)) {
-        $amount = -1;
+      # We _cannot_ throw away _all_ our backups,
+      # so save the most recent incremental and full no matter what
+      push(@{$must_save{$backup_points[0]}}, "most recent backup");
+      my @fulls = grep { exists($info->{full}->{$_}) } @backup_points;
+      push(@{$must_save{$fulls[0]}}, "most recent full");
+
+      # Process retention policy
+      my @parts = split(/;/, $retention);
+      my %retention_map;
+      foreach (@parts) {
+        my ($period, $amount) = split(/,/);
+        if (!defined($amount)) {
+          $amount = -1;
+        }
+        $retention_map{$period} = $amount;
       }
-      $retention_map{$period} = $amount;
-    }
-    my @periods = sort { $a <=> $b } keys(%retention_map);
-    my %backup_bins;
-    foreach(@periods) {
-      $backup_bins{$_} = ();
-    }
-    my $cutoff = $now - $periods[0];
-    # Sort backups into time period sections
-    foreach (@backup_points) {
-      # @backup_points is in descending order (newest first)
-      while ($_ <= $cutoff) {
-        # Move to the next largest bin if the current backup is not in the
-        # current bin. However, if there is no larger bin, then don't
-        shift(@periods);
-        if (@periods) {
-          $cutoff = $now - $periods[0];
-        } else {
+      my @periods = sort { $a <=> $b } keys(%retention_map);
+      my %backup_bins;
+      foreach(@periods) {
+        $backup_bins{$_} = ();
+      }
+      my $cutoff = $now - $periods[0];
+      # Sort backups into time period sections
+      foreach (@backup_points) {
+        # @backup_points is in descending order (newest first)
+        while ($_ <= $cutoff) {
+          # Move to the next largest bin if the current backup is not in the
+          # current bin. However, if there is no larger bin, then don't
+          shift(@periods);
+          if (@periods) {
+            $cutoff = $now - $periods[0];
+          } else {
+            last;
+          }
+        }
+        # Throw away all backups older than the largest time period specified
+        if (!@periods) {
           last;
         }
+        push(@{$backup_bins{$periods[0]}}, $_);
       }
-      # Throw away all backups older than the largest time period specified
-      if (!@periods) {
-        last;
+      foreach (keys(%backup_bins)) {
+        my $keep = $retention_map{$_}; # How many backups to keep
+        if ($backup_bins{$_}) {
+          my @backups = @{$backup_bins{$_}};
+          my $total = @backups;  # How many backups we have
+          # If we didn't specify how many to keep, keep them all
+          if ($keep == -1) { $keep = $total };
+          # If we have less backups than we should keep, keep them all
+          if ($total < $keep) { $keep = $total };
+          for (my $i = 1; $i <= $keep; $i++) {
+            my $idx = int(($i * $total) / $keep) - 1;
+            push(@{$must_save{$backups[$idx]}}, "retention policy - $_");
+          }
+        }
       }
-      push(@{$backup_bins{$periods[0]}}, $_);
-    }
-    foreach (keys(%backup_bins)) {
-      my $keep = $retention_map{$_}; # How many backups to keep
-      if ($backup_bins{$_}) {
-        my @backups = @{$backup_bins{$_}};
-        my $total = @backups;  # How many backups we have
-        # If we didn't specify how many to keep, keep them all
-        if ($keep == -1) { $keep = $total };
-        # If we have less backups than we should keep, keep them all
-        if ($total < $keep) { $keep = $total };
-        for (my $i = 1; $i <= $keep; $i++) {
-          my $idx = int(($i * $total) / $keep) - 1;
-          push(@{$must_save{$backups[$idx]}}, "retention policy - $_");
+      if ($DEBUG) {
+        print "    => Backup bins:\n";
+        foreach my $a (keys(%backup_bins)) {
+          print "      => $a\n";
+          foreach my $i (@{$backup_bins{$a}}) {
+            my $trans = $now - $i;
+            print "         => $i ($trans seconds old)";
+            if (exists($must_save{$i})) { print " => keep" };
+            print "\n";
+          }
         }
       }
-    }
-    if ($DEBUG) {
-      print "    => Backup bins:\n";
-      foreach my $a (keys(%backup_bins)) {
-        print "      => $a\n";
-        foreach my $i (@{$backup_bins{$a}}) {
-          my $trans = $now - $i;
-          print "         => $i ($trans seconds old)";
-          if (exists($must_save{$i})) { print " => keep" };
-          print "\n";
+
+      # Look for dependencies
+      foreach (@backup_points) {
+        if(exists($info->{incremental}->{$_})) {
+          print "   => $_ depends on $info->{incremental}->{$_}->{depends}\n" if($DEBUG);
+          if (exists($must_save{$_})) {
+            push(@{$must_save{$info->{incremental}->{$_}->{depends}}},
+              "dependency");
+          }
         }
       }
-    }
 
-    # Look for dependencies
-    foreach (@backup_points) {
-      if(exists($info->{incremental}->{$_})) {
-        print "   => $_ depends on $info->{incremental}->{$_}->{depends}\n" if($DEBUG);
-        if (exists($must_save{$_})) {
-          push(@{$must_save{$info->{incremental}->{$_}->{depends}}},
-            "dependency");
+      my @removals = grep { !exists($must_save{$_}) } @backup_points;
+      if($DEBUG) {
+        my $tf = config_get($host, 'time_format');
+        print "    => Candidates for removal:\n";
+        foreach (@backup_points) {
+          print "      => ". strftime($tf, localtime($_));
+          print " ($_)";
+          print " [". (exists($info->{full}->{$_}) ? "full":"incremental") ."]";
+          if (exists($must_save{$_})) {
+            my $reason = join(", ", @{$must_save{$_}});
+            print " => keep ($reason)";
+          } else {
+            print " => remove";
+          }
+          print "\n";
         }
       }
-    }
-
-    my @removals = grep { !exists($must_save{$_}) } @backup_points;
-    if($DEBUG) {
-      my $tf = config_get($host, 'time_format');
-      print "    => Candidates for removal:\n";
-      foreach (@backup_points) {
-        print "      => ". strftime($tf, localtime($_));
-        print " ($_)";
-        print " [". (exists($info->{full}->{$_}) ? "full":"incremental") ."]";
-        if (exists($must_save{$_})) {
-          my $reason = join(", ", @{$must_save{$_}});
-          print " => keep ($reason)";
+      foreach (@removals) {
+        my $efs = dir_encode($disk);
+        my $filename;
+        my $dataset;
+        if(exists($info->{full}->{$_}->{file})) {
+          $filename = $info->{full}->{$_}->{file};
+        } elsif(exists($info->{incremental}->{$_}->{file})) {
+          $filename = $info->{incremental}->{$_}->{file};
+        } elsif(exists($info->{full}->{$_}->{dataset})) {
+          $dataset = $info->{full}->{$_}->{dataset};
+        } elsif(exists($info->{incremental}->{$_}->{dataset})) {
+          $dataset = $info->{incremental}->{$_}->{dataset};
         } else {
-          print " => remove";
+          print "ERROR: We tried to expunge $host $disk [$_], but couldn't find it.\n";
         }
-        print "\n";
-      }
-    }
-    foreach (@removals) {
-      my $efs = dir_encode($disk);
-      my $filename;
-      my $dataset;
-      if(exists($info->{full}->{$_}->{file})) {
-        $filename = $info->{full}->{$_}->{file};
-      } elsif(exists($info->{incremental}->{$_}->{file})) {
-        $filename = $info->{incremental}->{$_}->{file};
-      } elsif(exists($info->{full}->{$_}->{dataset})) {
-        $dataset = $info->{full}->{$_}->{dataset};
-      } elsif(exists($info->{incremental}->{$_}->{dataset})) {
-        $dataset = $info->{incremental}->{$_}->{dataset};
-      } else {
-        print "ERROR: We tried to expunge $host $disk [$_], but couldn't find it.\n";
-      }
-      print "    => expunging ${filename}${dataset}\n" if($DEBUG);
-      unless($NEUTERED) {
-        if ($filename) {
-          unlink($filename) || print "ERROR: unlink $filename: $?\n";
-        } elsif ($dataset) {
-          `__ZFS__ destroy $dataset`;
-          if ($?) {
-            print "ERROR: zfs destroy $dataset: $?\n";
+        print "    => expunging ${filename}${dataset}\n" if($DEBUG);
+        unless($NEUTERED) {
+          if ($filename) {
+            unlink($filename) || print "ERROR: unlink $filename: $?\n";
+          } elsif ($dataset) {
+            `__ZFS__ destroy $dataset`;
+            if ($?) {
+              print "ERROR: zfs destroy $dataset: $?\n";
+            }
           }
         }
       }
@@ -1134,7 +1144,13 @@
   my (@files, @datasets, %classmap);
   my $tf = config_get($host, 'time_format');
   foreach my $class (get_classes()) {
-    print "=> Class: $class\n" if $DEBUG;
+    if ($DEBUG) {
+      if ($class) {
+        print "=> Class: $class\n" if $class;
+      } else {
+        print "=> Class: (none)\n";
+      }
+    }
     my $store = get_store($host, $class);
     my $backup_info = scan_for_backups($store);
     foreach my $disk (sort keys %{$backup_info}) {
@@ -1403,9 +1419,6 @@
       next;
     }
 
-    # TODO - remove once class functionality is completed
-    my $store = get_store($host, '');
-  
     if($LIST || $SUMMARY || $SUMMARY_EXT || $SUMMARY_VIOLATORS || $ARCHIVE) {
       show_backups($host, $ZFS);
     }
@@ -1413,7 +1426,7 @@
       plan_and_run($host, $ZFS);
     }
     if($EXPUNGE) {
-      perform_retention($host, $store);
+      perform_retention($host);
     }
   }
 }



More information about the Zetaback-devel mailing list