| Line 6... |
Line 6... |
| 6 |
use FindBin;
|
6 |
use FindBin;
|
| 7 |
use lib "$FindBin::Bin/..";
|
7 |
use lib "$FindBin::Bin/..";
|
| 8 |
use Data::Dumper;
|
8 |
use Data::Dumper;
|
| 9 |
use ZFS_Utils qw(loadConfig shredFile logMsg makeReplicateCommands mountDriveByLabel mountGeli runCmd $logFileName $displayLogsOnConsole);
|
9 |
use ZFS_Utils qw(loadConfig shredFile logMsg makeReplicateCommands mountDriveByLabel mountGeli runCmd $logFileName $displayLogsOnConsole);
|
| 10 |
|
10 |
|
| - |
|
11 |
# if set, will not actually write files to disk
|
| - |
|
12 |
my $DEBUG = 0;
|
| - |
|
13 |
|
| 11 |
# set the log file to be next to this script
|
14 |
# set the log file to be next to this script
|
| 12 |
$logFileName = "$FindBin::Bin/sneakernet.log";
|
15 |
$logFileName = "$FindBin::Bin/sneakernet.log";
|
| 13 |
# log only for one run
|
16 |
# log only for one run
|
| 14 |
unlink ( $logFileName ) if -f $logFileName;
|
17 |
unlink ( $logFileName ) if -f $logFileName;
|
| 15 |
|
18 |
|
| Line 128... |
Line 131... |
| 128 |
my ($config, $statusList) = @_;
|
131 |
my ($config, $statusList) = @_;
|
| 129 |
my $newStatus = [];
|
132 |
my $newStatus = [];
|
| 130 |
foreach my $dataset ( sort keys %{$config->{datasets}} ) {
|
133 |
foreach my $dataset ( sort keys %{$config->{datasets}} ) {
|
| 131 |
logMsg("Processing dataset '$dataset'");
|
134 |
logMsg("Processing dataset '$dataset'");
|
| 132 |
# get list of all snapshots on dataset
|
135 |
# get list of all snapshots on dataset
|
| 133 |
my $sourceList = [ runCmd( "zfs", "list", "-rt", "snap", "-H", "-o", "name", $config->{datasets}->{$dataset}->{source} ) ];
|
136 |
my $sourceList = [ runCmd( "zfs list -rt snap -H -o name $config->{datasets}->{$dataset}->{source} " ) ];
|
| 134 |
# process dataset here
|
137 |
# process dataset here
|
| 135 |
my $commands = makeReplicateCommands($sourceList, $statusList, $newStatus );
|
138 |
my $commands = makeReplicateCommands($sourceList, $statusList, $newStatus );
|
| 136 |
if ( %$commands ) {
|
139 |
if ( %$commands ) {
|
| 137 |
foreach my $cmd ( keys %$commands ) {
|
140 |
foreach my $cmd ( keys %$commands ) {
|
| 138 |
my $command = $commands->{$cmd};
|
141 |
my $command = $commands->{$cmd};
|
| 139 |
$command .= " | openssl enc -aes-256-cbc -K $config->{transport}->{encryption}->{key} -iv $config->{transport}->{encryption}->{IV} " if $config->{transport}->{encryption}->{key};
|
142 |
$command .= " | openssl enc -aes-256-cbc -K $config->{transport}->{encryption}->{key} -iv $config->{transport}->{encryption}->{IV} " if $config->{transport}->{encryption}->{key};
|
| 140 |
$command .= " > $config->{transport}->{mount_point}/" . replaceSlashWithDot($cmd);
|
143 |
$command .= " > $config->{transport}->{mount_point}/" . replaceSlashWithDot($cmd);
|
| 141 |
logMsg("Running command: $command");
|
144 |
logMsg("Running command: $command");
|
| 142 |
#runCmd( split( /\s+/, $cmd ) );
|
145 |
runCmd( $command ) unless $DEBUG;
|
| 143 |
}
|
146 |
}
|
| 144 |
} else {
|
147 |
} else {
|
| 145 |
logMsg( "Nothing to do for $dataset" );
|
148 |
logMsg( "Nothing to do for $dataset" );
|
| 146 |
}
|
149 |
}
|
| 147 |
}
|
150 |
}
|
| 148 |
return $newStatus;
|
151 |
return $newStatus;
|
| 149 |
}
|
152 |
}
|
| 150 |
|
153 |
|
| - |
|
154 |
# clean all files from a directory, but not any subdirectories
|
| - |
|
155 |
sub cleanDirectory {
|
| - |
|
156 |
my $dirname = shift;
|
| - |
|
157 |
logMsg( "Cleaning up $dirname of all files" );
|
| - |
|
158 |
# clean up a directory
|
| - |
|
159 |
opendir( my $dh, $dirname ) || fatalError( "Can not open $dirname: #!" );
|
| - |
|
160 |
# get all file names, but leave directories alone
|
| - |
|
161 |
my @files = map{ $dirname . "/$_" } grep { -f "$dirname/$_" } readdir($dh);
|
| - |
|
162 |
closedir $dh;
|
| - |
|
163 |
foreach my $file (@files) {
|
| - |
|
164 |
unlink $file or warn "Could not unlink $file: #!\n";
|
| - |
|
165 |
}
|
| - |
|
166 |
return;
|
| - |
|
167 |
}
|
| - |
|
168 |
|
| - |
|
169 |
|
| - |
|
170 |
|
| 151 |
# how to handle a fatal error
|
171 |
# how to handle a fatal error
|
| 152 |
sub fatalError {
|
172 |
sub fatalError {
|
| 153 |
my $message = shift;
|
173 |
my $message = shift;
|
| 154 |
logMsg( $message );
|
174 |
logMsg( $message );
|
| 155 |
die;
|
175 |
die;
|
| Line 176... |
Line 196... |
| 176 |
unless $config->{transport}->{mount_point} = mountDriveByLabel( $config->{transport}->{disk_label}, $config->{transport}->{mount_point}, $config->{transport}->{timeout} );
|
196 |
unless $config->{transport}->{mount_point} = mountDriveByLabel( $config->{transport}->{disk_label}, $config->{transport}->{mount_point}, $config->{transport}->{timeout} );
|
| 177 |
|
197 |
|
| 178 |
my $servername = `hostname -s`;
|
198 |
my $servername = `hostname -s`;
|
| 179 |
chomp $servername;
|
199 |
chomp $servername;
|
| 180 |
if ( $servername eq $config->{source_server}->{hostname} ) {
|
200 |
if ( $servername eq $config->{source_server}->{hostname} ) {
|
| 181 |
logMsg "Running as source server\n";
|
201 |
logMsg "Running as source server";
|
| - |
|
202 |
# remove all files from transport disk, but leave all subdirectories alone
|
| - |
|
203 |
cleanDirectory( $config->{transport}->{mount_point} );
|
| 182 |
my $statusList = getStatusFile($config->{status_file});
|
204 |
my $statusList = getStatusFile($config->{status_file});
|
| 183 |
$statusList = doSourceReplication($config, $statusList);
|
205 |
$statusList = doSourceReplication($config, $statusList);
|
| 184 |
writeStatusFile($config->{status_file}, $statusList);
|
206 |
writeStatusFile($config->{status_file}, $statusList);
|
| 185 |
# source server logic here
|
207 |
# source server logic here
|
| 186 |
} elsif ( $servername eq $config->{target_server}->{hostname} ) {
|
208 |
} elsif ( $servername eq $config->{target_server}->{hostname} ) {
|
| 187 |
logMsg "Running as target server\n";
|
209 |
logMsg "Running as target server";
|
| - |
|
210 |
die "Target Server code not complete\n";
|
| 188 |
die "GELI target server logic not yet implemented\n" if ( defined $config->{target_server}->{geli} );
|
211 |
die "GELI target server logic not yet implemented\n" if ( defined $config->{target_server}->{geli} );
|
| 189 |
mountGeli( $config->{target_server}->{geli} ) if ( defined $config->{target_server}->{geli} );
|
212 |
mountGeli( $config->{target_server}->{geli} ) if ( defined $config->{target_server}->{geli} );
|
| 190 |
} else {
|
213 |
} else {
|
| 191 |
logMsg "This server ($servername) is neither source nor target server as per config\n";
|
214 |
logMsg "This server ($servername) is neither source nor target server as per config\n";
|
| 192 |
die;
|
215 |
die;
|
| 193 |
}
|
216 |
}
|
| 194 |
|
217 |
|
| 195 |
# unmount the sneakernet drive
|
218 |
# unmount the sneakernet drive
|
| 196 |
`umount $config->{transport}->{mount_point}`;
|
219 |
`umount $config->{transport}->{mount_point}`;
|
| 197 |
# and remove the directory
|
220 |
# and remove the directory
|
| 198 |
unlink $config->{transport}->{mount_point};
|
221 |
rmdir $config->{transport}->{mount_point};
|
| 199 |
|
- |
|
| 200 |
die "Source and target server logic not yet implemented\n";
|
- |
|
| 201 |
|
- |
|
| 202 |
#my $newStatus = [];
|
- |
|
| 203 |
#foreach my $dataset ( sort keys %{$config->{datasets}} ) {
|
- |
|
| 204 |
# logMsg("Processing dataset '$dataset'");
|
- |
|
| 205 |
# my $sourceList = [ runCmd( "zfs", "list", "-rt", "snap", "-H", "-o", "name", $config->{datasets}->{$dataset}->{source} ) ];
|
- |
|
| 206 |
# # process dataset here
|
- |
|
| 207 |
# my $commands = makeReplicateCommands($sourceList, $targetList, $newStatus );
|
- |
|
| 208 |
# foreach my $cmd ( @$commands ) {
|
- |
|
| 209 |
# $cmd .= " > $config->{transport}->{mount_point}/" . $dataset;
|
- |
|
| 210 |
# logMsg("Running command: $cmd");
|
- |
|
| 211 |
# #runCmd( split( /\s+/, $cmd ) );
|
- |
|
| 212 |
# }
|
- |
|
| 213 |
#}
|
- |
|
| 214 |
|
- |
|
| 215 |
|
- |
|
| 216 |
|
222 |
|
| 217 |
1;
|
223 |
1;
|
| 218 |
|
224 |
|
| 219 |
|
225 |
|
| 220 |
#`cat $config->{input} | openssl enc -aes-256-cbc -K $config->{key} -iv $config->{IV} > $config->{output}`;
|
226 |
#`cat $config->{input} | openssl enc -aes-256-cbc -K $config->{key} -iv $config->{IV} > $config->{output}`;
|