1 class role::backup::postgresql inherits role::backup {
2 # This manifest is supposed to be part of the backup server
4 $password_seed = lookup("base_installation::puppet_pass_seed")
6 $user = lookup("role::backup::user")
7 $group = lookup("role::backup::group")
11 $ldap_cn = lookup("base_installation::ldap_cn")
12 $ldap_password = generate_password(24, $password_seed, "ldap")
13 $ldap_server = lookup("base_installation::ldap_server")
14 $ldap_base = lookup("base_installation::ldap_base")
15 $ldap_dn = lookup("base_installation::ldap_dn")
16 $ldap_attribute = "uid"
18 $pg_slot = regsubst($ldap_cn, '-', "_", "G")
20 ensure_packages(["postgresql", "pgbouncer", "pam_ldap"])
22 $pg_backup_hosts = lookup("role::backup::postgresql::backup_hosts", { "default_value" => {} })
23 $ldap_filter = lookup("role::backup::postgresql::pgbouncer_access_filter", { "default_value" => undef })
25 unless empty($pg_backup_hosts) {
26 file { "/etc/systemd/system/postgresql_backup@.service":
30 content => template("role/backup/postgresql_backup@.service.erb"),
33 unless empty($ldap_filter) {
34 concat { "/etc/pgbouncer/pgbouncer.ini":
38 ensure_newline => true,
39 notify => Service["pgbouncer"],
42 concat::fragment { "pgbouncer_head":
43 target => "/etc/pgbouncer/pgbouncer.ini",
45 content => template("role/backup/pgbouncer.ini.erb"),
48 file { "/etc/systemd/system/pgbouncer.service.d":
49 ensure => "directory",
55 file { "/etc/systemd/system/pgbouncer.service.d/override.conf":
60 content => "[Service]\nUser=\nUser=$pg_user\n",
61 notify => Service["pgbouncer"],
64 service { "pgbouncer":
69 File["/etc/systemd/system/pgbouncer.service.d/override.conf"],
70 Concat["/etc/pgbouncer/pgbouncer.ini"]
74 file { "/etc/pam_ldap.d":
80 file { "/etc/pam_ldap.d/pgbouncer.conf":
85 content => template("role/backup/pam_ldap_pgbouncer.conf.erb"),
87 file { "/etc/pam.d/pgbouncer":
92 source => "puppet:///modules/role/backup/pam_pgbouncer"
97 $pg_backup_hosts.each |$backup_host_cn, $pg_infos| {
98 $host = find_host($facts["ldapvar"]["other"], $backup_host_cn)
100 $pg_backup_host = $backup_host_cn
101 } elsif has_key($host["vars"], "host") {
102 $pg_backup_host = $host["vars"]["host"][0]
104 $pg_backup_host = $host["vars"]["real_hostname"][0]
106 $pg_path = "$mountpoint/$pg_backup_host/postgresql"
107 $pg_backup_path = "$mountpoint/$pg_backup_host/postgresql_backup"
108 $pg_host = "$pg_backup_host"
109 $pg_port = $pg_infos["dbport"]
111 if !empty($ldap_filter) and ($pg_infos["pgbouncer"]) {
112 concat::fragment { "pgbouncer_$pg_backup_host":
113 target => "/etc/pgbouncer/pgbouncer.ini",
115 content => "${pg_infos[pgbouncer_dbname]} = host=$mountpoint/$pg_backup_host/postgresql user=${pg_infos[dbuser]} dbname=${pg_infos[dbname]}",
118 postgresql::server::pg_hba_rule { "$pg_backup_host - local access as ${pg_infos[dbuser]} user":
119 description => "Allow local access to ${pg_infos[dbuser]} user",
121 database => $pg_infos["dbname"],
122 user => $pg_infos["dbuser"],
123 auth_method => 'trust',
125 target => "$pg_path/pg_hba.conf",
126 postgresql_version => "10",
130 file { "$mountpoint/$pg_backup_host":
141 require => File["$mountpoint/$pg_backup_host"],
144 file { $pg_backup_path:
149 require => File["$mountpoint/$pg_backup_host"],
152 cron::job::multiple { "backup_psql_$pg_host":
154 require => [File[$pg_backup_path], File[$pg_path]],
157 command => "/usr/bin/pg_dumpall -h $pg_path -f $pg_backup_path/\$(date -Iseconds).sql",
159 hour => "22,4,10,16",
161 description => "Backup the database",
164 command => "/usr/bin/rm -f $(ls -1 $pg_backup_path/*.sql | grep -v 'T22:' | sort -r | sed -e '1,12d')",
168 description => "Cleanup the database backups",
171 command => "cd $pg_backup_path ; /usr/bin/rm -f $(ls -1 *T22*.sql | log2rotate --skip 7 --fuzz 7 --delete --format='%Y-%m-%dT%H:%M:%S+02:00.sql')",
175 description => "Cleanup the database backups exponentially",
180 exec { "pg_basebackup $pg_path":
183 creates => "$pg_path/PG_VERSION",
184 environment => ["PGPASSWORD=$ldap_password"],
185 command => "/usr/bin/pg_basebackup -w -h $pg_host -U $ldap_cn -D $pg_path -S $pg_slot",
187 Concat["$pg_path/pg_hba.conf"],
188 Concat["$pg_path/recovery.conf"],
189 File["$pg_path/postgresql.conf"],
193 concat { "$pg_path/pg_hba.conf":
199 postgresql::server::pg_hba_rule { "$pg_backup_host - local access as postgres user":
200 description => 'Allow local access to postgres user',
204 auth_method => 'ident',
206 target => "$pg_path/pg_hba.conf",
207 postgresql_version => "10",
209 postgresql::server::pg_hba_rule { "$pg_backup_host - localhost access as postgres user":
210 description => 'Allow localhost access to postgres user',
214 address => "127.0.0.1/32",
215 auth_method => 'md5',
217 target => "$pg_path/pg_hba.conf",
218 postgresql_version => "10",
220 postgresql::server::pg_hba_rule { "$pg_backup_host - localhost ip6 access as postgres user":
221 description => 'Allow localhost access to postgres user',
225 address => "::1/128",
226 auth_method => 'md5',
228 target => "$pg_path/pg_hba.conf",
229 postgresql_version => "10",
231 postgresql::server::pg_hba_rule { "$pg_backup_host - deny access to postgresql user":
232 description => 'Deny remote access to postgres user',
236 address => "0.0.0.0/0",
237 auth_method => 'reject',
239 target => "$pg_path/pg_hba.conf",
240 postgresql_version => "10",
243 postgresql::server::pg_hba_rule { "$pg_backup_host - local access":
244 description => 'Allow local access with password',
248 auth_method => 'md5',
250 target => "$pg_path/pg_hba.conf",
251 postgresql_version => "10",
254 postgresql::server::pg_hba_rule { "$pg_backup_host - local access with same name":
255 description => 'Allow local access with same name',
259 auth_method => 'ident',
261 target => "$pg_path/pg_hba.conf",
262 postgresql_version => "10",
265 $primary_conninfo = "host=$pg_host port=$pg_port user=$ldap_cn password=$ldap_password sslmode=require"
266 $primary_slot_name = regsubst($ldap_cn, '-', "_", "G")
269 concat { "$pg_path/recovery.conf":
275 concat::fragment { "$pg_path/recovery.conf":
276 target => "$pg_path/recovery.conf",
277 content => template('postgresql/recovery.conf.erb'),
280 file { "$pg_path/postgresql.conf":
284 content => template("role/backup/postgresql.conf.erb"),
287 service { "postgresql_backup@$pg_backup_host":
291 File["/etc/systemd/system/postgresql_backup@.service"],
292 Concat["$pg_path/pg_hba.conf"],
293 Concat["$pg_path/recovery.conf"],
294 File["$pg_path/postgresql.conf"],