Created
January 4, 2014 18:04
-
-
Save owainlewis/8258286 to your computer and use it in GitHub Desktop.
Backup task for Postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :postgres do | |
desc 'Backup postgres database' | |
task :backup => :environment do | |
db_config = Rails.application.config.database_configuration[Rails.env] | |
backup_dir = '/home/deploy/backups' | |
outfile = "#{ backup_dir }/#{ Rails.env }_#{ DateTime.now.strftime("%Y%m%d_%H%M%S.sql.gz") }" | |
cmd = "pg_dump -h localhost -U #{ db_config['username'] } #{ db_config['database'] } | gzip -c > #{outfile}" | |
puts "Making database backup..." | |
system cmd | |
end | |
desc 'Restore postgres from an existing backup' | |
task :restore, [:backup] => [:environment] do |t, args| | |
db_config = Rails.application.config.database_configuration[Rails.env] | |
cmd = "psql -h localhost -U #{ db_config['username'] } #{ db_config['database'] } < #{ args[:backup] }" | |
puts cmd | |
end | |
end |
Author
owainlewis
commented
Jan 4, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment