Class: Bitferry::Restic::Task

Inherits:
Task
  • Object
show all
Defined in:
lib/bitferry.rb

Direct Known Subclasses

Backup, Restore

Constant Summary

Constants inherited from Task

Task::ROUTE

Instance Attribute Summary collapse

Attributes inherited from Task

#exclude, #generation, #include, #modified, #tag

Instance Method Summary collapse

Methods inherited from Task

[], #commit, #delete, delete, intact, live, #live?, lookup, match, new, #process_options, register, registered, reset, restore, #restore_endpoint, #show_filters, stale

Methods included from Logging

log, #log, log=

Constructor Details

#initialize(directory, repository, **opts) ⇒ Task

Returns a new instance of Task.



1056
1057
1058
1059
1060
# File 'lib/bitferry.rb', line 1056

def initialize(directory, repository, **opts)
  super(**opts)
  @directory = directory.is_a?(Endpoint) ? directory : Bitferry.endpoint(directory)
  @repository = repository.is_a?(Endpoint) ? repository : Bitferry.endpoint(repository)
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



1053
1054
1055
# File 'lib/bitferry.rb', line 1053

def directory
  @directory
end

#repositoryObject (readonly)

Returns the value of attribute repository.



1053
1054
1055
# File 'lib/bitferry.rb', line 1053

def repository
  @repository
end

Instance Method Details

#common_optionsObject



1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'lib/bitferry.rb', line 1091

def common_options
  [
    case Bitferry.verbosity
      when :verbose then '--verbose'
      when :quiet then '--quiet'
      else nil
    end,
    '-r', repository.root.to_s
  ].compact
end

#create(directory, repository, password, **opts) ⇒ Object

Raises:

  • (TypeError)


1063
1064
1065
1066
1067
# File 'lib/bitferry.rb', line 1063

def create(directory, repository, password, **opts)
  super(directory, repository, **opts)
  raise TypeError, 'unsupported unencrypted endpoint type' unless self.directory.is_a?(Endpoint::Bitferry)
  Volume[self.directory.volume_tag].vault[tag] = Rclone.obscure(@password = password) # Token is stored on the decrypted end only
end

#execute(*args, simulate: false, chdir: nil) ⇒ Object



1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
# File 'lib/bitferry.rb', line 1103

def execute(*args, simulate: false, chdir: nil)
  cmd = [Restic.executable] + args
  ENV['RESTIC_PASSWORD'] = password
  ENV['RESTIC_PROGRESS_FPS'] = 1.to_s if Bitferry.verbosity == :verbose && Bitferry.ui == :gui
  cms = cmd.collect(&:shellescape).join(' ')
  $stdout.puts cms if Bitferry.verbosity == :verbose
  log.info(cms)
  if simulate
    log.info('(simulated)')
    true
  else
    wd = Dir.getwd unless chdir.nil?
    begin
      Dir.chdir(chdir) unless chdir.nil?
      if Bitferry.ui == :gui
        t = nil
        Open3.popen2e(*cmd) do |i, oe, thr|
          while x = oe.gets; $stdout.puts(x) end
          t = thr
        end
        status = t.value
      else
        status = Open3.pipeline(cmd).first
      end
      raise RuntimeError, "restic exit code #{status.exitstatus}" unless status.success?
      status.success?
    ensure
      Dir.chdir(wd) unless chdir.nil?
    end
  end
end

#externalizeObject



1136
1137
1138
1139
1140
1141
# File 'lib/bitferry.rb', line 1136

def externalize
  super.merge(
    directory: directory.externalize,
    repository: repository.externalize,
  ).compact
end

#formatObject



1085
# File 'lib/bitferry.rb', line 1085

def format = nil

#include_filtersObject



1088
# File 'lib/bitferry.rb', line 1088

def include_filters = include.collect { |x| ['--include', x]}.flatten

#intact?Boolean

Returns:

  • (Boolean)


1073
# File 'lib/bitferry.rb', line 1073

def intact? = live? && directory.intact? && repository.intact?

#passwordObject



1070
# File 'lib/bitferry.rb', line 1070

def password = @password ||= Rclone.reveal(Volume[directory.volume_tag].vault.fetch(tag))

#refers?(volume) ⇒ Boolean

Returns:

  • (Boolean)


1076
# File 'lib/bitferry.rb', line 1076

def refers?(volume) = directory.refers?(volume) || repository.refers?(volume)

#restore(hash) ⇒ Object



1144
1145
1146
1147
1148
1149
1150
1151
1152
# File 'lib/bitferry.rb', line 1144

def restore(hash)
  initialize(
    restore_endpoint(hash.fetch(:directory)),
    restore_endpoint(hash.fetch(:repository)),
    tag: hash.fetch(:task),
    modified: hash.fetch(:modified, DateTime.now)
  )
  super(hash)
end

#touchObject



1079
1080
1081
1082
# File 'lib/bitferry.rb', line 1079

def touch
  @generation = [directory.generation, repository.generation].max + 1
  super
end