Skip to content

Instantly share code, notes, and snippets.

@cimm cimm/gist:463765
Created Jul 4, 2010

What would you like to do?
Creates an Heroku bundle and downloads the result, removing the old local backup if needed.
#!/usr/bin/env ruby
app = "your-heroku-app"
STDOUT.sync = true
start_time =
# Prepare old backup for rotate if needed
if File.exist?("#{app}.tar.gz.backup")
puts "\e[31mThe previous backup did not end well, check manually.\e[0m"
exit 1
if File.exist?("#{app}.tar.gz")
File.rename("#{app}.tar.gz", "#{app}.tar.gz.backup")
# Remove the old bundle if needed
print "Removing the old Heroku bundle\t"
bundles = `heroku bundles --app #{app}`
if bundles.include? "complete"
bundle = bundles.split(" ")[0]
`heroku bundles:destroy #{bundle} --app #{app}`
print "[\e[32mDONE\e[0m]\n"
print "[\e[33mN/A\e[0m]\n"
# Capture a new bundle
print "Creating the new Heroku bundle\t"
status = `heroku bundles:capture --app #{app}`
if $? != 0
print "[\e[31mFAILED\e[0m]\n"
exit 1
i = 0
while !status.include?("complete") && i < 10
status = `heroku bundles --app #{app}`
i += 1
sleep 10
if i < 10
print "[\e[32mDONE\e[0m]\n"
print "[\e[31mFAILED\e[0m]\n"
exit 1
# Download the bundle
print "Downloading the Heroku bundle\t"
`heroku bundles:download --app #{app}`
if $? == 0
print "[\e[32mDONE\e[0m]\n"
print "[\e[31mFAILED\e[0m]\n"
exit 1
# Rotate old backup
print "Rotating the local backup\t"
if File.exist?("#{app}.tar.gz") && File.exist?("#{app}.tar.gz.backup")
print "[\e[32mDONE\e[0m]\n"
print "[\e[36mSKIPPED\e[0m]\n"
runtime = - start_time
puts "New Heroku backup for #{app} downloaded in #{runtime} sec."

This comment has been minimized.

Copy link
Owner Author

cimm commented Jul 4, 2010

Depreciated: Heroku depreciated their Bundle backup system in favor of the new PG Backups, you can find a modified version of this script in another gist.

Heroku is awesome! The easiest way to backup you application on Heroku is to pay $20 per month and have unlimited backups (or bundles as they call it).

The cheaper way is to create the backup on the fly (you get one bundle 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 Bundles add-on for this to work (and the Heroku gem as well, but that's kinda obvious... not?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.