Sha256: 6a140863a78724b605d940a5f4a12e305ee2f385f78f53d366104fa998c8c4c3

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

class PgExport
  class DumpStorage
    include Logging

    TIMESTAMP = '_%Y%m%d_%H%M%S'.freeze
    TIMESTAMP_REGEX = '[0-9]{8}_[0-9]{6}'.freeze

    def initialize(ftp_service, name)
      @ftp_service, @name = ftp_service, name
    end

    def upload(dump)
      dump_name = timestamped_name(dump)
      ftp_service.upload_file(dump.path, dump_name)
      logger.info "Upload #{dump} #{dump_name} to #{ftp_service}"
    end

    def download(name)
      dump = EncryptedDump.new
      ftp_service.download_file(dump.path, name)
      logger.info "Download #{dump} #{name} from #{ftp_service}"
      dump
    end

    def remove_old(keep:)
      find_by_name(name).drop(keep.to_i).each do |filename|
        ftp_service.delete(filename)
        logger.info "Remove #{filename} from #{ftp_service}"
      end
    end

    def find_by_name(s)
      ftp_service.list(s + '_*')
    end

    def all
      ftp_service.list('*')
    end

    private

    attr_reader :ftp_service, :name

    def timestamped_name(dump)
      name + Time.now.strftime(TIMESTAMP) + dump.ext
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pg_export-0.4.1 lib/pg_export/services/dump_storage.rb
pg_export-0.4.0 lib/pg_export/services/dump_storage.rb