From 530439b374e18fda6c6d6b81e07d2e6f304785ba Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 9 Aug 2013 11:41:57 -0600 Subject: create cap task to tail the server logs --- config/deploy.rb | 3 +++ config/recipes/server.rb | 4 ++++ 2 files changed, 7 insertions(+) create mode 100644 config/recipes/server.rb diff --git a/config/deploy.rb b/config/deploy.rb index 0ac0220..81b74a3 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/server" 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/server.rb b/config/recipes/server.rb new file mode 100644 index 0000000..6d71371 --- /dev/null +++ b/config/recipes/server.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 -- cgit v1.2.3 From 48f1b63b003425116a1ab8ff70cd5603e78c1c36 Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 9 Aug 2013 13:53:29 -0600 Subject: create cap task to backup a remote database and copy it to the local tmp dir --- config/recipes/postgresql.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 -- cgit v1.2.3 From 84ee59c6137ebe0e870aa47351bc06708b1260a8 Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 9 Aug 2013 13:56:47 -0600 Subject: extract backup_path and add hours and minute to backup file timestamp --- config/recipes/postgresql.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/config/recipes/postgresql.rb b/config/recipes/postgresql.rb index 6e80ebb..ee19b02 100644 --- a/config/recipes/postgresql.rb +++ b/config/recipes/postgresql.rb @@ -35,11 +35,12 @@ 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')}.sql" + filename = "#{rails_env}-#{Time.now.strftime('%Y-%m-%d-%H-%M')}.sql" + backup_path = "#{shared_path}/backups" 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| + run "pg_dump --clean -h #{postgresql_host} -U #{postgresql_user} -W #{postgresql_database} > #{backup_path}/#{filename}" do |channel, stream, data| channel.send_data "#{postgresql_password}\n" end - download("#{shared_path}/backups/#{filename}", "tmp/#{filename}", :via => :scp) + download("#{backup_path}/#{filename}", "tmp/#{filename}", :via => :scp) end end -- cgit v1.2.3 From fc961ccd9c2d3d78eb0ac8b13c0bb3bd29bd95f4 Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 9 Aug 2013 14:05:30 -0600 Subject: use a .dump file for db backups and create rake task --- config/recipes/postgresql.rb | 4 ++-- lib/tasks/db.rake | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 lib/tasks/db.rake 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 -- cgit v1.2.3 From 690f92d17a2ae743cca49a56c41cdbd2b7e1323e Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 9 Aug 2013 14:13:05 -0600 Subject: create symlink to last database backup for easy restores locally --- config/recipes/postgresql.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/recipes/postgresql.rb b/config/recipes/postgresql.rb index ae09ba0..aa8b1dc 100644 --- a/config/recipes/postgresql.rb +++ b/config/recipes/postgresql.rb @@ -42,5 +42,6 @@ namespace :postgresql do channel.send_data "#{postgresql_password}\n" end download("#{backup_path}/#{filename}", "tmp/#{filename}", :via => :scp) + run_locally "cd tmp; ln -s #{filename} database.dump" end end -- cgit v1.2.3 From 0f5075496d4eca5f4879c82cb360e90b7ac8d52a Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 9 Aug 2013 14:18:00 -0600 Subject: remove 'run' block syntax in favor of PGPASSWORD environment variable --- config/recipes/postgresql.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/config/recipes/postgresql.rb b/config/recipes/postgresql.rb index aa8b1dc..7ea9071 100644 --- a/config/recipes/postgresql.rb +++ b/config/recipes/postgresql.rb @@ -38,10 +38,9 @@ namespace :postgresql 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 "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 + + 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; ln -s #{filename} database.dump" + run_locally "cd tmp; rm database.dump; ln -s #{filename} database.dump" end end -- cgit v1.2.3 From cad0386b70822532909ef0d178856a1279fc2e5b Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 9 Aug 2013 14:22:16 -0600 Subject: add quotes around the password field for those crazy passwords with all sorts of interesting characters. --- config/recipes/postgresql.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/recipes/postgresql.rb b/config/recipes/postgresql.rb index 7ea9071..8788c4b 100644 --- a/config/recipes/postgresql.rb +++ b/config/recipes/postgresql.rb @@ -39,7 +39,7 @@ namespace :postgresql do 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}" + 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 -- cgit v1.2.3 From a90d09be8a682523e762bda68a3b69bae2bb2864 Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 9 Aug 2013 14:26:47 -0600 Subject: default to backing up the qa environment --- lib/tasks/db.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 20e7782..6bb4bcb 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -3,7 +3,7 @@ require "yaml" namespace :db do desc 'backup database (rake db:backup["production"]' task :backup, :env do |key, value| - environment = value[:env] || 'development' + environment = value[:env] || 'qa' sh "cap #{environment} postgresql:backup" end -- cgit v1.2.3 From 8eee6727265617955a1839ef2f8c69d9fe75db36 Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 9 Aug 2013 14:29:23 -0600 Subject: add descriptions to the database backup/restore tasks --- config/deploy.rb | 2 +- config/recipes/server.rb | 4 ---- config/recipes/utility.rb | 4 ++++ lib/tasks/db.rake | 3 ++- 4 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 config/recipes/server.rb create mode 100644 config/recipes/utility.rb diff --git a/config/deploy.rb b/config/deploy.rb index 81b74a3..d6a52e2 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -10,7 +10,7 @@ load "config/recipes/rbenv" load "config/recipes/newrelic" load "config/recipes/monit" # general tasks -load "config/recipes/server" +load "config/recipes/utility" set :application, "parley" set :user, "deployer" diff --git a/config/recipes/server.rb b/config/recipes/server.rb deleted file mode 100644 index 6d71371..0000000 --- a/config/recipes/server.rb +++ /dev/null @@ -1,4 +0,0 @@ -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/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 index 6bb4bcb..38fb2c5 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -1,12 +1,13 @@ require "yaml" namespace :db do - desc 'backup database (rake db:backup["production"]' + 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' )) -- cgit v1.2.3