summaryrefslogtreecommitdiff
path: root/recipes/postgres.rb
blob: 5851413f55313de3b3e12ea9a61eff9884955064 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
include_recipe "postgresql::server"
include_recipe "postgresql::contrib"
include_recipe "database::postgresql"

database = node["postgres"]["database"]
username = node["postgres"]["username"]
password = node["postgres"]["password"]
host = node["postgres"]["host"]

postgresql_connection_info = {
  host: host,
  port: 5432,
  username: "postgres",
  password: node["postgresql"]["password"]["postgres"],
}

postgresql_database_user username do
  connection postgresql_connection_info
  password password
  action :create
end

postgresql_database database do
  connection postgresql_connection_info
  action :create
end

postgresql_database_user username do
  connection postgresql_connection_info
  database_name database
  privileges [:all]
  action :grant
end

backups_dir = "/var/backups/postgresql"
directory backups_dir do
  user "postgres"
  group "postgres"
  recursive true
end

template "#{backups_dir}/pg_backup_rotated.sh" do
  user "postgres"
  group "postgres"
  mode "0744"
end

template "/etc/postgresql/pg_backup.config" do
  user "postgres"
  group "postgres"
  variables({
    backup_dir: "#{backups_dir}/",
    s3_backup_path: node['stronglifters']['s3_backup_path'],
  })
end

file "/var/lib/postgresql/.pgpass" do
  content "localhost:5432:*:postgres:#{node["postgresql"]["password"]["postgres"]}"
  group "postgres"
  mode "0600"
  user "postgres"
end

aws_config = node['stronglifters']['aws']['profiles']['default']

package "python-pip"
execute "pip install awscli"
cron 'pg_backups' do
  action :create
  command "#{backups_dir}/pg_backup_rotated.sh -c /etc/postgresql/pg_backup.config"
  environment({
    "AWS_ACCESS_KEY_ID" => aws_config['aws_access_key_id'],
    "AWS_SECRET_ACCESS_KEY" => aws_config['aws_secret_access_key'],
    "PGPASSFILE" => "/var/lib/postgresql/.pgpass",
    "PGPASSWORD" => node["postgresql"]["password"]["postgres"],
  })
  hour '1'
  minute '0'
  user 'postgres'
end