| 2 |
rodolico |
1 |
#! /usr/bin/env perl
|
|
|
2 |
|
| 4 |
rodolico |
3 |
# very simple script to replicate a ZFS snapshot to another server.
|
|
|
4 |
# no fancy bells and whistles, does not create snapshots, and does
|
|
|
5 |
# not prune them. No major error checking either
|
|
|
6 |
|
| 2 |
rodolico |
7 |
use strict;
|
|
|
8 |
use warnings;
|
|
|
9 |
|
|
|
10 |
use Data::Dumper;
|
| 4 |
rodolico |
11 |
use Getopt::Long;
|
|
|
12 |
Getopt::Long::Configure ("bundling");
|
| 2 |
rodolico |
13 |
|
| 4 |
rodolico |
14 |
# create our configuration, with some defaults
|
|
|
15 |
# these are overridden by command line stuff
|
| 2 |
rodolico |
16 |
my $config = {
|
| 4 |
rodolico |
17 |
# the source, where we're coming from
|
|
|
18 |
'source' => '',
|
|
|
19 |
# the target, where we want to replicate to
|
|
|
20 |
'target' => '',
|
| 2 |
rodolico |
21 |
# compile the regex
|
| 4 |
rodolico |
22 |
'filter' => qr/(\d{4}.\d{2}.\d{2}.\d{2}.\d{2})/,
|
|
|
23 |
# if non-zero, just display the commands we'd use, don't run them
|
|
|
24 |
'dryrun' => 0,
|
|
|
25 |
# whether to do all child datasets also (default)
|
|
|
26 |
'recurse' => 1,
|
|
|
27 |
# show more information
|
|
|
28 |
'verbose' => 0
|
| 2 |
rodolico |
29 |
};
|
|
|
30 |
|
|
|
31 |
sub parseDataSet {
|
|
|
32 |
my $data = shift;
|
|
|
33 |
my %return;
|
|
|
34 |
my ( $server, $dataset ) = split( ':', $data );
|
|
|
35 |
if ( $dataset ) { # they passed a server:dataset
|
|
|
36 |
$return{'server'} = $server;
|
|
|
37 |
$return{'dataset'} = $dataset;
|
|
|
38 |
} else { # only passing in dataset, so assume localhost
|
|
|
39 |
$return{'server'} = '';
|
|
|
40 |
$return{'dataset'} = $server;
|
|
|
41 |
}
|
|
|
42 |
return \%return;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
# runs a command, redirecting stderr to stdout (which it ignores)
|
|
|
46 |
# then returns 0 and $output on success.
|
|
|
47 |
# if error, returns error code and string describing error
|
|
|
48 |
sub run {
|
|
|
49 |
my $command = shift;
|
|
|
50 |
my $output = qx/$command 2>&1/;
|
|
|
51 |
if ($? == -1) {
|
|
|
52 |
return (-1,"failed to execute: $!");
|
|
|
53 |
} elsif ($? & 127) {
|
|
|
54 |
return ($?, sprintf "child died with signal %d, %s coredump",
|
|
|
55 |
($? & 127), ($? & 128) ? 'with' : 'without' );
|
|
|
56 |
} else {
|
|
|
57 |
return ($? >> 8, sprintf "child exited with value %d", $? >> 8 ) if $? >> 8;
|
|
|
58 |
}
|
|
|
59 |
return (0,$output);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
sub getSnaps {
|
|
|
64 |
my ($config,$pattern) = @_;
|
|
|
65 |
my %return;
|
|
|
66 |
# actual command to run to get all snapshots, recursively, of the dataset
|
|
|
67 |
my $command = 'zfs list -r -t snap ' . $config->{'dataset'};
|
|
|
68 |
$command = "ssh $config->{server} '$command'" if $config->{'server'};
|
|
|
69 |
#die "$command\n";
|
|
|
70 |
my ($error, $output ) = &run( $command );
|
|
|
71 |
#die "Error running $command with output\n$output" if $error;
|
|
|
72 |
my @snaps = split( "\n", $output );
|
|
|
73 |
chomp @snaps;
|
|
|
74 |
for (my $i = 0; $i < @snaps; $i++ ) {
|
|
|
75 |
# parse out the space delmited fields
|
|
|
76 |
my ($fullname, $used, $avail, $refer, $mount) = split( /\s+/, $snaps[$i] );
|
|
|
77 |
# break the name into dataset and snapname
|
|
|
78 |
my ($dataset, $snap) = split( '@', $fullname );
|
|
|
79 |
# remove the root dataset name
|
|
|
80 |
$dataset =~ s/^$config->{'dataset'}//;
|
|
|
81 |
# skip anything not matching our regex
|
|
|
82 |
next unless $pattern && $snap && $snap =~ m/$pattern/;
|
|
|
83 |
# grab the matched key
|
|
|
84 |
$return{$dataset}{'snaps'}{$snap}{'key'} = $1;
|
|
|
85 |
# and remove all non-numerics
|
|
|
86 |
$return{$dataset}{'snaps'}{$snap}{'key'} =~ s/[^0-9]//g;
|
|
|
87 |
# get the transfer size
|
|
|
88 |
$return{$dataset}{'snaps'}{$snap}{'refer'} = $refer;
|
|
|
89 |
# get the actual disk space used
|
|
|
90 |
$return{$dataset}{'snaps'}{$snap}{'used'} = $used;
|
|
|
91 |
}
|
|
|
92 |
return \%return;
|
|
|
93 |
}
|
|
|
94 |
|
| 4 |
rodolico |
95 |
#sub diffSnaps {
|
|
|
96 |
# my ( $config->{'source'}, $config->{'target'} ) = @_;
|
|
|
97 |
# my @source = sort keys %$config->{'source'};
|
|
|
98 |
# my @target = sort keys %$config->{'target'};
|
| 2 |
rodolico |
99 |
# print "===Source\n" . join( "\n", @source ) . "\n===Target\n" . join( "\n", @target ) . "\n";
|
|
|
100 |
|
| 4 |
rodolico |
101 |
# my $s = 0;
|
|
|
102 |
# my $t = 0;
|
|
|
103 |
# my %return;
|
|
|
104 |
# $return{'deleteTarget'} = [];
|
|
|
105 |
# $return{'addTarget'} = [];
|
|
|
106 |
# $return{'lastMatch'} = 0;
|
|
|
107 |
# $return{'finalSync'} = 0;
|
|
|
108 |
# while ( $s < @source && $t < @target ) {
|
|
|
109 |
# if ( $config->{'source'}[$s] eq $config->{'target'}[$t] ) { # matchies, just keep going
|
|
|
110 |
# print "Source $s [$config->{'source'}[$s]] matches target $t [$config->{'target'}[$t]]\n";
|
|
|
111 |
# $return{'lastMatch'} = $config->{'source'}[$s]; # keep track of the largest match
|
|
|
112 |
# $s++; $t++;
|
|
|
113 |
# } elsif ( $config->{'target'}[$t] ne $config->{'source'}[$s] ) { # we are processing stuff that needs to be deleted on target
|
|
|
114 |
# push @{$return{'deleteTarget'}}, $config->{'target'}[$t];
|
|
|
115 |
# print "Adding delete target $t [$config->{'target'}[$t]]\n";
|
|
|
116 |
# $t++;
|
|
|
117 |
# }
|
|
|
118 |
# }
|
|
|
119 |
# die "Could not reconcile snapshots, ran out of source too soon\n" if $s > @source;
|
|
|
120 |
# # put a value into finalSync to make sure there is one. If we do not have any sync
|
|
|
121 |
# # to do, final and lastMatch will be the same
|
|
|
122 |
# $return{'finalSync'} = $return{'lastMatch'};
|
|
|
123 |
# while ( $s < @source ) {
|
|
|
124 |
# push @{$return{'addTarget'}}, $config->{'source'}[$s];
|
|
|
125 |
# $return{'finalSync'} = $config->{'source'}[$s];
|
|
|
126 |
# $s++;
|
|
|
127 |
# }
|
| 2 |
rodolico |
128 |
# die Dumper( \%return );
|
| 4 |
rodolico |
129 |
# return \%return;
|
|
|
130 |
#}
|
| 2 |
rodolico |
131 |
|
| 4 |
rodolico |
132 |
#sub arrayEquals {
|
|
|
133 |
# my ($a, $b ) = @_;
|
|
|
134 |
# return 0 unless @{$a} == @{$b}; # they are different sizes
|
|
|
135 |
# for ( my $i = 0; $i < @$a; $i++ ) {
|
|
|
136 |
# if ( $$a[$i] ne $$b[$i] ) {
|
|
|
137 |
# print STDERR "No Match!\n" . join( "\t", @$a ) . "\n" . join( "\t", @$b ) . "\n";
|
|
|
138 |
# return 0;
|
|
|
139 |
# }
|
|
|
140 |
# }
|
|
|
141 |
# return 1;
|
|
|
142 |
#}
|
| 2 |
rodolico |
143 |
|
|
|
144 |
sub createCommands {
|
| 5 |
rodolico |
145 |
my ( $lastSource, $lastTarget, $config ) = @_;
|
| 2 |
rodolico |
146 |
my @return;
|
|
|
147 |
# check for new snapshots to sync
|
| 5 |
rodolico |
148 |
if ( $lastSource ne $lastTarget ) {
|
|
|
149 |
my $snapRange = $config->{'target'}->{'dataset'} . '@' . $config->{'target'};
|
|
|
150 |
my $flags = ( $config->{'recurse'} ? 'R' : '' ) .
|
|
|
151 |
( $config->{'verbose'} == 2 ? 'v' : '' );
|
|
|
152 |
my $source = $config->{'source'}->{'dataset'} . '@' . $lastSource;
|
|
|
153 |
my $target = $config->{'target'}->{'dataset'};
|
| 2 |
rodolico |
154 |
# first create the replicate command. The send command request recursion (-R)
|
|
|
155 |
# and the range of snapshots including all intermediate ones (-I)
|
| 5 |
rodolico |
156 |
my $command = 'zfs send -RI ';
|
|
|
157 |
$command .= $config->{'target'}->{'dataset'} . '@' . $config->{'target'} . ' ';
|
|
|
158 |
$command .= $config->{'source'}->{'dataset'} . '@' . $config->{'source'};
|
|
|
159 |
$command = "ssh $config->{source}->{server} '$config->{'source'}Command'" if $config->{'source'}->{'server'};
|
| 2 |
rodolico |
160 |
|
| 5 |
rodolico |
161 |
$command = 'zfs receive -v ';
|
|
|
162 |
$command .= $config->{'target'}->{'dataset'};
|
|
|
163 |
$command = "ssh $config->{target}->{server} '$config->{'source'}Command'" if $config->{'target'}->{'server'};
|
|
|
164 |
push @return, $command . ' | ' . $command;
|
| 2 |
rodolico |
165 |
} else {
|
|
|
166 |
push @return, '# Nothing new to sync';
|
|
|
167 |
}
|
|
|
168 |
# now, check for snapshots to remove
|
|
|
169 |
#if ( $config->{'actions'}->{'deleteTarget'} ) {
|
|
|
170 |
# my $delete = $config->{'actions'}->{'deleteTarget'};
|
|
|
171 |
# foreach my $ds ( @$delete ) {
|
|
|
172 |
# push @return, "zfs destroy -r $config->{target}->{'dataset'}\@$ds";
|
|
|
173 |
# }
|
|
|
174 |
#} else {
|
|
|
175 |
# push @return, "# No old snapshots to be removed";
|
|
|
176 |
#}
|
|
|
177 |
return \@return;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
# find the last snapshot in a hash. The hash is assumed to have a subkey
|
|
|
181 |
# 'key'. look for the largest subkey, and return the key for it
|
|
|
182 |
sub getLastSnapshot {
|
|
|
183 |
my $snapList = shift;
|
|
|
184 |
my $lastKey = 0;
|
|
|
185 |
my $lastSnap = '';
|
|
|
186 |
foreach my $snap ( keys %$snapList ) {
|
|
|
187 |
if ( $snapList->{$snap}->{'key'} > $lastKey ) {
|
|
|
188 |
$lastKey = $snapList->{$snap}->{'key'};
|
|
|
189 |
$lastSnap = $snap;
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
return $lastSnap;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
|
|
|
196 |
sub calculate {
|
|
|
197 |
my $config = shift;
|
|
|
198 |
|
|
|
199 |
my @warnings;
|
|
|
200 |
|
|
|
201 |
# find the last snapshot date in each dataset, on each target
|
|
|
202 |
foreach my $machine ( 'source', 'target' ) {
|
|
|
203 |
$config->{$machine}->{'last'} = 0; # track the last entry in all children in dataset
|
|
|
204 |
$config->{$machine}->{'allOk'} = 1; # assumed to be true, becomes false if some children do not have snapshots
|
|
|
205 |
foreach my $child ( keys %{ $config->{$machine}->{'snapshots'} } ) {
|
|
|
206 |
$config->{$machine}->{'snapshots'}->{$child}->{'last'} =
|
|
|
207 |
&getLastSnapshot( $config->{$machine}->{'snapshots'}->{$child}->{'snaps'} );
|
|
|
208 |
# set the machine last if we haven't done so yet
|
|
|
209 |
$config->{$machine}->{'last'} = $config->{$machine}->{'snapshots'}->{$child}->{'last'} unless $config->{$machine}->{'last'};
|
|
|
210 |
# keep track of the last snapshot for each set
|
|
|
211 |
if ( $config->{$machine}->{'last'} ne $config->{$machine}->{'snapshots'}->{$child}->{'last'} ) {
|
|
|
212 |
$config->{$machine}->{'allOk'} = 0;
|
|
|
213 |
push @warnings, "Warning: $machine does not have consistent snapshots at $child";;
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
# make sure the source has a corresponding snap for target->last
|
|
|
218 |
foreach my $child ( keys %{ $config->{'target'}->{'snapshots'} } ) {
|
|
|
219 |
if (! exists ($config->{'source'}->{'snapshots'}->{$child}->{'snaps'}->{$config->{'target'}->{'snapshots'}->{$child}->{'last'}} ) ) {
|
|
|
220 |
$config->{'source'}->{'allOk'} = 0;
|
|
|
221 |
push @warnings, "Warning: We do not have consistent snapshots";
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
my $return;
|
|
|
225 |
if ( $config->{'source'}->{'allOk'} and $config->{'target'}->{'allOk'} ) { # whew, they match
|
|
|
226 |
return( $config->{'source'}->{'last'}, $config->{'target'}->{'last'}, \@warnings );
|
|
|
227 |
} else {
|
|
|
228 |
return( '','',\@warnings);
|
|
|
229 |
}
|
| 4 |
rodolico |
230 |
} # sub calculate
|
| 2 |
rodolico |
231 |
|
| 4 |
rodolico |
232 |
GetOptions( $config,
|
|
|
233 |
'source|s=s',
|
|
|
234 |
'target|t=s',
|
|
|
235 |
'filter|f=s',
|
|
|
236 |
'dryrun|n',
|
|
|
237 |
'recurse|r',
|
|
|
238 |
'verbose|v',
|
|
|
239 |
'help|h'
|
|
|
240 |
);
|
| 2 |
rodolico |
241 |
|
| 4 |
rodolico |
242 |
# allow them to use positional, without flags, such as
|
|
|
243 |
# replicate source target --filter='regex' -n
|
|
|
244 |
$config->{'source'} = shift unless $config->{'source'};
|
|
|
245 |
$config->{'target'} = shift unless $config->{'target'};
|
|
|
246 |
die "You must enter a source and a target, at a minimum\n" unless $config->{'source'} && $config->{'target'};
|
|
|
247 |
|
|
|
248 |
# WARNING: this converts source and targets from a string to a hash
|
|
|
249 |
# '10.0.0.1:data/set' becomes ( 'server' => '10.0.0.1', 'dataset' => 'data/set')
|
|
|
250 |
# and 'data/set' becomes ( 'server' => '', 'dataset' => 'data/set')
|
|
|
251 |
$config->{'source'} = &parseDataSet( $config->{'source'} );
|
|
|
252 |
$config->{'target'} = &parseDataSet( $config->{'target'} );
|
|
|
253 |
|
| 2 |
rodolico |
254 |
# both source and target can not have a server portion; one must be local
|
|
|
255 |
die "Source and Target can not both be remote\n" if $config->{'source'}->{'server'} && $config->{'target'}->{'server'};
|
|
|
256 |
|
| 4 |
rodolico |
257 |
$config->{'source'}->{'snapshots'} = &getSnaps( $config->{'source'}, $config->{'filter'} );
|
|
|
258 |
$config->{'target'}->{'snapshots'} = &getSnaps( $config->{'target'}, $config->{'filter'} );
|
| 2 |
rodolico |
259 |
|
| 4 |
rodolico |
260 |
# we sync from last snap on target machine to last snap on source machine
|
| 2 |
rodolico |
261 |
my ( $lastSource, $lastTarget ) = &calculate( $config );
|
|
|
262 |
|
|
|
263 |
#print Dumper( $config ) . "\nSource = $lastSource\nTarget = $lastTarget\n"; die;
|
|
|
264 |
|
| 4 |
rodolico |
265 |
# actually creates the commands to do the replicate
|
| 2 |
rodolico |
266 |
my $commands = &createCommands( $lastSource, $lastTarget, $config );
|
|
|
267 |
for ( my $i = 0; $i < @{$commands}; $i++ ) {
|
| 4 |
rodolico |
268 |
print "$$commands[$i]\n" if $config->{'verbose'} or $config->{'dryrun'};
|
|
|
269 |
if ( $config->{'dryrun'} ) {
|
| 2 |
rodolico |
270 |
print "Dry Run\n";
|
|
|
271 |
} else {
|
|
|
272 |
print qx/$$commands[$i]/ if $$commands[$i] =~ m/^[a-zA-Z]/;
|
|
|
273 |
}
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
#print Dumper( $config );
|
|
|
277 |
|
|
|
278 |
1;
|