class Rcloner::Backuper

Public Class Methods

new(config) click to toggle source
# 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

Public Instance Methods

backup!() click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

execute(command, env: {}, path: nil) click to toggle source
# File lib/rcloner/backuper.rb, line 86
def execute(command, env: {}, path: nil)
  if ENV['VERBOSE'] == 'true'
    print_command =
      if command.class == Array
        command.join(' ')
      else
        command
      end

    puts "Execute: `#{print_command}`\n\n"
  end

  if path
    system env, *command, chdir: path
  else
    system env, *command
  end
end
full_local_path(relative_path) click to toggle source
# File lib/rcloner/backuper.rb, line 107
def full_local_path(relative_path)
  File.join(@config['origin'], relative_path)
end
validate_config!() click to toggle source
# File lib/rcloner/backuper.rb, line 119
def validate_config!
  if ENV['PASSPHRASE'].nil? || ENV['PASSPHRASE'].empty?
    raise ConfigError, '`PASSPHRASE` env variable is not set'
  end

  unless @config['destination']
    raise ConfigError, 'Please provide a destination option'
  end
end