In the project I’m currently working on, we use Heroku as infrasture provider. Heroku has its own command line operations to execute tasks like db backups, managing addons, etc.

Last March, Heroku deprecated its PG Backups addon, so the CLI syntax changed. The project I’m working on is a Rails project, so by default we already use Rake to run tasks like database migrations. Thus, instead of learning the new Heroku syntax, I’ve decide to write a couple of Rake tasks to wrap the new Heroku commands.

namespace :heroku do
  DEFAULT_APP = 'my_app'

  desc 'Create database backup'
  task :backup, :app do |_task, args|
    args.with_defaults(app: DEFAULT_APP)
    Bundler.with_clean_env do
      `heroku pg:backups capture --app #{args[:app]}`
    end

    puts $?.exitstatus == 0 ? 'Backup created.' : 'Backup failed.'
  end

  desc 'Download latest database backup'
  task :download_backup, :app do |_task, args|
    args.with_defaults(app: DEFAULT_APP)
    Bundler.with_clean_env do
      url = `heroku pg:backups public-url --app #{args[:app]}`
      `curl -o latest.dump "#{url}"`
    end

    puts $?.exitstatus == 0 ? 'Database downloaded.' : 'Download failed.'
  end
end

Next time Heroku updates its command line syntax, I’ll only need to update the commands in my Rake tasks and I won’t have to learn the nex syntax.