summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2013-08-09 13:53:29 -0600
committermo khan <mo@mokhan.ca>2013-08-09 13:53:29 -0600
commit48f1b63b003425116a1ab8ff70cd5603e78c1c36 (patch)
tree38fe8b5283d753fdeca465c6e24f8148d10022e7
parent530439b374e18fda6c6d6b81e07d2e6f304785ba (diff)
create cap task to backup a remote database and copy it to the local tmp dir
-rw-r--r--config/recipes/postgresql.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/config/recipes/postgresql.rb b/config/recipes/postgresql.rb
index 63fa23b..6e80ebb 100644
--- a/config/recipes/postgresql.rb
+++ b/config/recipes/postgresql.rb
@@ -32,4 +32,14 @@ namespace :postgresql do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "postgresql:symlink"
+
+ 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')}.sql"
+ run "mkdir -p #{shared_path}/backups"
+ run "pg_dump --clean -h #{postgresql_host} -U #{postgresql_user} -W #{postgresql_database} > #{shared_path}/backups/#{filename}" do |channel, stream, data|
+ channel.send_data "#{postgresql_password}\n"
+ end
+ download("#{shared_path}/backups/#{filename}", "tmp/#{filename}", :via => :scp)
+ end
end