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

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

Overview

Base class for all restic-based targets

See README.md for the YAML configuration file format

Direct Known Subclasses

ResticB2, ResticFile, ResticSFTP

Defined Under Namespace

Classes: Forget

Constant Summary collapse

FORGET_DURATION_KEYS =
%w{tags hourly daily weekly monthly yearly}
FORGET_KEYS =
['prune', *FORGET_DURATION_KEYS].freeze

Instance Attribute Summary

Attributes inherited from Base

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#available?, #nice_commands

Constructor Details

#initialize(name) ⇒ Restic

Returns a new instance of Restic.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/restic/service/targets/restic.rb', line 8

def initialize(name)
    super

    @password = nil
    @includes = []
    @excludes = []
    @one_filesystem = false

    @io_class     = 3
    @io_priority  = 0
    @cpu_priority = 19
end

Class Method Details

.normalize_yaml(yaml) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/restic/service/targets/restic.rb', line 25

def self.normalize_yaml(yaml)
    yaml = Hash['includes' => [],
                'excludes' => [],
                'one_filesystem' => false,
                'io_class' => 3,
                'io_priority' => 0,
                'cpu_priority' => 19].merge(yaml)
    if yaml['includes'].empty?
        raise Conf::InvalidConfigurationFile, "nothing to backup"
    elsif !yaml['password']
        raise Conf::InvalidConfigurationFile, "no password field"
    end
    yaml
end

Instance Method Details

#one_filesystem?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/restic/service/targets/restic.rb', line 21

def one_filesystem?
    @one_filesystem
end

#parse_forget_setup(setup) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/restic/service/targets/restic.rb', line 61

def parse_forget_setup(setup)
    parsed = Forget.new true, []
    if (invalid_key = setup.find { |k, _| !FORGET_KEYS.include?(k) })
        raise ArgumentError, "#{invalid_key} is not a valid key within "\
            "'forget', valid keys are: #{FORGET_KEYS.join(", ")}"
    end

    FORGET_KEYS.each do |key|
        parsed[key] = setup.fetch(key, key == "prune")
    end
    parsed
end

#run_backup(*args, **options) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/restic/service/targets/restic.rb', line 74

def run_backup(*args, **options)
    extra_args = []
    if one_filesystem?
        extra_args << '--one-file-system'
    end

    run_restic(*args, *extra_args,
           *@excludes.flat_map { |e| ['--exclude', e] },
           *@includes)
end

#run_forget(*args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/restic/service/targets/restic.rb', line 104

def run_forget(*args)
    extra_args = []
    FORGET_DURATION_KEYS.each do |key|
        arg_key =
            if key == "tags" then "tag"
            else key
            end
        if value = @forget[key]
            extra_args << "--keep-#{arg_key}" << value.to_s
        end
    end
    if @forget.prune?
        extra_args << "--prune"
        puts "PRUNE"
    end
    run_restic(*args, *extra_args)
end

#run_restic(*args, **options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/restic/service/targets/restic.rb', line 85

def run_restic(*args, **options)
    home = ENV['HOME'] || '/root'
    env = if args.first.kind_of?(Hash)
              env = args.shift
          else
              env = Hash.new
          end

    extra_args = []
    if @bandwidth_limit
        limit_KiB = @bandwidth_limit / 1000 
        extra_args << '--limit-download' << limit_KiB.to_s << '--limit-upload' << limit_KiB.to_s
    end

    system(Hash['HOME' => home, 'RESTIC_PASSWORD' => @password].merge(env),
           *nice_commands,
           @restic_path.to_path, "--cleanup-cache", *args, *extra_args, in: :close, **options)
end

#setup_from_conf(conf, yaml) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/restic/service/targets/restic.rb', line 40

def setup_from_conf(conf, yaml)
    super

    @restic_path    = conf.tool_path('restic')
    @password       = yaml['password']
    @includes       = yaml['includes'] || Array.new
    @excludes       = yaml['excludes'] || Array.new
    @one_filesystem = !!yaml['one_filesystem']
    @io_class       = Integer(yaml['io_class'])
    @io_priority    = Integer(yaml['io_priority'])
    @cpu_priority   = Integer(yaml['cpu_priority'])
    @forget         = parse_forget_setup(yaml['forget'] || Hash.new)
end