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

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

  ViewVC Help
Powered by ViewVC 1.1.26