| 2 |
rodolico |
1 |
#! /usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
use strict;
|
|
|
4 |
use warnings;
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
BEGIN {
|
|
|
8 |
use FindBin;
|
|
|
9 |
use File::Spec;
|
|
|
10 |
# use libraries from the directory this script is in
|
|
|
11 |
use Cwd 'abs_path';
|
|
|
12 |
use File::Basename;
|
|
|
13 |
use lib dirname( abs_path( __FILE__ ) );
|
|
|
14 |
}
|
|
|
15 |
|
|
|
16 |
use YAML::Tiny; # pkg install p5-YAML-Tiny-1.74
|
|
|
17 |
use Data::Dumper;
|
|
|
18 |
use Email::Simple; # cpan install Email::Simple
|
|
|
19 |
|
|
|
20 |
my $cwd = $FindBin::RealBin;
|
|
|
21 |
my $configFileName = $cwd . '/sync.yaml';
|
|
|
22 |
my $replicateScript = $cwd . '/replicate';
|
|
|
23 |
|
|
|
24 |
my $configuration;
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
# load Configuration File
|
|
|
28 |
# read the config file and return it
|
|
|
29 |
sub readConfig {
|
|
|
30 |
my $filename = shift;
|
|
|
31 |
die "Config file $filename not found: $!" unless -f $filename;
|
|
|
32 |
my $yaml = YAML::Tiny->new( {} );
|
|
|
33 |
if ( -f $filename ) {
|
|
|
34 |
$yaml = YAML::Tiny->read( $filename );
|
|
|
35 |
}
|
|
|
36 |
return $yaml->[0];
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
# this calls gshred which will overwrite the file 3 times, then
|
|
|
42 |
# remove it.
|
|
|
43 |
# NOTE: this will not work on ZFS, since ZFS is CopyOnWrite (COW)
|
|
|
44 |
# so assuming /tmp is a ramdisk
|
|
|
45 |
sub shredFile {
|
|
|
46 |
my $filename = shift;
|
|
|
47 |
`/usr/local/bin/gshred -u -f -s 32 $filename`;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
# runs a command, redirecting stderr to stdout (which it ignores)
|
|
|
52 |
# then returns 0 on success.
|
|
|
53 |
# if error, returns string describing error
|
|
|
54 |
sub runCommand {
|
|
|
55 |
my $command = shift;
|
|
|
56 |
my $output = qx/$command 2>&1/;
|
|
|
57 |
if ($? == -1) {
|
|
|
58 |
return (-1, "failed to execute: $!" );
|
|
|
59 |
} elsif ($? & 127) {
|
|
|
60 |
return (-1,sprintf "child died with signal %d, %s coredump",
|
|
|
61 |
($? & 127), ($? & 128) ? 'with' : 'without');
|
|
|
62 |
} else {
|
|
|
63 |
return ( $?>>8, sprintf "child exited with value %d", $? >> 8 ) if $? >> 8;
|
|
|
64 |
}
|
|
|
65 |
return (0, $output);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
# grabs the encryption key from the remote server, and uses it to unlock the
|
|
|
69 |
# datasets, then mount the drives.
|
|
|
70 |
# a return of '' is success, anything else is an error
|
|
|
71 |
sub mountDrives {
|
|
|
72 |
my $configuration = shift;
|
|
|
73 |
return (0, 'No encrypted target found' ) unless defined( $configuration->{'target'}->{'encryptionKeyPath'} ) && $configuration->{'target'}->{'encryptionKeyPath'};
|
|
|
74 |
# try to grab the file from the remote machine
|
|
|
75 |
&runCommand( "scp $configuration->{remoteMachine}->{ip}:$configuration->{remoteMachine}->{encryptionKeyPath} $configuration->{localMachine}->{encryptionKeyPath}" );
|
|
|
76 |
# If we do not have the encryption key, we need to abort
|
|
|
77 |
return "Could not copy file $configuration->{remoteMachine}->{ip}:$configuration->{remoteMachine}->{encryptionKeyPath}, aborting"
|
|
|
78 |
unless -f $configuration->{'target'}->{'encryptionKeyPath'};
|
|
|
79 |
my $error = '';
|
|
|
80 |
my $output = '';
|
|
|
81 |
# load the key into zfs and unlock all volumes
|
|
|
82 |
($error,$output) = &runCommand( "zfs load-key -a" );
|
|
|
83 |
# finally, remount all of the zfs shares which need the key
|
|
|
84 |
($error,$output) = &runCommand( "zfs mount -a" ) unless $error;
|
|
|
85 |
# if we succeeded, we want to shred the keyfile
|
|
|
86 |
&shredFile( $configuration->{localMachine}->{encryptionKeyPath} ) if -f $configuration->{localMachine}->{encryptionKeyPath};
|
|
|
87 |
return $error;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
# a very simple mailer, using Email::Simple to just get status messages out
|
|
|
91 |
sub sendMail {
|
|
|
92 |
my ($message, $configuration, $subject ) = @_;
|
|
|
93 |
$configuration->{'email'}->{'notify'} = 'root' unless $configuration->{'email'}->{'notify'};
|
|
|
94 |
die "No message in outgoing message\n" unless $message;
|
|
|
95 |
my $email = Email::Simple->create(
|
|
|
96 |
header => [
|
|
|
97 |
To => $configuration->{'email'}->{'notify'},
|
|
|
98 |
Subject=> $configuration->{'email'}->{'subject'} . ( $subject ? " - $subject" : '' ),
|
|
|
99 |
From => $configuration->{'email'}->{'from'}
|
|
|
100 |
],
|
|
|
101 |
body => $message
|
|
|
102 |
);
|
|
|
103 |
$message = $email->as_string;
|
|
|
104 |
if ( $configuration->{'testing'} > 1 ) {
|
|
|
105 |
print "$message\n";
|
|
|
106 |
} else {
|
|
|
107 |
`echo '$message' | sendmail $configuration->{'email'}->{'notify'}`;
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
# checks to see if we should be in maintenance mode
|
|
|
112 |
# if $remoteMachine->{'maintenanceMode'} exists, set mode
|
|
|
113 |
# otherwise, wait localMachine->{'waittime'} minutes, then check
|
|
|
114 |
# $localMachine->{'maintenanceMode'}.
|
|
|
115 |
# if neither exists, begin sync
|
|
|
116 |
sub checkMaintenance {
|
|
|
117 |
my $configuration = shift;
|
|
|
118 |
return 0 unless # exit if maintenanceFlag has not been set at all
|
|
|
119 |
( defined( $configuration->{'target'}->{'maintenanceFlag'} ) && $configuration->{'target'}->{'maintenanceFlag'} ) ||
|
|
|
120 |
( defined( $configuration->{'source'}->{'maintenanceFlag'} ) && $configuration->{'source'}->{'maintenanceFlag'} );
|
|
|
121 |
# see if maintenance is set on remote. If so, simply return the message
|
|
|
122 |
if ( $configuration->{'source'}->{'up'} ) {
|
|
|
123 |
my ($error, $output) = &runCommand( "ssh $configuration->{remoteMachine}->{ip} 'ls $configuration->{remoteMachine}->{maintenanceFlag}'" );
|
|
|
124 |
if ( ! $error ) {
|
|
|
125 |
# remove the file from the remote server
|
|
|
126 |
&runCommand( "ssh $configuration->{remoteMachine}->{ip} 'rm $configuration->{remoteMachine}->{maintenanceFlag}'" );
|
|
|
127 |
# create a valid return, which will exit the program
|
|
|
128 |
return "Maintenance Flag found on remote machine";
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
# not on remote machine, so give them waitTime seconds to put it here
|
|
|
132 |
# we'll loop, checking every $sleepTime seconds until our wait time
|
|
|
133 |
# ($configuration->{'target'}->{'waitTime'}) has expired
|
|
|
134 |
my $sleepTime = 60;
|
|
|
135 |
for ( my $i = $configuration->{'target'}->{'waitTime'}; $i > 0; $i -= $sleepTime ) {
|
|
|
136 |
sleep $sleepTime;
|
|
|
137 |
# then look for the maintenance flag file on the local machine
|
|
|
138 |
return "Maintenance Flag found on local machine" if -f $configuration->{'target'}->{'maintenanceFlag'};
|
|
|
139 |
}
|
|
|
140 |
# no maintenance flags found, so return false
|
|
|
141 |
return 0;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
sub shutdownMachine {
|
|
|
145 |
my $configuration = shift;
|
|
|
146 |
my $subject = shift;
|
|
|
147 |
push @_, "Shutting down" if $configuration->{'shutdown'};
|
|
|
148 |
&sendMail( join( "\n", @_), $configuration, $subject );
|
|
|
149 |
# do not actually shut down the server unless we are told to
|
|
|
150 |
exit unless $configuration->{'shutdown'};
|
| 7 |
rodolico |
151 |
die "Shutting down machine now\n";
|
| 2 |
rodolico |
152 |
&runCommand( "poweroff" ) unless $configuration->{'testing'};
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
# returns the current time as a string
|
|
|
156 |
sub currentTime {
|
|
|
157 |
my $format = shift;
|
|
|
158 |
# default to YY-MM-DD HH-MM-SS
|
|
|
159 |
$format = '%Y-%m-%d %H-%M-%S' unless $format;
|
|
|
160 |
use POSIX;
|
|
|
161 |
return POSIX::strftime( $format, localtime() );
|
|
|
162 |
}
|
|
|
163 |
|
| 7 |
rodolico |
164 |
# verify a remote machine is up and running
|
| 2 |
rodolico |
165 |
sub checkRemoteUp {
|
|
|
166 |
my $configuration = shift;
|
|
|
167 |
my $ip;
|
|
|
168 |
if ( defined( $configuration->{'target'}->{'server'} ) && $configuration->{'target'}->{'server'} ) {
|
|
|
169 |
$ip = $configuration->{'target'}->{'server'};
|
|
|
170 |
} else {
|
|
|
171 |
$ip = $configuration->{'source'}->{'server'};
|
|
|
172 |
}
|
|
|
173 |
my ($error, $message ) = $ip ? &runCommand( "ping -c 1 -t 5 $ip" ) : (0,'No address defined for either target or server' );
|
| 7 |
rodolico |
174 |
# $message = "Checking IP $ip\n" . $message;
|
| 2 |
rodolico |
175 |
#die "error is $error, message is $message for $ip\n";
|
|
|
176 |
return ($error, $message);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
my @status;
|
|
|
180 |
my $error = 0;
|
|
|
181 |
my $output = '';
|
|
|
182 |
|
|
|
183 |
$configuration = &readConfig($configFileName);
|
|
|
184 |
|
|
|
185 |
# die Dumper( $configuration ) . "\n";
|
|
|
186 |
|
|
|
187 |
my $servername = `hostname`;
|
|
|
188 |
chomp $servername;
|
|
|
189 |
|
| 7 |
rodolico |
190 |
push @status, "Replication on $servername has been started at " . ¤tTime();
|
| 2 |
rodolico |
191 |
&sendMail( "Replication on $servername has been started, " . ¤tTime(), $configuration, "Replication on $servername started" );
|
|
|
192 |
|
|
|
193 |
# see if remote machine is up by sending one ping. Expect response in 5 seconds
|
|
|
194 |
( $error,$output) = &checkRemoteUp( $configuration );
|
|
|
195 |
$configuration->{'up'} = ! $error;
|
|
|
196 |
push @status, "remote machine is " . ( $configuration->{'up'} ? 'Up' : 'Down' ) . "\n";
|
|
|
197 |
# we can not connect to the remote server, so just shut down
|
|
|
198 |
&shutdownMachine( $configuration, "No connection to remote machine", @status ) unless $configuration->{'up'};
|
|
|
199 |
|
|
|
200 |
# check for maintenance flags, exit if we should go into mainteance mode
|
|
|
201 |
if ( my $result = &checkMaintenance( $configuration ) ) {
|
|
|
202 |
push @status,$result;
|
|
|
203 |
&sendMail( join( "\n", @status), $configuration, "Maintenance Mode" );
|
|
|
204 |
die;
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
# try to mount the datasets if they are encrypted
|
|
|
208 |
($error,$output) = &mountDrives( $configuration );
|
|
|
209 |
if ( $error ) { # could not mount datasets
|
|
|
210 |
push @status, $error;
|
|
|
211 |
&shutdownMachine( $configuration, "Mount Drive Error: [$output]", @status );
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
&sendMail( "Backup has been started at " . ¤tTime(), $configuration, "Backup Starting" );
|
| 7 |
rodolico |
215 |
push @status, ¤tTime() . ' Backup started';
|
| 2 |
rodolico |
216 |
|
|
|
217 |
$configuration->{'source'}->{'server'} = $configuration->{'source'}->{'server'} ? $configuration->{'source'}->{'server'} . ':' : '';
|
|
|
218 |
$configuration->{'target'}->{'server'} = $configuration->{'target'}->{'server'} ? $configuration->{'target'}->{'server'} . ':' : '';
|
|
|
219 |
|
| 7 |
rodolico |
220 |
my @flags;
|
|
|
221 |
push @flags, '--dryrun' if $configuration->{'dryrun'};
|
|
|
222 |
push @flags, '--recurse' if $configuration->{'recurse'};
|
|
|
223 |
push @flags, '--verbose' if $configuration->{'verbose'};
|
|
|
224 |
push @flags, '--verbose' if $configuration->{'verbose'} > 1;
|
|
|
225 |
push @flags, "--bwlimit=$configuration->{bandwidth}" if $configuration->{'bandwidt'};
|
|
|
226 |
push @flags, "--filter='$configuration->{filter}'" if $configuration->{'filter'};
|
|
|
227 |
|
| 2 |
rodolico |
228 |
# For each dataset, let's find the snapshots we need
|
|
|
229 |
foreach my $sourceDir ( keys %{$configuration->{'source'}->{'dataset'}} ) {
|
| 7 |
rodolico |
230 |
print "Looking for $sourceDir\n" if $configuration->{'testing'} > 2;
|
|
|
231 |
print "syncing to $configuration->{target}->{dataset}\n" if $configuration->{'testing'} > 2;
|
|
|
232 |
my $command = $replicateScript . ' ' . join( ' ', @flags ) . ' ' .
|
|
|
233 |
'--source=' .
|
| 2 |
rodolico |
234 |
$configuration->{'source'}->{'server'} .
|
|
|
235 |
$configuration->{'source'}->{'dataset'}->{$sourceDir} . '/' . $sourceDir . ' ' .
|
| 7 |
rodolico |
236 |
'--target=' .
|
| 2 |
rodolico |
237 |
$configuration->{'target'}->{'server'} .
|
| 7 |
rodolico |
238 |
$configuration->{'target'}->{'dataset'} . '/' . $sourceDir;
|
|
|
239 |
push @status, ¤tTime() . " Running $command";
|
|
|
240 |
if ( ! $configuration->{'testing'} ) {
|
|
|
241 |
($error, $output) = &runCommand( $command );
|
|
|
242 |
push @status, $output;
|
|
|
243 |
}
|
|
|
244 |
push @status, ¤tTime() . " Completed command, with status $error";
|
| 2 |
rodolico |
245 |
}
|
|
|
246 |
|
| 7 |
rodolico |
247 |
push @status, ¤tTime() . ' Backup finished';
|
| 2 |
rodolico |
248 |
|
|
|
249 |
if ($configuration->{'testing'}) {
|
|
|
250 |
print join( "\n", @status ) . "\n";
|
|
|
251 |
} else {
|
|
|
252 |
&shutdownMachine( $configuration, "Backup Complete", @status );
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
1;
|