Skip to content

Instantly share code, notes, and snippets.

@cimm
Created July 4, 2010 21:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cimm/463765 to your computer and use it in GitHub Desktop.
Save cimm/463765 to your computer and use it in GitHub Desktop.
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 = Time.now
# 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
end
if File.exist?("#{app}.tar.gz")
File.rename("#{app}.tar.gz", "#{app}.tar.gz.backup")
end
# 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"
else
print "[\e[33mN/A\e[0m]\n"
end
# 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
else
i = 0
while !status.include?("complete") && i < 10
status = `heroku bundles --app #{app}`
i += 1
sleep 10
end
if i < 10
print "[\e[32mDONE\e[0m]\n"
else
print "[\e[31mFAILED\e[0m]\n"
exit 1
end
end
# Download the bundle
print "Downloading the Heroku bundle\t"
`heroku bundles:download --app #{app}`
if $? == 0
print "[\e[32mDONE\e[0m]\n"
else
print "[\e[31mFAILED\e[0m]\n"
exit 1
end
# Rotate old backup
print "Rotating the local backup\t"
if File.exist?("#{app}.tar.gz") && File.exist?("#{app}.tar.gz.backup")
File.delete("#{app}.tar.gz.backup")
print "[\e[32mDONE\e[0m]\n"
else
print "[\e[36mSKIPPED\e[0m]\n"
end
runtime = Time.now - start_time
puts "New Heroku backup for #{app} downloaded in #{runtime} sec."
exit
@cimm
Copy link
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