Subversion Repositories sysadmin_scripts

Rev

Rev 96 | Rev 98 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 96 Rev 97
Line 5... Line 5...
5
 
5
 
6
use Data::Dumper;
6
use Data::Dumper;
7
use Time::Local;
7
use Time::Local;
8
use POSIX qw(strftime);
8
use POSIX qw(strftime);
9
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian, BSD Systems: cpan -i YAML::Tiny
9
use YAML::Tiny; # apt-get libyaml-tiny-perl under debian, BSD Systems: cpan -i YAML::Tiny
-
 
10
use Hash::Merge::Simple qw/ merge clone_merge /; # libhash-merge-simple-perl
10
 
11
 
11
 
12
 
12
# globals
13
# globals
13
my $CONFIG_FILE_NAME = 'snapShot.yaml';
14
my $CONFIG_FILE_NAME = 'snapShot.yaml';
14
 
15
 
Line 89... Line 90...
89
# will convert something like 1 day to the number of seconds (86400) for math.
90
# will convert something like 1 day to the number of seconds (86400) for math.
90
# month and year are approximations (30.5 day = a month, 365.2425 days is a year)
91
# month and year are approximations (30.5 day = a month, 365.2425 days is a year)
91
sub period2seconds {
92
sub period2seconds {
92
   my ($count, $unit) = ( shift =~ m/\s*(\d+)\s*([a-z]+)\s*/i );
93
   my ($count, $unit) = ( shift =~ m/\s*(\d+)\s*([a-z]+)\s*/i );
93
   $unit = lc $unit;
94
   $unit = lc $unit;
94
   if ( unit eq 'hour' ) {
95
   if ( $unit eq 'hour' ) {
95
      $count *= 3600;
96
      $count *= 3600;
96
   } elsif ( $unit eq 'day' ) {
97
   } elsif ( $unit eq 'day' ) {
97
      $count *= 86400;
98
      $count *= 86400;
98
   } elsif ( $unit eq 'week' ) {
99
   } elsif ( $unit eq 'week' ) {
99
      $count *= 864000 * 7;
100
      $count *= 864000 * 7;
Line 107... Line 108...
107
   return $count;
108
   return $count;
108
}
109
}
109
 
110
 
110
# Merges datasets, snapshots and some stuff from the configuration into the datasets
111
# Merges datasets, snapshots and some stuff from the configuration into the datasets
111
# hash
112
# hash
112
sub merge {
113
sub mergeData {
113
   my ($datasets,$snapshots,$config) = @_;
114
   my ($datasets,$snapshots,$config) = @_;
114
   my $confKeys = $config->{'datasets'};
115
   my $confKeys = $config->{'datasets'};
115
   foreach my $thisDataset ( keys %$datasets ) {
116
   foreach my $thisDataset ( keys %$datasets ) {
116
      foreach my $conf (keys %$confKeys ) {
117
      foreach my $conf (keys %$confKeys ) {
117
         if ( $thisDataset =~ m/^$conf$/ ) {
118
         if ( $thisDataset =~ m/^$conf$/ ) {
Line 131... Line 132...
131
            # delete the snapshot
132
            # delete the snapshot
132
            delete $snapshots->{$snapshot};
133
            delete $snapshots->{$snapshot};
133
         } # if
134
         } # if
134
      } # foreach
135
      } # foreach
135
   } # foreach
136
   } # foreach
136
} # sub merge
137
} # sub mergeData
137
   
138
   
138
sub checkRetention {
139
sub checkRetention {
139
   my ( $retentionPeriod, $recursive, $snapshots, $now ) = @_;
140
   my ( $retentionPeriod, $recursive, $snapshots, $now ) = @_;
140
   my @toDelete;
141
   my @toDelete;
141
   foreach my $thisSnapshot ( keys %$snapshots ) {
142
   foreach my $thisSnapshot ( keys %$snapshots ) {
Line 206... Line 207...
206
      }
207
      }
207
   }
208
   }
208
   return 0;
209
   return 0;
209
}
210
}
210
 
211
 
-
 
212
&readConfig();
-
 
213
 
211
# grab the time once
214
# grab the time once
212
my $now = time;
215
my $now = time;
213
# create the string to be used for all snapshots, using $now and the template provided
216
# create the string to be used for all snapshots, using $now and the template provided
214
my $snapshotName = '@' . strftime($config->{'snapshot'}->{'template'},localtime $now);
217
my $snapshotName = '@' . strftime($config->{'snapshot'}->{'template'},localtime $now);
215
# Create the dataset regex for later use 
218
# Create the dataset regex for later use 
Line 223... Line 226...
223
my $dataSets = &getListing( $config, $config->{'dataset_regex'}, 'zfs list'  );
226
my $dataSets = &getListing( $config, $config->{'dataset_regex'}, 'zfs list'  );
224
# and, find all snapshots that match
227
# and, find all snapshots that match
225
my $snapshots = &getListing( $config, $config->{'snapshot_regex'}, 'zfs list -t snapshot'  );
228
my $snapshots = &getListing( $config, $config->{'snapshot_regex'}, 'zfs list -t snapshot'  );
226
# get the date/time of the snapshots and store them in the hash
229
# get the date/time of the snapshots and store them in the hash
227
&parseSnapshots($snapshots, $config );
230
&parseSnapshots($snapshots, $config );
228
# merge the snapshots into the datasets for convenience
231
# mergeData the snapshots into the datasets for convenience
229
&merge( $dataSets, $snapshots, $config );
232
&mergeData( $dataSets, $snapshots, $config );
230
# Now, let's do the actual processing
233
# Now, let's do the actual processing
231
my @commands  = &process( $dataSets, $now, $snapshotName, &period2seconds( $config->{'slop'} ) );
234
my @commands  = &process( $dataSets, $now, $snapshotName, &period2seconds( $config->{'slop'} ) );
232
my $errors;
235
my $errors;
233
print $errors if $errors = &run( $config->{'TESTING'}, @commands );
236
print $errors if $errors = &run( $config->{'TESTING'}, @commands );
234
   
237