Subversion Repositories zfs_utils

Rev

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

Rev 27 Rev 30
Line 66... Line 66...
66
         'filename' => 'files_share'
66
         'filename' => 'files_share'
67
      },
67
      },
68
   }
68
   }
69
};
69
};
70
 
70
 
-
 
71
# read the status file and return as list
-
 
72
sub getStatusFile {
-
 
73
   my $filename = shift;
-
 
74
   # read in history/status file
-
 
75
   my @lines;
-
 
76
   if ( -e $filename && open my $fh, '<', $filename ) {
-
 
77
      chomp( @lines = <$fh> );
-
 
78
      close $fh;
-
 
79
      logMsg("Read status file '$filename' with contents:\n" . join( "\n", @lines ) . "\n");
-
 
80
   } else {
-
 
81
      logMsg("Error: could not read status file '$filename': $!");
-
 
82
      die;
-
 
83
   }
-
 
84
   return \@lines;
-
 
85
}
-
 
86
 
-
 
87
# write the status list to file
-
 
88
sub writeStatusFile {
-
 
89
   my ( $filename, $statusList ) = @_;
-
 
90
   # backup existing status file
-
 
91
   if ( -e $filename ) {
-
 
92
      rename( $filename, "$filename.bak" ) or do {
-
 
93
         logMsg("Error: could not backup existing status file '$filename': $!");
-
 
94
         die;
-
 
95
      };
-
 
96
   }
-
 
97
   # write new status file
-
 
98
   if ( open my $fh, '>', $filename ) {
-
 
99
      foreach my $line ( @$statusList ) {
-
 
100
         print $fh "$line\n";
-
 
101
      }
-
 
102
      close $fh;
-
 
103
      logMsg("Wrote status file '$filename' with contents:\n" . join( "\n", @$statusList ) . "\n");
-
 
104
   } else {
-
 
105
      logMsg("Error: could not write status file '$filename': $!");
-
 
106
      die;
-
 
107
   }
-
 
108
}
-
 
109
 
-
 
110
# perform replication on source server
-
 
111
# $config - configuration hashref
-
 
112
# $statusList - list of last snapshots replicated for each dataset in previous replications
-
 
113
# return new status list after replication containing updated last snapshots
-
 
114
# this script will actually replicate the datasets to the sneakernet disk
-
 
115
sub doSourceReplication {
-
 
116
   my ($config, $statusList) = @_;
-
 
117
   my $newStatus = [];
-
 
118
   foreach my $dataset ( sort keys %{$config->{datasets}} ) {
-
 
119
      logMsg("Processing dataset '$dataset'\n");
-
 
120
      my $sourceList = [ runCmd( "zfs", "list", "-rt", "snap", "-H", "-o", "name", $config->{datasets}->{$dataset}->{source} ) ];
-
 
121
      
-
 
122
      # process dataset here
-
 
123
      my $commands = makeReplicateCommands($sourceList, $statusList, $newStatus );
-
 
124
      foreach my $cmd ( @$commands ) {
-
 
125
         logMsg("Running command: $cmd\n");
-
 
126
         #runCmd( split( /\s+/, $cmd ) );
-
 
127
      }
-
 
128
   }
-
 
129
   return $newStatus;
-
 
130
}
-
 
131
 
71
 
132
 
-
 
133
##################### main program starts here #####################
-
 
134
# Example to create a random key for encryption/decryption:
72
# generate a random key with
135
# generate a random key with
73
# openssl rand 32 | xxd -p | tr -d '\n' > test.key
136
# openssl rand 32 | xxd -p | tr -d '\n' > test.key
74
 
137
 
75
# If a YAML config file exists next to the script, load and merge it
138
# If a YAML config file exists next to the script, load and merge it
76
$config = loadConfig($configFileName, $config );
139
$config = loadConfig($configFileName, $config );
Line 84... Line 147...
84
 
147
 
85
my $servername = `hostname -s`;
148
my $servername = `hostname -s`;
86
chomp $servername;
149
chomp $servername;
87
if ( $servername eq $config->{source_server}->{hostname} ) {
150
if ( $servername eq $config->{source_server}->{hostname} ) {
88
    logMsg "Running as source server\n";
151
    logMsg "Running as source server\n";
-
 
152
    my $statusList = getStatusFile($config->{status_file});
-
 
153
    $statusList = doSourceReplication($config, $statusList);
-
 
154
    writeStatusFile($config->{status_file}, $statusList);
89
    # source server logic here
155
    # source server logic here
90
} elsif ( $servername eq $config->{target_server}->{hostname} ) {
156
} elsif ( $servername eq $config->{target_server}->{hostname} ) {
91
    logMsg "Running as target server\n";
157
    logMsg "Running as target server\n";
-
 
158
    die "GELI target server logic not yet implemented\n" if ( defined $config->{target_server}->{geli} );
92
    mountGeli( $config->{target_server}->{geli} ) if ( defined $config->{target_server}->{geli} );
159
    mountGeli( $config->{target_server}->{geli} ) if ( defined $config->{target_server}->{geli} );
93
} else {
160
} else {
94
    logMsg "This server ($servername) is neither source nor target server as per config\n";
161
    logMsg "This server ($servername) is neither source nor target server as per config\n";
95
    die;
162
    die;
96
}
163
}
97
 
164
 
98
# read in history/status file
165
die "Source and target server logic not yet implemented\n";
-
 
166
 
99
my $targetList = [];
167
#my $newStatus = [];
100
if ( -e $config->{status_file} && open my $fh, '<', $config->{status_file} ) {
168
#foreach my $dataset ( sort keys %{$config->{datasets}} ) {
101
   chomp( my @lines = <$fh> );
169
#   logMsg("Processing dataset '$dataset'");
-
 
170
#   my $sourceList = [ runCmd( "zfs", "list", "-rt", "snap", "-H", "-o", "name", $config->{datasets}->{$dataset}->{source} ) ];
102
   $targetList = \@lines;
171
#   # process dataset here
-
 
172
#   my $commands = makeReplicateCommands($sourceList, $targetList, $newStatus );
103
   close $fh;
173
#   foreach my $cmd ( @$commands ) {
-
 
174
#      $cmd .= " > $config->{transport}->{mount_point}/" . $dataset;
104
} else {
175
#      logMsg("Running command: $cmd");
105
   logMsg("Error: could not read status file '$config->{status_file}': $!");
176
#      #runCmd( split( /\s+/, $cmd ) );
106
   die;
177
#   }
107
}
178
#}
-
 
179
 
108
 
180
 
109
my $newStatus = [];
-
 
110
foreach my $dataset ( sort keys %{$config->{datasets}} ) {
-
 
111
   logMsg("Processing dataset '$dataset'");
-
 
112
   my $sourceList = [ runCmd( "zfs", "list", "-rt", "snap", "-H", "-o", "name", $config->{datasets}->{$dataset}->{source} ) ];
-
 
113
   # process dataset here
-
 
114
   my $commands = makeReplicateCommands($sourceList, $targetList, $newStatus );
-
 
115
   foreach my $cmd ( @$commands ) {
-
 
116
      $cmd .= " > $config->{transport}->{mount_point}/" . $dataset;
-
 
117
      logMsg("Running command: $cmd");
-
 
118
      #runCmd( split( /\s+/, $cmd ) );
-
 
119
   }
-
 
120
}
-
 
121
 
181
 
122
1;
182
1;
123
 
183
 
124
 
184
 
125
#`cat $config->{input} | openssl enc -aes-256-cbc -K $config->{key} -iv $config->{IV} > $config->{output}`;
185
#`cat $config->{input} | openssl enc -aes-256-cbc -K $config->{key} -iv $config->{IV} > $config->{output}`;