/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 117 - (hide annotations)
Thu Jun 25 11:07:26 2009 UTC (14 years, 10 months ago) by dpavlin
File MIME type: text/plain
File size: 1672 byte(s)
added multiple rules for each snapshot in backup

1 dpavlin 107 #!/usr/bin/perl
2    
3     use warnings;
4     use strict;
5    
6     use DateTime;
7 dpavlin 117 use Data::Dump qw/dump/;
8 dpavlin 107
9 dpavlin 117 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 dpavlin 107 my $now = DateTime->now();
26    
27 dpavlin 108 my $last_backup;
28    
29 dpavlin 107 open(my $fs, '-|', 'zfs list -H');
30     while(<$fs>) {
31     chomp;
32     my ( $name, $used, $avail, $refer, $mountpoint ) = split(/\t/,$_,6);
33    
34 dpavlin 108 next unless $name =~ m{(.+)@(\d\d\d\d)-(\d\d)-(\d\d)};
35 dpavlin 107
36 dpavlin 108 my $host = $1;
37 dpavlin 107
38 dpavlin 108 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 dpavlin 117 my $c = (grep { $host =~ m{\Q$_\E} } keys %$config)[0];
46     $c = 'default' unless defined $c;
47 dpavlin 108
48 dpavlin 117 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 dpavlin 108 $last_backup->{$host} ||= $date;
73     $last = $last_backup->{$host}->delta_days( $date )->delta_days;
74    
75 dpavlin 117 if ( $last && $last < $keep_every_days ) {
76     $op = 'D';
77 dpavlin 108 } else {
78 dpavlin 117 $op = ' ';
79 dpavlin 108 $last_backup->{$host} = $date;
80     }
81 dpavlin 117 } else {
82     $config_applied = 'none';
83 dpavlin 108 }
84    
85 dpavlin 117 print "$op $name\t$used\t$refer\t$age\t$last\t$config_applied\n";
86 dpavlin 108
87 dpavlin 117 system "zfs destroy $name" if $op eq 'D' && @ARGV;
88 dpavlin 107 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26