Skip to content

Instantly share code, notes, and snippets.

@mguterl
Created April 18, 2014 14:04
Show Gist options
  • Save mguterl/11045879 to your computer and use it in GitHub Desktop.
Save mguterl/11045879 to your computer and use it in GitHub Desktop.
namespace :pgbackup do
desc 'capture pgbackup from production'
task :capture => :environment do
system("heroku pgbackups:capture --expire")
end
desc 'download most recent pgbackup from production and put in ../pgbackups'
task :download => :environment do
timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
system("curl -o #{Rails.root.join("pgbackups", "production_#{timestamp}.dump")} --create-dirs `heroku pgbackups:url`")
end
desc 'load most recently downloaded pgbackup into development database'
task :restore => :environment do
last_backup = Dir.entries(Rails.root.join("pgbackups")).last
database = Rails.configuration.database_configuration[Rails.env]['database']
system("pg_restore --verbose --clean --no-acl --no-owner -h localhost -d #{database} pgbackups/#{last_backup}")
end
desc 'capture, download and restore pgbackup from production to development'
task :seed => :environment do
`rake pgbackup:capture`
`rake pgbackup:download`
`rake pgbackup:restore`
end
desc 'capture and restore pgbackup from production to staging'
task :to_staging => :environment do
`rake pgbackup:capture`
system("heroku pgbackups:restore `heroku pgbackups:url --remote production` --remote staging")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment