Class: Rcloner::Backuper

Inherits:
Object
  • Object
show all
Defined in:
lib/rcloner/backuper.rb

Defined Under Namespace

Classes: ConfigError

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Backuper

Returns a new instance of Backuper.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rcloner/backuper.rb', line 9

def initialize(config)
  @config = config

  @config['origin'] ||= ENV['RCLONER_ORIGIN'] || ENV['PWD']
  @config['destination'] ||= ENV['RCLONER_DESTINATION']

  # Provide absolute paths for files in include list
  if @config['include']
    @config['include'] = @config['include'].map { |path| full_local_path(path) }
  end

  # If no entry provided for exclude list and include list exists,
  # that probably means we should only backup these derectories,
  # and skip all the rest - so for that we should provide '**' operator:
  unless @config['exclude']
    @config['exclude'] = ['**']
  end

  # Create tmp/ folder inside root_path:
  Dir.chdir(@config['origin']) do
    FileUtils.mkdir_p(File.join @config['origin'], 'tmp')
  end

  validate_config!
end

Instance Method Details

#backup!Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rcloner/backuper.rb', line 35

def backup!
  if before_command = @config.dig('on_backup', 'before')
    puts "Perform before command: #{before_command}"

    Dir.chdir(@config['origin']) do
      execute before_command
    end
  end

  # https://github.com/GilGalaad/duplicity-rclone does not creates
  # folders automatically on a remote destination (if there's no folder
  # command will fail). So first we will create backup folder using rclone:
  if @config['destination'].include?('rclone://')
    rclone_storage = @config['destination'].sub('rclone://', '')
    execute %W(rclone mkdir #{rclone_storage})
  end

  @command = %W(duplicity)
  @config['include'].each { |i| @command.push('--include', i) }
  @config['exclude'].each { |i| @command.push('--exclude', i) }

  @command.push(@config['origin'], @config['destination'])
  execute @command

  if after_command = @config.dig('on_backup', 'after')
    puts "Perform after command: #{after_command}"

    Dir.chdir(@config['origin']) do
      execute after_command
    end
  end
end

#restore!(to = nil, force = false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rcloner/backuper.rb', line 68

def restore!(to = nil, force = false)
  @command = %W(duplicity)

  if to
    to = File.expand_path(to)
  else
    to = @config['origin']
  end

  @command.push('restore', @config['destination'], to)
  @command.push('--force') if force
  execute @command
end