Module: Backup::DSL

Defined in:
lib/backup-agent/dsl.rb

Instance Method Summary collapse

Instance Method Details

#backup(to:, &block) ⇒ Object



86
87
88
89
# File 'lib/backup-agent/dsl.rb', line 86

def backup(to:, &block)
  storages = [to].flatten.map { |h| h.map { |k, v| self.storages(k => v) } }.flatten
  Backup::Performer.new(storages).tap { |performer| performer.instance_eval(&block) }
end

#command(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/backup-agent/dsl.rb', line 26

def command(*args)
  returned, msec = measure args.map(&:to_s).join(" ") do

    if instance_variable_defined?(:@current_command_environment) && @current_command_environment
      args.unshift(@current_command_environment)
    end

    stdout, stderr, exit_status = \
      if instance_variable_defined?(:@current_command_stdin_data)
        Open3.capture3 *args, \
          stdin_data: @current_command_stdin_data,
          binmode:    @current_command_stdin_data_binmode
      else
        Open3.capture3(*args)
      end

    fail stderr unless exit_status.success?
    # echo stdout
    stdout
  end
  returned
end

#construct_filename(basename, extension_with_dot = nil) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/backup-agent/dsl.rb', line 58

def construct_filename(basename, extension_with_dot = nil)
  [basename.gsub(/[^[[:alnum:]]]/i, "-")
           .gsub(/[-–—]+/, "-")
           .mb_chars.downcase.to_s,
   "--#{Time.now.getutc.strftime("%Y-%m-%d--%H-%M-%S--UTC")}",
   extension_with_dot.to_s.mb_chars.downcase.to_s].join("")
end

#credentials(pair = nil, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/backup-agent/dsl.rb', line 76

def credentials(pair = nil, &block)
  if pair
    Backup::Credentials.instance[pair]
  elsif block
    Backup::Credentials.instance.instance_exec(&block)
  else
    Backup::Credentials.instance
  end
end

#delete_backups_older_than(x) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/backup-agent/dsl.rb', line 91

def delete_backups_older_than(x)
  cutoff_timestamp = Time.now.utc.to_i - x
  storages.each do |storage|
    storage.each do |object|
      if object.last_modified.to_i < cutoff_timestamp
        puts "Delete #{object.to_s} from #{storage.to_s}"
        storage.delete(object.id)
      end
    end
  end
end

#echo(*args) ⇒ Object



6
7
8
# File 'lib/backup-agent/dsl.rb', line 6

def echo(*args)
  puts(*args)
end

#measure(action) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/backup-agent/dsl.rb', line 49

def measure(action)
  echo "\n", action
  started  = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
  returned = yield
  finished = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
  echo "(#{ (finished - started).round(1) }ms)", "\n"
  returned
end

#stdin(data, binmode: false) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/backup-agent/dsl.rb', line 17

def stdin(data, binmode: false)
  @current_command_stdin_data         = data
  @current_command_stdin_data_binmode = binmode
  yield
ensure
  remove_instance_variable(:@current_command_stdin_data)
  remove_instance_variable(:@current_command_stdin_data_binmode)
end

#storages(pair = nil, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/backup-agent/dsl.rb', line 66

def storages(pair = nil, &block)
  if pair
    Backup::Storages.instance[pair]
  elsif block
    Backup::Storages.instance.instance_exec(&block)
  else
    Backup::Storages.instance
  end
end

#with(environment) ⇒ Object



10
11
12
13
14
15
# File 'lib/backup-agent/dsl.rb', line 10

def with(environment)
  @current_command_environment = environment&.each_with_object({}) { |(k, v), m| m[k.to_s] = v.to_s }
  yield
ensure
  remove_instance_variable(:@current_command_environment)
end