Subversion Repositories zfs_utils

Rev

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

Rev Author Line No. Line
24 rodolico 1
#! /usr/bin/env perl
2
 
3
use strict;
4
use warnings;
5
 
6
use FindBin;
7
use lib "$FindBin::Bin/..";
8
use ZFS_Utils qw(loadConfig shredFile mountDriveByLabel mountGeli $logFileName $displayLogsOnConsole);
9
 
10
# set the log file to be next to this script
11
$logFileName = "$FindBin::Bin/sneakernet.log";
12
# display all log messages on console in addition to the log file
13
$displayLogsOnConsole = 1;
14
 
15
my $configFileName = "$0.conf.yaml";
16
 
17
my $config = {
18
   # file created on source server to track last copyed dataset
19
   'status_file' => "$0.status",
20
   #information about source server
21
   'source_server' => {
22
      'hostname' => '', # used to see if we are on source
23
      'poolname' => '', # name of the ZFS pool to export
24
   },
25
   #information about target server
26
   'target_server' => {
27
      'hostname' => '', # used to see if we are on target
28
      'poolname' => '', # name of the ZFS pool to import
29
      # if this is set, the dataset uses GELI, so we must decrypt and
30
      # mount it first
31
      'geli' => {
32
         'keydiskname' => 'replica', # the GPT label of the key disk
33
         'keyfile' => 'geli.key', # the name of the key file on keydiskname
34
         'localKey' => 'e98c660cccdae1226550484d62caa2b72f60632ae0c607528aba1ac9e7bfbc9c', # hex representation of the local key part
35
         'target' => '/media/geli.key', # location to create the combined keyfile
36
         'poolname' => '', # name of the ZFS pool to import
37
         'diskList' => [ 
38
            '/dev/gpt/sneakernet_disk' 
39
            ], # list of disks to try to mount the dataset from
40
      }
41
   },
42
   'transport' => {
43
      # this is the GPT label of the sneakernet disk
44
      'disk_label' => 'sneakernet',
45
      # where we want to mount it
46
      'mount_point' => '/mnt/sneakernet',
47
      # amount of time to wait for the disk to appear
48
      'timeout' => 600,
49
      # if set, all files will be encrypted with this key/IV during transport
50
      'encryption' => {
51
         'key'    => '', # openssl rand 32 | xxd -p | tr -d '\n' > test.key
52
         'IV'     => '00000000000000000000000000000000',
53
      },
54
   },
55
   'datasets' => {
56
      'iscsi' => {
57
         'source' => 'storage/backup/iscsi',
58
         'target' => 'storage/backup/iscsi',
59
         'filename' => 'iscsi'
60
      },
61
      'nextcloud'  => {
62
         'source' => 'storage/backup/nextcloud',
63
         'target' => 'storage/backup/nextcloud',
64
         'filename' => 'nextcloud'
65
      },
66
      'files_share'  => {
67
         'source' => 'storage/backup/files_share',
68
         'target' => 'storage/backup/files_share',
69
         'filename' => 'files_share'
70
      },
71
   }
72
};
73
 
74
 
75
# generate a random key with
76
# openssl rand 32 | xxd -p | tr -d '\n' > test.key
77
 
78
# If a YAML config file exists next to the script, load and merge it
79
$config = loadConfig($configFileName, $config );
25 rodolico 80
# set some defaults
81
$config->{'status_file'} = "$0.status" unless ( defined $config->{'status_file'} );
24 rodolico 82
 
25 rodolico 83
 
24 rodolico 84
die "Invalid config file: missing source and/or target server\n"
85
    unless (defined $config->{source_server} && defined $config->{target_server});
86
 
87
my $servername = `hostname -s`;
88
chomp $servername;
89
if ( $servername eq $config->{source_server}->{hostname} ) {
25 rodolico 90
    logMsg "Running as source server\n";
24 rodolico 91
    # source server logic here
92
} elsif ( $servername eq $config->{target_server}->{hostname} ) {
25 rodolico 93
    logMsg "Running as target server\n";
24 rodolico 94
    mountGeli( $config->{target_server}->{geli} ) if ( defined $config->{target_server}->{geli} );
95
} else {
25 rodolico 96
    logMsg "This server ($servername) is neither source nor target server as per config\n";
97
    die;
24 rodolico 98
}
99
 
25 rodolico 100
# read in history/status file
101
my $targetList = [];
102
if ( -e $config->{status_file} && open my $fh, '<', $config->{status_file} ) {
103
   chomp( my @lines = <$fh> );
104
   $targetList = \@lines;
105
   close $fh;
106
} else {
107
   logMsg("Error: could not read status file '$config->{status_file}': $!");
108
   die;
109
}
24 rodolico 110
 
25 rodolico 111
my $newStatus = [];
112
foreach my $dataset ( sort keys %{$config->{datasets}} ) {
113
   logMsg("Processing dataset '$dataset'\n");
114
   my $sourceList = [ runCmd( "zfs", "list", "-rt", "snap", "-H", "-o", "name", $config->{datasets}->{$dataset}->{source} ) ];
115
 
116
   # process dataset here
117
   my $commands = makeReplicateCommands($sourceList, $targetList, $newStatus );
118
   foreach my $cmd ( @$commands ) {
119
      logMsg("Running command: $cmd\n");
120
      #runCmd( split( /\s+/, $cmd ) );
121
   }
122
}
123
 
124
1;
125
 
126
 
24 rodolico 127
#`cat $config->{input} | openssl enc -aes-256-cbc -K $config->{key} -iv $config->{IV} > $config->{output}`;
128
 
129
# this will decrypt $config->{output} to stdout
130
#`cat $config->{output} | openssl enc -aes-256-cbc -d -K $config->{key} -iv $config->{IV} > test.out`;