class HerokuS3Backups::Heroku

Attributes

app_name[RW]

Public Class Methods

new(app_name) click to toggle source

@param {string} app_name @return nil

# File lib/heroku_s3_backups.rb, line 11
def initialize(app_name)
  @app_name = app_name
  @backup_filename = nil
end

Public Instance Methods

backup_to_s3(backup_location, options = { capture: true }) click to toggle source

Backup Heroku DB to S3 Valid options:

> {boolean} capture - If true, capture a new backup at the current point in

time. Otherwise, we'll just download the latest backup

@param {hash} options @return nil

# File lib/heroku_s3_backups.rb, line 22
def backup_to_s3(backup_location, options = { capture: true })

  # Capture backups if toggled
  capture if options[:capture]

  # Download the latest backup from Heroku and store it on S3
  generate_backup_filename
  download(@backup_filename)
  store_on_s3(backup_location)

  # Remove the backup from the local system
  remove_backup(@backup_filename)
end
capture(options = { maintenance_mode: false }) click to toggle source

Creates a new Heroku backup for a particular moment in time Valid options:

> {boolean} maintenance_mode - If true, set the application to go into

maintenance mode to prevent further interaction until the capture is
complete

@param {hash} options @return nil

# File lib/heroku_s3_backups.rb, line 43
def capture(options = { maintenance_mode: false })
  # Enable maintenance mode if set
  HerokuCLI.cmd("maintenance:on", @app_name) if options[:maintenance_mode]

  HerokuCLI.cmd("pg:backups:capture", @app_name)

  # Turn off maintenance mode once capture is complete
  HerokuCLI.cmd("maintenance:off", @app_name) if options[:maintenance_mode]
end
download(output_filename) click to toggle source

Download the latest backup TODO: Be more explicit about which DB to download @param {string} output_filename

# File lib/heroku_s3_backups.rb, line 56
def download(output_filename)
  raise "Please specify a filename" if output_filename.length.eql?(0)
  HerokuCLI.cmd("pg:backups:download --output #{output_filename}", @app_name)
end

Private Instance Methods

AWS_S3() click to toggle source

Instantiates a new S3 service using the provided credentials @return S3::Service

# File lib/heroku_s3_backups.rb, line 88
def AWS_S3
  S3::Service.new(
    access_key_id: ENV["S3_ACCESS_KEY_ID"],
    secret_access_key: ENV["S3_SECRET_ACCESS_KEY"]
  )
end
generate_backup_filename() click to toggle source
# File lib/heroku_s3_backups.rb, line 81
def generate_backup_filename
  curr_time = Time.now.strftime("%Y-%m-%d_%H%M%S")
  @backup_filename = "backup_#{curr_time}.dump"
end
remove_backup() click to toggle source
# File lib/heroku_s3_backups.rb, line 63
def remove_backup
  system("rm #{@backup_filename}")
  @backup_filename = nil
end
store_on_s3(backup_location, backup_filename) click to toggle source

Stores the recently backed up file in a specified S3 bucket @param {string} backup_location

# File lib/heroku_s3_backups.rb, line 70
def store_on_s3(backup_location, backup_filename)
  prod_backup_folder = AWS_S3().buckets.find(ENV["S3_PRODUCTION_BACKUP_BUCKET"]).objects(prefix: backup_location)

  backup_obj = prod_backup_folder.build("#{backup_location}/#{backup_filename}")

  # Need to do this to set content length for some reason
  backup_obj.content = open(@backup_filename)

  backup_obj.save
end