summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2013-08-09 14:05:30 -0600
committermo khan <mo@mokhan.ca>2013-08-09 14:05:30 -0600
commitfc961ccd9c2d3d78eb0ac8b13c0bb3bd29bd95f4 (patch)
treed7edf4aaec2c720366269c42a55e9bca06d8b120
parent84ee59c6137ebe0e870aa47351bc06708b1260a8 (diff)
use a .dump file for db backups and create rake task
-rw-r--r--config/recipes/postgresql.rb4
-rw-r--r--lib/tasks/db.rake16
2 files changed, 18 insertions, 2 deletions
diff --git a/config/recipes/postgresql.rb b/config/recipes/postgresql.rb
index ee19b02..ae09ba0 100644
--- a/config/recipes/postgresql.rb
+++ b/config/recipes/postgresql.rb
@@ -35,10 +35,10 @@ namespace :postgresql do
desc "Backup the database and copy it locally"
task :backup, roles: :db, only: {primary: true} do
- filename = "#{rails_env}-#{Time.now.strftime('%Y-%m-%d-%H-%M')}.sql"
+ filename = "#{rails_env}-#{Time.now.strftime('%Y-%m-%d-%H-%M')}.dump"
backup_path = "#{shared_path}/backups"
run "mkdir -p #{shared_path}/backups"
- run "pg_dump --clean -h #{postgresql_host} -U #{postgresql_user} -W #{postgresql_database} > #{backup_path}/#{filename}" do |channel, stream, data|
+ run "pg_dump --clean -Fc -h #{postgresql_host} -U #{postgresql_user} -W #{postgresql_database} > #{backup_path}/#{filename}" do |channel, stream, data|
channel.send_data "#{postgresql_password}\n"
end
download("#{backup_path}/#{filename}", "tmp/#{filename}", :via => :scp)
diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake
new file mode 100644
index 0000000..20e7782
--- /dev/null
+++ b/lib/tasks/db.rake
@@ -0,0 +1,16 @@
+require "yaml"
+
+namespace :db do
+ desc 'backup database (rake db:backup["production"]'
+ task :backup, :env do |key, value|
+ environment = value[:env] || 'development'
+ sh "cap #{environment} postgresql:backup"
+ end
+
+ task :restore, :env do |key, value|
+ environment = value[:env] || 'development'
+ all_configuration = YAML.load_file(File.join(File.dirname(__FILE__), '../../config/database.yml' ))
+ config = all_configuration[environment]
+ sh "pg_restore --verbose --clean --no-acl --no-owner -h #{config["host"]} -U #{config["username"]} -d #{config["database"]} tmp/database.dump"
+ end
+end