class NxtHerokuDatabaseBackup::Manager

Public Instance Methods

backup_latest_dump() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 18
def backup_latest_dump
  error = nil
  download_backup
  encrypt_backup
  upload_backup
  execute_success_callback
rescue StandardError => err
  error = err
ensure
  [backup_tmp_path, encrypted_backup_tmp_path].each do |path|
    File.delete(path) if File.exist?(path)
  end

  raise error if error
end

Private Instance Methods

build_uploader() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 36
def build_uploader
  Uploader::S3.build(
    bucket_name: upload_destination,
    file_path: encrypted_backup_tmp_path,
    backup_name: backup_name
  )
end
database_backup_url() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 78
def database_backup_url
  @database_backup_url ||= begin
    run_heroku_cli_command "pg:backups:url"
  end
end
default_backup_name() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 70
def default_backup_name
  "#{heroku_app_name}-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}.dump"
end
default_backup_tmp_path() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 74
def default_backup_tmp_path
  "/tmp/#{backup_name}"
end
default_environment() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 96
def default_environment
  Object.const_defined?('HerokuEnv') && HerokuEnv.env || 'development'
end
download_backup() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 58
def download_backup
  IO.copy_stream(open(database_backup_url), backup_tmp_path)
end
encrypt_backup() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 48
def encrypt_backup
  data = NxtHerokuDatabaseBackup::Encrypter.new(
    pass_phrase: pass_phrase,
    salt: salt,
    data: File.read(backup_tmp_path)
  ).call

  File.open(encrypted_backup_tmp_path, 'wb') { |f| f.puts(data) }
end
encrypted_backup_tmp_path() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 62
def encrypted_backup_tmp_path
  "#{backup_tmp_path}.encrypted"
end
execute_success_callback() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 66
def execute_success_callback
  success_callback.call
end
heroku_app_name() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 88
def heroku_app_name
  @heroku_app_name ||= ENV.fetch('HEROKU_APP_NAME')
end
run_heroku_cli_command(command) click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 84
def run_heroku_cli_command(command)
  `heroku #{command} -a #{heroku_app_name}`
end
upload_backup() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 44
def upload_backup
  uploader.call
end
upload_destination() click to toggle source
# File lib/nxt_heroku_database_backup/manager.rb, line 92
def upload_destination
  @upload_destination ||= "#{heroku_app_name}-db-backups"
end