Class: Restic::Service::Targets::ResticSFTP

Inherits:
Restic
  • Object
show all
Defined in:
lib/restic/service/targets/restic_sftp.rb

Overview

A target that backs up to a SFTP target using Restic

See README.md for the YAML configuration file format

Constant Summary

Constants inherited from Restic

Restic::Service::Targets::Restic::FORGET_DURATION_KEYS, Restic::Service::Targets::Restic::FORGET_KEYS

Instance Attribute Summary

Attributes inherited from Base

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Restic

#one_filesystem?, #parse_forget_setup, #run_backup, #run_forget, #run_restic

Constructor Details

#initialize(name) ⇒ ResticSFTP

Returns a new instance of ResticSFTP.



8
9
10
11
12
13
14
# File 'lib/restic/service/targets/restic_sftp.rb', line 8

def initialize(name)
    super
    @host = nil
    @username = nil
    @path = nil
    @host_keys = []
end

Class Method Details

.normalize_yaml(yaml) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/restic/service/targets/restic_sftp.rb', line 22

def self.normalize_yaml(yaml)
    %w{host username path password}.each do |required_field|
        if !yaml[required_field]
            raise Conf::InvalidConfigurationFile, "missing '#{required_field}' field in target"
        end
    end
    super
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/restic/service/targets/restic_sftp.rb', line 16

def available?
    ssh = SSHKeys.new
    actual_keys = ssh.query_keys(@host)
    valid?(actual_keys)
end

#forgetObject



55
56
57
58
59
60
61
62
# File 'lib/restic/service/targets/restic_sftp.rb', line 55

def forget
    ssh = SSHKeys.new
    ssh_config_name = ssh.ssh_setup_config(@target_name, @username, @host, @key_path)

    run_forget('-r', "sftp:#{ssh_config_name}:#{@path}", 'forget')
ensure
    ssh.ssh_cleanup_config
end

#runObject



46
47
48
49
50
51
52
53
# File 'lib/restic/service/targets/restic_sftp.rb', line 46

def run
    ssh = SSHKeys.new
    ssh_config_name = ssh.ssh_setup_config(@target_name, @username, @host, @key_path)

    run_backup('-r', "sftp:#{ssh_config_name}:#{@path}", 'backup')
ensure
    ssh.ssh_cleanup_config
end

#setup_from_conf(conf, yaml) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/restic/service/targets/restic_sftp.rb', line 31

def setup_from_conf(conf, yaml)
    @target_name = yaml['name']
    @key_path    = conf.conf_keys_path_for(self)
    @host_keys = SSHKeys.load_keys_from_file(@key_path)
    @host      = yaml['host'].to_str
    @username  = yaml['username'].to_str
    @path      = yaml['path'].to_str
    @password  = yaml['password'].to_str
    super
end

#valid?(actual_keys) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/restic/service/targets/restic_sftp.rb', line 42

def valid?(actual_keys)
    actual_keys.any? { |k| @host_keys.include?(k) }
end