Subversion Repositories zfs_utils

Rev

Rev 18 | Rev 21 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
19
my $cwd = $FindBin::RealBin;
20
my $configFileName = $cwd . '/sync.yaml';
21
my $replicateScript = $cwd . '/replicate';
22
 
23
my $configuration;
24
 
25
 
26
# load Configuration File
27
# read the config file and return it
28
sub readConfig {
29
   my $filename = shift;
30
   die "Config file $filename not found: $!" unless -f $filename;
31
   my $yaml = YAML::Tiny->new( {} );
32
   if ( -f $filename ) {
33
      $yaml = YAML::Tiny->read( $filename );
34
   }
35
   return $yaml->[0];
36
}
37
 
10 rodolico 38
sub logit {
39
   open LOG, ">>/tmp/replicate.log" or die "Could not open replicate.log: $!\n";
40
   print LOG join( "\n", @_ ) . "\n";
41
   close LOG;
42
}
2 rodolico 43
 
44
 
45
# this calls gshred which will overwrite the file 3 times, then
46
# remove it.
47
# NOTE: this will not work on ZFS, since ZFS is CopyOnWrite (COW)
20 rodolico 48
# so assuming file is on a ramdisk
2 rodolico 49
sub shredFile {
50
   my $filename = shift;
51
   `/usr/local/bin/gshred -u -f -s 32 $filename`;
52
}
53
 
54
 
55
# runs a command, redirecting stderr to stdout (which it ignores)
56
# then returns 0 on success.
57
# if error, returns string describing error
58
sub runCommand {
59
   my $command = shift;
10 rodolico 60
   #logit( $command );
2 rodolico 61
   my $output = qx/$command 2>&1/;
62
   if ($? == -1) {
63
      return (-1, "failed to execute: $!" );
64
   } elsif ($? & 127) {
65
      return (-1,sprintf "child died with signal %d, %s coredump",
66
        ($? & 127),  ($? & 128) ? 'with' : 'without');
67
   } else {
68
      return ( $?>>8, sprintf "child exited with value %d", $? >> 8 ) if $? >> 8;
69
   }
70
   return (0, $output);
71
}
20 rodolico 72
 
73
# Checks if a zpool is available. If not, retrieves the geli key from a remote server,
74
# decrypts the drives, and mounts the zpool.
75
# Params:
76
#   $zpool         - Name of the zpool
77
#   $local_key     - Local path to store the geli key
78
#   $remote_server - Remote server to fetch the key from (user@host)
79
#   $remote_key    - Path to the geli key on the remote server
80
#   $drives_ref    - Arrayref of drives to decrypt (e.g., ['/dev/ada0p3', '/dev/ada1p3'])
81
sub mountGeliZpool {
82
    my ($zpool, $local_key, $remote_server, $remote_key, $drives_ref) = @_;
83
 
84
    # Check if the zpool is available
85
    my ($error, $output) = runCommand("zpool list $zpool");
86
    return 0 unless $error; # zpool is available
87
 
88
    # Retrieve geli key from remote server
89
    ($error, $output) = runCommand("scp $remote_server:$remote_key $local_key");
90
    return ($error, "Failed to retrieve geli key from $remote_server:$remote_key" ) if $error;
91
 
92
    # Attach geli key to each drive
93
    foreach my $drive (@$drives_ref) {
94
        ($error, $output) = runCommand("geli attach -k $local_key $drive");
95
         return ($error, "Failed to attach geli key to $drive: $output" ) if $error;
96
    }
97
 
98
    # Import the zpool
99
    ($error, $output) = runCommand("zpool import $zpool");
100
    return ( $error, "Failed to import zpool $zpool: $output" ) if $error;
101
 
102
    # Optionally, mount all datasets in the zpool
103
    ($error, $output) = runCommand("zfs mount -a");
104
     return( $error,"Failed to mount datasets in zpool $zpool: $output" ) if $error;
105
 
106
    # Shred the key file after use
107
    shredFile($local_key) if -f $local_key;
108
 
109
    return 1;
2 rodolico 110
}
111
 
10 rodolico 112
# a very simple mailer, just send information to sendmail
2 rodolico 113
sub sendMail {
114
   my ($message, $configuration, $subject ) = @_;
10 rodolico 115
   if ( $message ) {
116
      open MAIL,"|sendmail -t" or die "Could not open sendmail: $!\n";
117
      print MAIL "To: $configuration->{email}->{notify}\n";
118
      print MAIL "From: $configuration->{email}->{from}\n";
119
      print MAIL "Subject: " . 
120
                 ($configuration->{'email'}->{'subject'} . ( $subject ? " - $subject" : '' ) ) .
121
                 "\n\n";
122
      print MAIL $message;
123
      close MAIL;
2 rodolico 124
   } else {
10 rodolico 125
      warn "no message in outgoing email\n";
2 rodolico 126
   }
127
}
128
 
129
# checks to see if we should be in maintenance mode
130
# if $remoteMachine->{'maintenanceMode'} exists, set mode
131
# otherwise, wait localMachine->{'waittime'} minutes, then check
132
# $localMachine->{'maintenanceMode'}.
133
# if neither exists, begin sync
134
sub checkMaintenance {
135
   my $configuration = shift;
136
   return 0 unless # exit if maintenanceFlag has not been set at all
137
     ( defined( $configuration->{'target'}->{'maintenanceFlag'} ) && $configuration->{'target'}->{'maintenanceFlag'} ) ||
138
     ( defined( $configuration->{'source'}->{'maintenanceFlag'} ) && $configuration->{'source'}->{'maintenanceFlag'} );
139
   # see if maintenance is set on remote. If so, simply return the message
140
   if ( $configuration->{'source'}->{'up'} ) {
141
      my ($error, $output) = &runCommand( "ssh $configuration->{remoteMachine}->{ip} 'ls $configuration->{remoteMachine}->{maintenanceFlag}'" );
142
      if ( ! $error ) {
143
         # remove the file from the remote server
144
         &runCommand( "ssh $configuration->{remoteMachine}->{ip} 'rm $configuration->{remoteMachine}->{maintenanceFlag}'" );
145
         # create a valid return, which will exit the program
146
         return "Maintenance Flag found on remote machine";
147
      }
148
   }
149
   # not on remote machine, so give them waitTime seconds to put it here
150
   # we'll loop, checking every $sleepTime seconds until our wait time
151
   # ($configuration->{'target'}->{'waitTime'}) has expired
152
   my $sleepTime = 60;
153
   for ( my $i = $configuration->{'target'}->{'waitTime'}; $i > 0; $i -= $sleepTime ) {
154
      sleep $sleepTime;
155
      # then look for the maintenance flag file on the local machine
156
      return "Maintenance Flag found on local machine" if -f $configuration->{'target'}->{'maintenanceFlag'};
157
   }
158
   # no maintenance flags found, so return false
159
   return 0;
160
}
161
 
162
sub shutdownMachine {
163
   my $configuration = shift;
10 rodolico 164
   exit unless $configuration->{'shutdown'};
2 rodolico 165
   # do not actually shut down the server unless we are told to
166
   &runCommand( "poweroff" ) unless $configuration->{'testing'};
167
}
168
 
169
# returns the current time as a string
170
sub currentTime {
171
   my $format = shift;
172
   # default to YY-MM-DD HH-MM-SS
16 rodolico 173
   $format = '%Y-%m-%d %H:%M:%S' unless $format;
2 rodolico 174
   use POSIX;
175
   return POSIX::strftime( $format, localtime() );
176
}
177
 
7 rodolico 178
# verify a remote machine is up and running
2 rodolico 179
sub checkRemoteUp {
180
   my $configuration = shift;
181
   my $ip;
182
   if ( defined( $configuration->{'target'}->{'server'} ) && $configuration->{'target'}->{'server'} ) {
183
      $ip = $configuration->{'target'}->{'server'};
184
   } else {
185
      $ip = $configuration->{'source'}->{'server'};
186
   }
187
   my ($error, $message ) =  $ip ? &runCommand( "ping -c 1 -t 5 $ip" ) : (0,'No address defined for either target or server' );
7 rodolico 188
#   $message = "Checking IP $ip\n"  . $message;
2 rodolico 189
   #die "error is $error, message is $message for $ip\n";
190
   return ($error, $message);
191
}
192
 
12 rodolico 193
sub updateStats {
194
   my ( $label, $filename, $output ) = @_;
195
   if ( $output =~ m/bytes\t(\d+).*seconds\t(\d+)/gms ) { # global, multiline, . matches newlines
196
      my $seconds = $2;
197
      my $bytes = $1;
198
      open STATS,">>$filename" or warn "Could not create file $filename: $!\n";
16 rodolico 199
      print STATS &currentTime('') . "\t$label\t$seconds\t$bytes\n";
12 rodolico 200
      close STATS
201
   } else {
18 rodolico 202
      warn "updateStats called with invalid report\n" if $configuration->{'verbose'}>1;
12 rodolico 203
   }
204
}
205
 
2 rodolico 206
my @status;   
207
my $error = 0;
208
my $output = '';
209
 
210
$configuration = &readConfig($configFileName);
211
 
212
# die Dumper( $configuration ) . "\n";
213
 
214
my $servername = `hostname`;
215
chomp $servername;
216
 
18 rodolico 217
if ( $configuration->{'verbose'} > 1 ) {
218
   push @status, "Replication on $servername has been started at " . &currentTime();
219
   &sendMail( "Replication on $servername has been started, " . &currentTime(), $configuration, "Replication on $servername started" );
220
}
2 rodolico 221
 
222
# see if remote machine is up by sending one ping. Expect response in 5 seconds
223
( $error,$output) = &checkRemoteUp( $configuration );
224
$configuration->{'up'} = ! $error;
225
push @status, "remote machine is " . ( $configuration->{'up'} ? 'Up' : 'Down' ) . "\n";
10 rodolico 226
if ( ! $configuration->{'up'} ) {
227
   # we can not connect to the remote server, so just shut down
228
   sendMail( join( "\n", @status ), $configuration, "No connection to remote machine" );
229
   &shutdownMachine( $configuration );
230
}
2 rodolico 231
 
232
# check for maintenance flags, exit if we should go into mainteance mode
233
if ( my $result = &checkMaintenance( $configuration ) ) {
234
   push @status,$result;
235
   &sendMail( join( "\n", @status), $configuration, "Maintenance Mode" );
20 rodolico 236
   exit 1;
2 rodolico 237
}
238
 
20 rodolico 239
# if the zpool is encrypted with geli, make sure it is available
240
($error, $output) = &mountGeliZpool {(
241
   $configuration->{'geli'}->{'zpool'},
242
   $configuration->{'geli'}->{'localKey'},
243
   $configuration->{'geli'}->{'server'},
244
   $configuration->{'geli'}->{'remoteKey'},
245
   split( ' ', $configuration->{'geli'}->{'drives'} ) )
246
   if exists ( $configuration->{'geli'} );
247
 
248
if ( $error) { # could not mount datasets
10 rodolico 249
   push @status, $output;
250
   &sendMail( join( "\n", @status ), $configuration, "Mount Drive Error: [$output]" );
251
   &shutdownMachine( $configuration );
2 rodolico 252
}
253
 
10 rodolico 254
#&sendMail( "Backup has been started at " . &currentTime(), $configuration, "Backup Starting" );
18 rodolico 255
push @status, &currentTime() . ' Backup started' if $configuration->{'verbose'};
2 rodolico 256
 
257
$configuration->{'source'}->{'server'} = $configuration->{'source'}->{'server'} ? $configuration->{'source'}->{'server'} . ':' : '';
258
$configuration->{'target'}->{'server'} = $configuration->{'target'}->{'server'} ? $configuration->{'target'}->{'server'} . ':' : '';
259
 
7 rodolico 260
my @flags;
261
push @flags, '--dryrun' if $configuration->{'dryrun'};
262
push @flags, '--recurse' if $configuration->{'recurse'};
16 rodolico 263
push @flags, '-' . 'v'x$configuration->{verbose} if $configuration->{'verbose'};
10 rodolico 264
push @flags, "--bwlimit=$configuration->{bandwidth}" if $configuration->{'bandwidth'};
7 rodolico 265
push @flags, "--filter='$configuration->{filter}'" if $configuration->{'filter'};
266
 
16 rodolico 267
# die join( ' ', @flags ) . "\n";
268
 
12 rodolico 269
# prepend the current working directory to stats if it does not have a path
270
$configuration->{'stats'} = $cwd . "/" . $configuration->{'stats'}
271
   if $configuration->{'stats'} && $configuration->{'stats'} !~ m/\//;
272
 
2 rodolico 273
# For each dataset, let's find the snapshots we need
274
foreach my $sourceDir ( keys %{$configuration->{'source'}->{'dataset'}} ) {
10 rodolico 275
   #print "Working on $sourceDir\n";
7 rodolico 276
   print "Looking for $sourceDir\n" if $configuration->{'testing'} > 2;
277
   print "syncing to $configuration->{target}->{dataset}\n" if $configuration->{'testing'} > 2;
278
   my $command = $replicateScript . ' ' . join( ' ', @flags ) . ' ' .
279
                 '--source=' .
2 rodolico 280
                 $configuration->{'source'}->{'server'} . 
281
                 $configuration->{'source'}->{'dataset'}->{$sourceDir} . '/' . $sourceDir . ' ' .
7 rodolico 282
                 '--target=' .
2 rodolico 283
                 $configuration->{'target'}->{'server'} . 
7 rodolico 284
                 $configuration->{'target'}->{'dataset'} . '/' . $sourceDir;
10 rodolico 285
   #print "Command is $command\n";
18 rodolico 286
   push @status, &currentTime() . " Running $command" if $configuration->{'verbose'} > 1;
7 rodolico 287
   if ( ! $configuration->{'testing'} ) {
288
      ($error, $output) = &runCommand( $command );
16 rodolico 289
      push @status, "Dataset\t$sourceDir\n$output";
12 rodolico 290
      # update stats file if they have requested it
291
      &updateStats( $sourceDir, $configuration->{'stats'}, $output ) if $configuration->{'stats'};
7 rodolico 292
   }
18 rodolico 293
   push @status, &currentTime() . " Completed command, with status $error" if $configuration->{'verbose'} > 1;;
2 rodolico 294
}
295
 
10 rodolico 296
#print "Finished processing\n";
297
#print "testing is " . $configuration->{'testing'} . "\n";
298
 
7 rodolico 299
push @status, &currentTime() . ' Backup finished';
2 rodolico 300
 
301
if ($configuration->{'testing'}) {
302
   print join( "\n", @status ) . "\n";
303
} else {
10 rodolico 304
   #print "Sending final email\n";
305
   &sendMail( join( "\n", @status ), $configuration, "Backup Complete" );
306
   #print "Running shutdown\n";
307
   &shutdownMachine( $configuration ) if $configuration->{'shutdown'};
2 rodolico 308
}
309
 
310
1;