master
 1include_recipe "postgresql::server"
 2include_recipe "postgresql::contrib"
 3include_recipe "database::postgresql"
 4
 5database = node["postgres"]["database"]
 6username = node["postgres"]["username"]
 7password = node["postgres"]["password"]
 8host = node["postgres"]["host"]
 9
10postgresql_connection_info = {
11  host: host,
12  port: 5432,
13  username: "postgres",
14  password: node["postgresql"]["password"]["postgres"],
15}
16
17postgresql_database_user username do
18  connection postgresql_connection_info
19  password password
20  action :create
21end
22
23postgresql_database database do
24  connection postgresql_connection_info
25  action :create
26end
27
28postgresql_database_user username do
29  connection postgresql_connection_info
30  database_name database
31  privileges [:all]
32  action :grant
33end
34
35backups_dir = "/var/backups/postgresql"
36directory backups_dir do
37  user "postgres"
38  group "postgres"
39  recursive true
40end
41
42template "#{backups_dir}/pg_backup_rotated.sh" do
43  user "postgres"
44  group "postgres"
45  mode "0744"
46end
47
48template "/etc/postgresql/pg_backup.config" do
49  user "postgres"
50  group "postgres"
51  variables({
52    backup_dir: "#{backups_dir}/",
53    s3_backup_path: node['stronglifters']['s3_backup_path'],
54  })
55end
56
57file "/var/lib/postgresql/.pgpass" do
58  content "localhost:5432:*:postgres:#{node["postgresql"]["password"]["postgres"]}"
59  group "postgres"
60  mode "0600"
61  user "postgres"
62end
63
64aws_config = node['stronglifters']['aws']['profiles']['default']
65
66package "python-pip"
67execute "pip install awscli"
68cron 'pg_backups' do
69  action :create
70  command "#{backups_dir}/pg_backup_rotated.sh -c /etc/postgresql/pg_backup.config"
71  environment({
72    "AWS_ACCESS_KEY_ID" => aws_config['aws_access_key_id'],
73    "AWS_SECRET_ACCESS_KEY" => aws_config['aws_secret_access_key'],
74    "PGPASSFILE" => "/var/lib/postgresql/.pgpass",
75    "PGPASSWORD" => node["postgresql"]["password"]["postgres"],
76  })
77  hour '1'
78  minute '0'
79  user 'postgres'
80end