class StellarCoreBackup::S3

Public Class Methods

new(config) click to toggle source
# File lib/stellar-core-backup/s3.rb, line 8
def initialize(config)
  @config       = config
  @working_dir  = StellarCoreBackup::Utils.create_working_dir(@config.get('working_dir'))
  @s3_region    = @config.get('s3_region')
  @s3_bucket    = @config.get('s3_bucket')
  @s3_path      = @config.get('s3_path')
  begin
    @s3_client    = Aws::S3::Client.new(region: @s3_region)
    @s3_resource  = Aws::S3::Resource.new(client: @s3_client)
  rescue Aws::S3::Errors::ServiceError => e
    puts "info: error connecting to s3"
    puts e
  end
end

Public Instance Methods

get(file) click to toggle source

fetches a backup tar file from s3, places in working dir

# File lib/stellar-core-backup/s3.rb, line 38
def get(file)
  local_copy = "#{@working_dir}/#{File.basename(file)}"
  begin
    download = @s3_resource.bucket(@s3_bucket).object(file)
    if download.download_file(local_copy) then
      puts "info: fetched #{file} from s3 (#{@s3_bucket})"
      return local_copy
    else
      puts "error: download from s3 failed"
    end
  rescue Aws::S3::Errors::ServiceError => e
    puts "info: error downloading #{file} from s3 (#{@s3_bucket})"
    puts e
  end
end
latest(listlen) click to toggle source
# File lib/stellar-core-backup/s3.rb, line 54
def latest(listlen)
  begin
    @s3_client.list_objects_v2({bucket: @s3_bucket, prefix: @s3_path+'/core-backup-'}).contents.map{|o| o.key}.sort{|a,b| a.gsub(/(\d+)/,'\1') <=> b.gsub(/(\d+)/,'\1')}.last(listlen)
  rescue Aws::S3::Errors::ServiceError => e
    puts "info: error listing s3 (#{@s3_bucket})"
    puts e
  end
end
push(file) click to toggle source
# File lib/stellar-core-backup/s3.rb, line 23
def push(file)
  begin
    upload = @s3_resource.bucket(@s3_bucket).object("#{@s3_path}/#{File.basename(file)}")
    if upload.upload_file(file) then
      puts "info: pushed #{file} to s3 (#{@s3_bucket})"
    else
      puts "error: upload to s3 failed"
    end
  rescue Aws::S3::Errors::ServiceError => e
    puts "info: error pushing #{file} to s3 (#{@s3_bucket})"
    puts e
  end
end