Skip to content

Instantly share code, notes, and snippets.

@cimm
Created December 10, 2010 22:56
Show Gist options
  • Save cimm/736953 to your computer and use it in GitHub Desktop.
Save cimm/736953 to your computer and use it in GitHub Desktop.
Creates an Heroku dump and downloads the result, removing the old local backup if needed.
#!/usr/bin/env ruby
app = "your-heroku-app"
STDOUT.sync = true
start_time = Time.now
# Fail if the previous dump did not end well
if File.exist?("#{app}.dump.backup")
puts "\e[31mThe previous dump did not end well, check manually.\e[0m"
exit 1
end
# Prepare old dump for rotate
if File.exist?("#{app}.dump")
File.rename("#{app}.dump", "#{app}.dump.backup")
end
# Ask a new dump and overwrite the oldest one
print "Creating the new Heroku dump\t"
`heroku pgbackups:capture --expire --app #{app}`
if $? != 0
print "[\e[31mFAILED\e[0m]\n"
exit 1
else
print "[\e[32mDONE\e[0m]\n"
end
# Download the dump
print "Downloading the Heroku dump\t"
`curl -s -o #{app}.dump \`heroku pgbackups:url --app #{app}\``
if $? == 0
print "[\e[32mDONE\e[0m]\n"
else
print "[\e[31mFAILED\e[0m]\n"
exit 1
end
# Rotate old dump
print "Rotating the local dump\t\t"
if File.exist?("#{app}.dump") && File.exist?("#{app}.dump.backup")
File.delete("#{app}.dump.backup")
print "[\e[32mDONE\e[0m]\n"
else
print "[\e[36mSKIPPED\e[0m]\n"
end
runtime = Time.now - start_time
puts "New Heroku dump for #{app} downloaded in #{runtime} sec."
exit
@cimm
Copy link
Author

cimm commented Dec 10, 2010

Heroku is awesome! The easiest way to backup you application on Heroku is to pay $30 per month and have unlimited database backups.

The cheaper way is to create the backup on the fly (you get two dumps for free) and download it to a local storage somewhere. This is my preferred solution as it is cheaper and it makes sure you have a local copy of your application if Heroku would fail for some reason.

The script above removes the previous local backup if the new one is successful. You could improve this and have more local backups if needed but this script covers my basic needs.

You'll need to have your Heroku credentials in ~/.heroku/credentials and activate the PG Backups add-on for this to work (and the Heroku gem - at least version 1.14.8 - as well, but that's kinda obvious... not?).

This script is a newer version of my older Heroku backup script using the Heroku backup Bundles but this system has been depreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment