-
-
Save solutelabs-savan/3307fd21a16919a258c6229b62a49026 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment