├── .gitignore ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── heroku_backup_task.gemspec └── lib ├── heroku_backup_task.rb └── heroku_backup_task ├── tasks.rb └── version.rb /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle 2 | pkg 3 | *.gem 4 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gem "heroku", ">= 1.13.7" 4 | gem "rake" -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: http://rubygems.org/ 3 | specs: 4 | configuration (1.2.0) 5 | heroku (2.1.2) 6 | launchy (>= 0.3.2) 7 | rest-client (~> 1.6.1) 8 | term-ansicolor (~> 1.0.5) 9 | launchy (0.4.0) 10 | configuration (>= 0.0.5) 11 | rake (>= 0.8.1) 12 | mime-types (1.16) 13 | rake (0.8.7) 14 | rest-client (1.6.1) 15 | mime-types (>= 1.16) 16 | term-ansicolor (1.0.5) 17 | 18 | PLATFORMS 19 | ruby 20 | 21 | DEPENDENCIES 22 | heroku (>= 1.13.7) 23 | rake 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # heroku\_backup\_task 2 | 3 | ## FYI: automatic backups are now in private beta 4 | 5 | Kinda thought this was right around the corner ;) 6 | [http://addons.heroku.com/pgbackups](http://addons.heroku.com/pgbackups) 7 | 8 | ## Description 9 | 10 | A simple helper to automate your [Heroku](http://heroku.com) app [backups](http://addons.heroku.com/pgbackups) 11 | 12 | ## Installation 13 | 14 | # Gemfile 15 | gem "heroku_backup_task" 16 | 17 | # Rakefile 18 | require "heroku_backup_task/tasks" 19 | task :cron => :heroku_backup 20 | 21 | # Rakefile (alternative) 22 | require "heroku_backup_task" 23 | task :cron do 24 | # other code here 25 | HerokuBackupTask.execute 26 | end 27 | 28 | Make sure you install the pgbackups addon 29 | 30 | heroku addons:add pgbackups:basic 31 | 32 | ## Configuration 33 | 34 | By default, `heroku_backup_task` will back up `DATABASE_URL`. You can change this with: 35 | 36 | heroku config:add HEROKU_BACKUP_DATABASES="SOME_DATABASE_URL,OTHER_DATABASE_URL" 37 | 38 | ## Usage 39 | 40 | ### NOTE: `heroku_backup_task` will expire your oldest backup to make room for a new backup if necessary. 41 | 42 | Set up cron on your application with 43 | 44 | heroku addons:add cron:daily 45 | 46 | You will see something like this in your cron logs 47 | 48 | [Thu Nov 18 12:59:56 -0500 2010] starting heroku backup task 49 | [Thu Nov 18 12:59:57 -0500 2010] backing up: DATABASE_URL 50 | 51 | ## License 52 | 53 | MIT -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "rubygems" 2 | require "bundler/setup" 3 | 4 | $:.unshift File.expand_path("../lib", __FILE__) 5 | 6 | require "heroku_backup_task/tasks" 7 | 8 | task :cron => :heroku_backup -------------------------------------------------------------------------------- /heroku_backup_task.gemspec: -------------------------------------------------------------------------------- 1 | $:.unshift File.expand_path("../lib", __FILE__) 2 | 3 | require "rubygems" 4 | require "heroku_backup_task/version" 5 | 6 | Gem::Specification.new do |gem| 7 | gem.name = "heroku_backup_task" 8 | gem.version = HerokuBackupTask::VERSION 9 | 10 | gem.authors = "David Dollar, Joe Sak" 11 | gem.email = "joe@joesak.com" 12 | gem.homepage = "http://github.com/joemsak/heroku_backup_task" 13 | 14 | gem.summary = "Automate your Heroku backups. Many thanks & kudos to David for writing this gem." 15 | 16 | gem.files = Dir["**/*"].select { |d| d =~ %r{^(README|bin/|data/|ext/|lib/|spec/|test/)} } 17 | 18 | gem.add_dependency "heroku", ">= 1.13.7" 19 | gem.add_dependency "rake" 20 | end 21 | -------------------------------------------------------------------------------- /lib/heroku_backup_task.rb: -------------------------------------------------------------------------------- 1 | require "heroku" 2 | require "heroku/client/pgbackups" 3 | 4 | module HerokuBackupTask; class << self 5 | 6 | def log(message) 7 | puts "[#{Time.now}] #{message}" 8 | end 9 | 10 | def backups_url 11 | ENV["PGBACKUPS_URL"] 12 | end 13 | 14 | def client 15 | @client ||= Heroku::Client::Pgbackups.new(ENV["PGBACKUPS_URL"]) 16 | end 17 | 18 | def databases 19 | if db = ENV["HEROKU_BACKUP_DATABASES"] 20 | db.split(",").map(&:strip) 21 | else 22 | ["DATABASE_URL"] 23 | end 24 | end 25 | 26 | def backup_name(to_url) 27 | # translate s3://bucket/email/foo/bar.dump => foo/bar 28 | parts = to_url.split('/') 29 | parts.slice(4..-1).join('/').gsub(/\.dump$/, '') 30 | end 31 | 32 | def execute 33 | log "starting heroku backup task" 34 | 35 | databases.each do |db| 36 | db_url = ENV[db] 37 | log "backing up: #{db}" 38 | client.create_transfer(db_url, db, nil, "BACKUP", :expire => true) 39 | end 40 | end 41 | 42 | end; end 43 | -------------------------------------------------------------------------------- /lib/heroku_backup_task/tasks.rb: -------------------------------------------------------------------------------- 1 | require "heroku_backup_task" 2 | 3 | task :heroku_backup do 4 | HerokuBackupTask.execute 5 | end 6 | -------------------------------------------------------------------------------- /lib/heroku_backup_task/version.rb: -------------------------------------------------------------------------------- 1 | module HerokuBackupTask 2 | VERSION = "0.0.5.1" 3 | end 4 | --------------------------------------------------------------------------------