Subversion Repositories sysadmin_scripts

Rev

Rev 168 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
166 rodolico 1
#! /usr/bin/env perl
2
 
3
use strict;
4
use warnings;
5
 
6
use Data::Dumper;
7
 
8
my $source = shift;
9
my $target = shift;
10
 
11
die "Usage: replicate source target\n" unless $source && $target;
12
 
13
my $config = {
14
   # compile the regex
15
   'pattern' => qr/auto-\d{4}-\d{2}-\d{2}_\d{2}-\d{2}/
16
   };
17
 
18
sub parseDataSet {
19
   my $data = shift;
20
   my %return;
21
   my ( $server, $dataset ) = split( ':', $data );
22
   if ( $dataset ) { # they passed a server:dataset
23
      $return{'server'} = $server;
24
      $return{'dataset'} = $dataset;
25
   } else { # only passing in dataset, so assume localhost
26
      $return{'server'} = '';
27
      $return{'dataset'} = $server;
28
   }
29
   return \%return;
30
}
31
 
32
# runs a command, redirecting stderr to stdout (which it ignores)
33
# then returns 0 and $output on success.
34
# if error, returns error code and string describing error
35
sub run {
36
   my $command = shift;
37
   my $output = qx/$command 2>&1/;
38
   if ($? == -1) {
39
      return (-1,"failed to execute: $!");
40
   } elsif ($? & 127) {
41
      return ($?, sprintf "child died with signal %d, %s coredump",
42
        ($? & 127),  ($? & 128) ? 'with' : 'without' );
43
   } else {
44
      return ($? >> 8, sprintf "child exited with value %d", $? >> 8 ) if $? >> 8;
45
   }
46
   return (0,$output);
47
}
48
 
49
 
50
sub getSnaps {
51
   my ($config,$pattern) = @_;
52
   my %return;
53
   # actual command to run to get all snapshots, recursively, of the dataset
54
   my $command = 'zfs list -r -t snap ' . $config->{'dataset'};
55
   $command = "ssh $config->{server} '$command'" if $config->{'server'};
56
   my ($error, $output ) = &run( $command );
57
   die $output if $error;
58
   my @snaps = split( "\n", $output );
59
   chomp @snaps;
60
   for (my $i = 0; $i < @snaps; $i++ ) {
61
      # parse out the space delmited fields
62
      my ($fullname, $used, $avail, $refer, $mount) = split( /\s+/, $snaps[$i] );
63
      # break the name into dataset and snapname
64
      my ($dataset, $snap) = split( '@', $fullname );
65
      # remove the root dataset name
66
      $dataset =~ s/^$config->{'dataset'}//;
67
      # skip anything not matching our regex
68
      next unless $pattern && $snap && $snap =~ m/$pattern/;
69
      $return{$dataset}{'snaps'}{$snap}{'refer'} = $refer;
70
      $return{$dataset}{'snaps'}{$snap}{'used'} = $used;
71
   }
72
   return \%return;
73
}
74
 
75
sub diffSnaps {
76
   my ( $source, $target ) = @_;
77
   my @source = sort keys %$source;
78
   my @target = sort keys %$target;
79
#   print "===Source\n" . join( "\n", @source ) . "\n===Target\n" . join( "\n", @target ) . "\n";
80
 
81
   my $s = 0;
82
   my $t = 0;
83
   my %return;
84
   $return{'deleteTarget'} = [];
85
   $return{'addTarget'} = [];
86
   $return{'lastMatch'} = 0;
87
   $return{'finalSync'} = 0;
88
   while ( $s < @source && $t < @target ) {
89
      if ( $source[$s] eq $target[$t] ) { # matchies, just keep going
90
#         print "Source $s [$source[$s]] matches target $t [$target[$t]]\n";
91
         $return{'lastMatch'} = $source[$s]; # keep track of the largest match
92
         $s++; $t++;
93
      } elsif ( $target[$t] ne $source[$s] ) { # we are processing stuff that needs to be deleted on target
94
         push @{$return{'deleteTarget'}}, $target[$t];
95
#         print "Adding delete target $t [$target[$t]]\n";
96
         $t++;
97
      }
98
   }
99
   die "Could not reconcile snapshots, ran out of source too soon\n" if $s > @source;
100
   # put a value into finalSync to make sure there is one. If we do not have any sync
101
   # to do, final and lastMatch will be the same
102
   $return{'finalSync'} = $return{'lastMatch'};
103
   while ( $s < @source ) {
104
      push @{$return{'addTarget'}}, $source[$s];
105
      $return{'finalSync'} = $source[$s];
106
      $s++;
107
   }
108
#   die Dumper( \%return );
109
   return \%return;
110
}
111
 
112
sub arrayEquals {
113
   my ($a, $b ) = @_;
114
   return 0 unless @{$a} == @{$b}; # they are different sizes
115
   for ( my $i = 0; $i < @$a; $i++ ) {
116
      if ( $$a[$i] ne $$b[$i] ) {
117
         print STDERR "No Match!\n" . join( "\t", @$a ) . "\n" . join( "\t", @$b ) . "\n";
118
         return 0;
119
      }
120
   }
121
   return 1;
122
}
123
 
124
sub createCommands {
125
   my $config = shift;
126
   my @return;
127
   # check for new snapshots to sync
128
   if ( $config->{'actions'}->{'lastMatch'} ne $config->{'actions'}->{'finalSync'} ) {
129
      # first create the replicate command. The send command request recursion (-R)
130
      # and the range of snapshots including all intermediate ones (-I)
131
      my $sourceCommand = 'zfs send -RI ';
132
      $sourceCommand .= $config->{'source'}->{'dataset'} . '@' . $config->{'actions'}->{'lastMatch'} . ' ';
133
      $sourceCommand .= $config->{'source'}->{'dataset'} . '@' . $config->{'actions'}->{'finalSync'};
134
      $sourceCommand = "ssh $config->{source}->{server} '$sourceCommand'" if $config->{'source'}->{'server'};
135
 
136
      my $targetCommand = 'zfs receive -v ';
137
      $targetCommand .= $config->{'target'}->{'dataset'};
138
      $targetCommand = "ssh $config->{target}->{server} '$sourceCommand'" if $config->{'target'}->{'server'};
139
      push @return, $sourceCommand . ' | ' . $targetCommand;
140
   } else {
141
      push @return, '# No action, all synced';
142
   }
143
   # now, check for snapshots to remove
144
   if ( $config->{'actions'}->{'deleteTarget'} ) {
145
      for ( my $i = 0; $i < @{$config->{'actions'}->{'deleteTarget'}}; $i++ ) {
146
         push @return, 'zfs destroy -r ' . {$config->{'actions'}->{'deleteTarget'}}->[$i];
147
      }
148
   }
149
 
150
   return \@return;
151
}
152
 
153
 
154
 
155
sub calculate {
156
   my $config = shift;
157
   my $return;
158
   my $allMatch;
159
   my $lastMatch;
160
   foreach my $dataset ( sort keys %{$config->{'source'}->{'snapshots'}} ) {
161
      next unless $dataset;
162
      die "No matching target for $dataset\n" unless $config->{'target'}->{'snapshots'}->{$dataset};
163
      $return->{$dataset} = &diffSnaps( 
164
         $config->{'source'}->{'snapshots'}->{$dataset}->{'snaps'},
165
         $config->{'target'}->{'snapshots'}->{$dataset}->{'snaps'}
166
      );
167
      $allMatch = $return->{$dataset} unless $allMatch;
168
      next;
169
      unless ( 
170
         &arrayEquals( $return->{'allMatch'}->{'deleteTarget'}, $return->{$dataset}->{'deleteTarget'} ) &&
171
         &arrayEquals( $return->{'allMatch'}->{'addTarget'},    $return->{$dataset}->{'addTarget'} )
172
         ) {
173
         warn "Warning: dataset $dataset does not match\n";
174
         last;
175
      }
176
   }
177
   #print Dumper( $allMatch );
178
   $return->{'lastMatch'} = $allMatch->{'lastMatch'};
179
   $return->{'finalSync'} = $allMatch->{'finalSync'};
180
   return $return;
181
   #print Dumper( $return ) . "\n"; die;
182
}
183
 
184
$config->{'source'} = &parseDataSet( $source );
185
$config->{'target'} = &parseDataSet( $target );
186
 
187
# both source and target can not have a server portion; one must be local
188
die "Source and Target can not both be remote\n" if $config->{'source'}->{'server'} && $config->{'target'}->{'server'};
189
 
190
#die Dumper( $config );
191
 
192
$config->{'source'}->{'snapshots'} = &getSnaps( $config->{'source'}, $config->{'pattern'} );
193
$config->{'target'}->{'snapshots'} = &getSnaps( $config->{'target'}, $config->{'pattern'} );
194
 
195
$config->{'actions'} = &calculate( $config );
196
 
197
my $commands = &createCommands( $config );
198
for ( my $i = 0; $i < @{$commands}; $i++ ) {
199
   print $$commands[$i] . "\n";
200
}
201
 
202
#print Dumper( $config );
203
 
204
1;