Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Forked from hopsoft/db.rake
Last active January 4, 2016 14:56
Show Gist options
  • Save ifyouseewendy/4074a843d76f376ab3a3 to your computer and use it in GitHub Desktop.
Save ifyouseewendy/4074a843d76f376ab3a3 to your computer and use it in GitHub Desktop.
Rails rake tasks for dump & restore of PostgreSQL databases
# lib/tasks/pg.rake
namespace :pg do
desc "Dumps the database to backups"
task :dump => :environment do
cmd = nil
with_config do |app, host, db, user|
cmd = "pg_dump -h #{host} -d #{db} -U #{user} -Ft -v -c -f #{Rails.root}/public/resources/duoduo/db_backup/#{Time.now.strftime("%Y%m%d%H%M%S")}_#{db}.tar"
end
puts cmd
exec cmd
end
desc "Restores the database from backups"
task :restore, [:timestamp] => :environment do |task,args|
if args.timestamp.present?
cmd = nil
with_config do |app, host, db, user|
cmd = "pg_restore -h #{host} -d #{db} -U #{user} -Ft -v -c #{Rails.root}/public/resources/duoduo/db_backup/#{args.timestamp}_#{db}.tar"
end
Rake::Task["db:drop"].invoke
Rake::Task["db:create"].invoke
puts cmd
exec cmd
else
puts 'Please pass a date to the task'
end
end
private
def with_config
yield Rails.application.class.parent_name.underscore,
ActiveRecord::Base.connection_config[:host] || 'localhost',
ActiveRecord::Base.connection_config[:database],
ActiveRecord::Base.connection_config[:username]
end
end
@ifyouseewendy
Copy link
Author

rake pg:dump
rake pg:restore[timestamp]

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