class PgbackupsArchive::Job

Attributes

backup_url[RW]
client[R]
created_at[RW]

Public Class Methods

call() click to toggle source
# File lib/pgbackups-archive/job.rb, line 11
def self.call
  new.call
end
new(attrs={}) click to toggle source
# File lib/pgbackups-archive/job.rb, line 15
def initialize(attrs={})
  Heroku::Command.load
  @client = Heroku::Command::Pg.new([], :app => ENV["PGBACKUPS_APP"])
end

Public Instance Methods

archive() click to toggle source
# File lib/pgbackups-archive/job.rb, line 28
def archive
  if PgbackupsArchive::Storage.new(key, file).store
    client.display "Backup archived"
  end
end
call() click to toggle source
# File lib/pgbackups-archive/job.rb, line 20
def call
  # expire  # Disabled b/c Heroku seems to be keeping only 2 on its own
  capture
  download
  archive
  delete
end
capture() click to toggle source
# File lib/pgbackups-archive/job.rb, line 34
def capture
  attachment = client.send(:generate_resolver).resolve(database)
  backup = client.send(:hpg_client, attachment).backups_capture
  client.send(:poll_transfer, "backup", backup[:uuid])

  self.created_at = backup[:created_at]

  self.backup_url = Heroku::Client::HerokuPostgresqlApp
    .new(ENV["PGBACKUPS_APP"]).transfers_public_url(backup[:num])[:url]
end
delete() click to toggle source
# File lib/pgbackups-archive/job.rb, line 45
def delete
  File.delete(temp_file)
end
download() click to toggle source
# File lib/pgbackups-archive/job.rb, line 49
def download
  File.open(temp_file, "wb") do |output|
    streamer = lambda do |chunk, remaining_bytes, total_bytes|
      output.write chunk
    end

    # https://github.com/excon/excon/issues/475
    Excon.get backup_url,
      :response_block    => streamer,
      :omit_default_port => true
  end
end
expire() click to toggle source
# File lib/pgbackups-archive/job.rb, line 62
def expire
  transfers = client.send(:hpg_app_client, ENV["PGBACKUPS_APP"]).transfers
    .select  { |b| b[:from_type] == "pg_dump" && b[:to_type] == "gof3r" }
    .sort_by { |b| b[:created_at] }

  if transfers.size > pgbackups_to_keep
    backup_id  = "b%03d" % transfers.first[:num]
    backup_num = client.send(:backup_num, backup_id)

    expire_backup(backup_num)

    client.display "Backup #{backup_id} expired"
  end
end

Private Instance Methods

database() click to toggle source
# File lib/pgbackups-archive/job.rb, line 84
def database
  ENV["PGBACKUPS_DATABASE"] || "DATABASE_URL"
end
environment() click to toggle source
# File lib/pgbackups-archive/job.rb, line 88
def environment
  defined?(Rails) ? Rails.env : nil
end
expire_backup(backup_num) click to toggle source
# File lib/pgbackups-archive/job.rb, line 79
def expire_backup(backup_num)
  client.send(:hpg_app_client, ENV["PGBACKUPS_APP"])
    .transfers_delete(backup_num)
end
file() click to toggle source
# File lib/pgbackups-archive/job.rb, line 92
def file
  File.open(temp_file, "r")
end
key() click to toggle source
# File lib/pgbackups-archive/job.rb, line 96
def key
  timestamp = created_at.gsub(/\/|\:|\.|\s/, "-").concat(".dump")
  ["pgbackups", environment, timestamp].compact.join("/")
end
pgbackups_to_keep() click to toggle source
# File lib/pgbackups-archive/job.rb, line 101
def pgbackups_to_keep
  ENV["PGBACKUPS_KEEP"] ? ENV["PGBACKUPS_KEEP"].to_i : 30
end
temp_file() click to toggle source
# File lib/pgbackups-archive/job.rb, line 105
def temp_file
  "#{Dir.tmpdir}/pgbackup"
end