This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
forked from dmytro/capistrano-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql_backup.rb
59 lines (46 loc) · 1.96 KB
/
postgresql_backup.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
namespace :postgresql do
##
# Recipe to do backup of the remote PostgreSQL server. Uses
# configuration from config/database.yml
#
# Automatically create pre/post backups on deploy
#
namespace :backup do
set :pg_backup_suffix, ''
set :pg_backup_dir, "#{shared_path}/pg_backups"
desc "Create directory for backup files"
task :setup, :roles => :db, :only => { :primary => true } do
run "[ -d #{pg_backup_dir} ] || { mkdir -p #{pg_backup_dir}; chmod 700 #{pg_backup_dir} ; }"
end
desc "Dump remote production database NOTE: postgreSQL specific"
task :production, :roles => :db, :only => { :primary => true } do
# First lets get the remote database config file so that we can
# read in the database settings
begin
tmp_db_yml = %x{ mktemp /tmp/psql_backup_XXX }.chomp.strip
get("#{shared_path}/config/database.yml", tmp_db_yml)
# load the production settings within the database file
db = YAML::load_file(tmp_db_yml)["production"]
ensure
File.delete tmp_db_yml
end
file = "#{pg_backup_dir}/#{db['database']}_dump.#{Time.now.strftime("%y%m%d.%H%M")}"+
"#{pg_backup_suffix}.bz2"
run "pg_dump --clean --no-owner --no-privileges -U#{db['username']} -h localhost #{db['database']} | bzip2 > #{file}" do |ch, stream, out|
ch.send_data "#{db['password']}\n" if out =~ /^Password:/
puts out
end
end
task :production_pre , :roles => :db, :only => { :primary => true } do
set :pg_backup_suffix, '.prerelease'
postgresql.backup.production
end
task :production_post , :roles => :db, :only => { :primary => true } do
set :pg_backup_suffix, '.postrelease'
postgresql.backup.production
end
before 'deploy:migrate', 'postgresql:backup:production_pre'
before 'deploy:reload', 'postgresql:backup:production_post'
after 'deploy:setup', 'postgresql:backup:setup'
end #backup
end