added multiple rules for each snapshot in backup
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 25 Jun 2009 11:07:26 +0000 (11:07 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 25 Jun 2009 11:07:26 +0000 (11:07 +0000)
git-svn-id: svn://svn.rot13.org/sysadmin-cookbook@117 191e9f34-6774-4a6d-acfc-7664dacd4a2a

recepies/zfs/zfs-expire-snapshot.pl

index 9e5a9e6..6de3cce 100755 (executable)
@@ -4,6 +4,23 @@ use warnings;
 use strict;
 
 use DateTime;
+use Data::Dump qw/dump/;
+
+my $debug = 0;
+
+my $config = {
+       'default' => {
+               14 => 5,
+               30 => 10,
+               60 => 30,
+       },
+       '212052' => {   # koha-dev
+               7 => 10,
+       },
+       '212056' => {   # webpac2
+               7 => 5,
+       }
+};
 
 my $now = DateTime->now();
 
@@ -25,20 +42,47 @@ while(<$fs>) {
        my $op = ' ';
        my $last = 0;
 
-       if ( $age > 14 ) {
+       my $c = (grep { $host =~ m{\Q$_\E} } keys %$config)[0];
+       $c = 'default' unless defined $c;
+
+       warn "# config: $c\n" if $debug;
+
+       my $h = $host;
+       $h =~ s{,+/([^/]+)$}{}; # just hostname without path
+
+       $c = $config->{$c} || die "can't find config for $c";
+
+       warn "# c = ",dump($c) if $debug;
+
+       my $keep_every_days;
+       my $older_than_days;
+       foreach ( keys %$c ) {
+               $older_than_days = $_;
+               $keep_every_days = $c->{$_};
+               warn "## $host $age > $older_than_days" if $debug;
+               last if $age > $older_than_days;
+       }
+
+       my $config_applied = '';
+
+       if ( $age > $older_than_days ) {
+
+               $config_applied = "> $older_than_days keep $keep_every_days";
 
                $last_backup->{$host} ||= $date;
                $last = $last_backup->{$host}->delta_days( $date )->delta_days;
 
-               if ( $last && $last < 5 ) {
-                       $op = '-';
+               if ( $last && $last < $keep_every_days ) {
+                       $op = 'D';
                } else {
-                       $op = '+';
+                       $op = ' ';
                        $last_backup->{$host} = $date;
                }
+       } else {
+               $config_applied = 'none';
        }
 
-       warn "$op $name\t$used\t$refer\t$age\t$last\n";
+       print "$op $name\t$used\t$refer\t$age\t$last\t$config_applied\n";
 
-       system "zfs destroy $name" if $op eq '-' && @ARGV;
+       system "zfs destroy $name" if $op eq 'D' && @ARGV;
 }