Subversion Repositories zfs_utils

Rev

Rev 26 | Rev 30 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#! /usr/bin/env perl

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/..";
use Data::Dumper;
use ZFS_Utils qw(loadConfig shredFile logMsg makeReplicateCommands mountDriveByLabel mountGeli runCmd $logFileName $displayLogsOnConsole);

# set the log file to be next to this script
$logFileName = "$FindBin::Bin/sneakernet.log";
# display all log messages on console in addition to the log file
$displayLogsOnConsole = 1;

my $configFileName = "$0.conf.yaml";

my $config = {
   # file created on source server to track last copyed dataset
   'status_file' => "$0.status",
   #information about source server
   'source_server' => {
      'hostname' => '', # used to see if we are on source
      'poolname' => '', # name of the ZFS pool to export
   },
   #information about target server
   'target_server' => {
      'hostname' => '', # used to see if we are on target
      'poolname' => '', # name of the ZFS pool to import
      # if this is set, the dataset uses GELI, so we must decrypt and
      # mount it first
      'geli' => {
         'keydiskname' => 'replica', # the GPT label of the key disk
         'keyfile' => 'geli.key', # the name of the key file on keydiskname
         'localKey' => 'e98c660cccdae1226550484d62caa2b72f60632ae0c607528aba1ac9e7bfbc9c', # hex representation of the local key part
         'target' => '/media/geli.key', # location to create the combined keyfile
         'poolname' => '', # name of the ZFS pool to import
         'diskList' => [ 
            '/dev/gpt/sneakernet_disk' 
            ], # list of disks to try to mount the dataset from
      }
   },
   
   'transport' => {
      # this is the GPT label of the sneakernet disk
      'disk_label' => 'sneakernet',
      # where we want to mount it
      'mount_point' => '/mnt/sneakernet',
      # amount of time to wait for the disk to appear
      'timeout' => 600,
      # if set, all files will be encrypted with this key/IV during transport
      'encryption' => {
         'key'    => '', # openssl rand 32 | xxd -p | tr -d '\n' > test.key
         'IV'     => '00000000000000000000000000000000',
      },
   },
   'datasets' => {
      'iscsi' => {
         'source' => 'storage/backup/iscsi',
         'target' => 'storage/backup/iscsi',
         'filename' => 'iscsi'
      },
      'files_share'  => {
         'source' => 'storage/backup/files_share',
         'target' => 'storage/backup/files_share',
         'filename' => 'files_share'
      },
   }
};


# generate a random key with
# openssl rand 32 | xxd -p | tr -d '\n' > test.key

# If a YAML config file exists next to the script, load and merge it
$config = loadConfig($configFileName, $config );

# set some defaults
$config->{'status_file'} = "$0.status" unless ( defined $config->{'status_file'} );


die "Invalid config file: missing source and/or target server\n"
    unless (defined $config->{source_server} && defined $config->{target_server});

my $servername = `hostname -s`;
chomp $servername;
if ( $servername eq $config->{source_server}->{hostname} ) {
    logMsg "Running as source server\n";
    # source server logic here
} elsif ( $servername eq $config->{target_server}->{hostname} ) {
    logMsg "Running as target server\n";
    mountGeli( $config->{target_server}->{geli} ) if ( defined $config->{target_server}->{geli} );
} else {
    logMsg "This server ($servername) is neither source nor target server as per config\n";
    die;
}

# read in history/status file
my $targetList = [];
if ( -e $config->{status_file} && open my $fh, '<', $config->{status_file} ) {
   chomp( my @lines = <$fh> );
   $targetList = \@lines;
   close $fh;
} else {
   logMsg("Error: could not read status file '$config->{status_file}': $!");
   die;
}

my $newStatus = [];
foreach my $dataset ( sort keys %{$config->{datasets}} ) {
   logMsg("Processing dataset '$dataset'");
   my $sourceList = [ runCmd( "zfs", "list", "-rt", "snap", "-H", "-o", "name", $config->{datasets}->{$dataset}->{source} ) ];
   # process dataset here
   my $commands = makeReplicateCommands($sourceList, $targetList, $newStatus );
   foreach my $cmd ( @$commands ) {
      $cmd .= " > $config->{transport}->{mount_point}/" . $dataset;
      logMsg("Running command: $cmd");
      #runCmd( split( /\s+/, $cmd ) );
   }
}

1;


#`cat $config->{input} | openssl enc -aes-256-cbc -K $config->{key} -iv $config->{IV} > $config->{output}`;

# this will decrypt $config->{output} to stdout
#`cat $config->{output} | openssl enc -aes-256-cbc -d -K $config->{key} -iv $config->{IV} > test.out`;

Generated by GNU Enscript 1.6.5.90.