diff options
| author | Rosemary Sanchez <rsanchez@madebyuppercut.com> | 2013-08-09 14:02:28 -0700 |
|---|---|---|
| committer | Rosemary Sanchez <rsanchez@madebyuppercut.com> | 2013-08-09 14:02:28 -0700 |
| commit | f720f3a81cf7a7077bcf320c9a0db0ca4fb1df3a (patch) | |
| tree | b3bd7e60168e7619129281472d12c7c5f916bb18 | |
| parent | c69a7be929e50678c92bd7a7114788de13ee3d74 (diff) | |
| parent | 8eee6727265617955a1839ef2f8c69d9fe75db36 (diff) | |
Add tasks to backup remote databases and restore them locally.
| -rw-r--r-- | config/deploy.rb | 3 | ||||
| -rw-r--r-- | config/recipes/postgresql.rb | 11 | ||||
| -rw-r--r-- | config/recipes/utility.rb | 4 | ||||
| -rw-r--r-- | lib/tasks/db.rake | 17 |
4 files changed, 35 insertions, 0 deletions
diff --git a/config/deploy.rb b/config/deploy.rb index 0ac0220..d6a52e2 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -9,6 +9,8 @@ load "config/recipes/nodejs" load "config/recipes/rbenv" load "config/recipes/newrelic" load "config/recipes/monit" +# general tasks +load "config/recipes/utility" set :application, "parley" set :user, "deployer" @@ -29,3 +31,4 @@ after "deploy", "deploy:cleanup" # keep only the last 5 releases # cap deploy:setup # cap deploy:cold # cap nginx:start # this may be necessary if it didn't start up properly before + diff --git a/config/recipes/postgresql.rb b/config/recipes/postgresql.rb index 63fa23b..8788c4b 100644 --- a/config/recipes/postgresql.rb +++ b/config/recipes/postgresql.rb @@ -32,4 +32,15 @@ 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-%H-%M')}.dump" + backup_path = "#{shared_path}/backups" + run "mkdir -p #{shared_path}/backups" + + run "PGPASSWORD='#{postgresql_password}' pg_dump -Fc --no-acl --no-owner -h #{postgresql_host} -U #{postgresql_user} #{postgresql_database} > #{backup_path}/#{filename}" + download("#{backup_path}/#{filename}", "tmp/#{filename}", :via => :scp) + run_locally "cd tmp; rm database.dump; ln -s #{filename} database.dump" + end end diff --git a/config/recipes/utility.rb b/config/recipes/utility.rb new file mode 100644 index 0000000..6d71371 --- /dev/null +++ b/config/recipes/utility.rb @@ -0,0 +1,4 @@ +desc "tail the logs on an app server (cap qa logs)" +task :logs, roles: :app do + stream "tail -f #{shared_path}/log/#{rails_env}.log" +end diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake new file mode 100644 index 0000000..38fb2c5 --- /dev/null +++ b/lib/tasks/db.rake @@ -0,0 +1,17 @@ +require "yaml" + +namespace :db do + desc 'backup database (rake db:backup["prod"])' + task :backup, :env do |key, value| + environment = value[:env] || 'qa' + sh "cap #{environment} postgresql:backup" + end + + desc 'restore database to local' + 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 |
