/[sysadmin-cookbook]/recepies/zfs/zfs-expire-snapshot.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /recepies/zfs/zfs-expire-snapshot.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 107 by dpavlin, Sat Jun 6 13:21:07 2009 UTC revision 117 by dpavlin, Thu Jun 25 11:07:26 2009 UTC
# Line 4  use warnings; Line 4  use warnings;
4  use strict;  use strict;
5    
6  use DateTime;  use DateTime;
7    use Data::Dump qw/dump/;
8    
9    my $debug = 0;
10    
11    my $config = {
12            'default' => {
13                    14 => 5,
14                    30 => 10,
15                    60 => 30,
16            },
17            '212052' => {   # koha-dev
18                    7 => 10,
19            },
20            '212056' => {   # webpac2
21                    7 => 5,
22            }
23    };
24    
25  my $now = DateTime->now();  my $now = DateTime->now();
26    
27    my $last_backup;
28    
29  open(my $fs, '-|', 'zfs list -H');  open(my $fs, '-|', 'zfs list -H');
30  while(<$fs>) {  while(<$fs>) {
31          chomp;          chomp;
32          my ( $name, $used, $avail, $refer, $mountpoint ) = split(/\t/,$_,6);          my ( $name, $used, $avail, $refer, $mountpoint ) = split(/\t/,$_,6);
33    
34          next unless $name =~ m{@(\d\d\d\d)-(\d\d)-(\d\d)};          next unless $name =~ m{(.+)@(\d\d\d\d)-(\d\d)-(\d\d)};
35    
36            my $host = $1;
37    
38            my $date = DateTime->new( year => $2, month => $3, day => $4 );
39    
40            my $age = $now->delta_days( $date )->delta_days;
41    
42            my $op = ' ';
43            my $last = 0;
44    
45            my $c = (grep { $host =~ m{\Q$_\E} } keys %$config)[0];
46            $c = 'default' unless defined $c;
47    
48            warn "# config: $c\n" if $debug;
49    
50            my $h = $host;
51            $h =~ s{,+/([^/]+)$}{}; # just hostname without path
52    
53            $c = $config->{$c} || die "can't find config for $c";
54    
55            warn "# c = ",dump($c) if $debug;
56    
57            my $keep_every_days;
58            my $older_than_days;
59            foreach ( keys %$c ) {
60                    $older_than_days = $_;
61                    $keep_every_days = $c->{$_};
62                    warn "## $host $age > $older_than_days" if $debug;
63                    last if $age > $older_than_days;
64            }
65    
66            my $config_applied = '';
67    
68            if ( $age > $older_than_days ) {
69    
70                    $config_applied = "> $older_than_days keep $keep_every_days";
71    
72                    $last_backup->{$host} ||= $date;
73                    $last = $last_backup->{$host}->delta_days( $date )->delta_days;
74    
75                    if ( $last && $last < $keep_every_days ) {
76                            $op = 'D';
77                    } else {
78                            $op = ' ';
79                            $last_backup->{$host} = $date;
80                    }
81            } else {
82                    $config_applied = 'none';
83            }
84    
85          my $date = DateTime->new( year => $1, month => $2, day => $3 );          print "$op $name\t$used\t$refer\t$age\t$last\t$config_applied\n";
         my $age = $now->delta_days( $date );  
86    
87          warn "$name $used $refer [", $age->delta_days, " days]\n";          system "zfs destroy $name" if $op eq 'D' && @ARGV;
88  }  }

Legend:
Removed from v.107  
changed lines
  Added in v.117

  ViewVC Help
Powered by ViewVC 1.1.26