Sha256: f66a68084de81ddbf91085dd4a6febe2b448ce1296c413786964828897443260

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

require "open-uri"
require "aws-sdk-v1"
require "base64"

module HerokuPgBackupsArchive
  class BackupArchive
    def self.perform(backup)
      backup_archive = new(backup)
      backup_archive.perform
    end

    def initialize(backup)
      @backup = backup
    end

    def perform
      bucket.objects[archive_path].write(backup_data, write_options)
    end

    private

    attr_reader :backup

    def bucket
      @bucket ||= s3.buckets[HerokuPgBackupsArchive.config.bucket_name]
    end

    def s3
      @s3 ||= AWS::S3.new(
        access_key_id: HerokuPgBackupsArchive.config.aws_access_key_id,
        secret_access_key: HerokuPgBackupsArchive.config.aws_secret_access_key
      )
    end

    def archive_path
      "#{backup.finished_at.strftime("%Y/%m/%d")}/#{backup.finished_at.iso8601}"
    end

    def backup_data
      open(backup.url)
    end

    def write_options
      unless HerokuPgBackupsArchive.config.sse_customer_key.nil?
        {
          sse_customer_algorithm: :AES256,
          sse_customer_key: Base64.encode64(HerokuPgBackupsArchive.config.sse_customer_key),
          sse_customer_key_md5: Base64.encode64(OpenSSL::Digest::MD5.digest(HerokuPgBackupsArchive.config.sse_customer_key))
        }
      else
        {}
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
heroku_pg_backups_archive-0.1.3 lib/heroku_pg_backups_archive/backup_archive.rb
heroku_pg_backups_archive-0.1.2 lib/heroku_pg_backups_archive/backup_archive.rb
heroku_pg_backups_archive-0.1.1 lib/heroku_pg_backups_archive/backup_archive.rb
heroku_pg_backups_archive-0.1.0 lib/heroku_pg_backups_archive/backup_archive.rb