bd6ef0916d861004bd118ba5a54a5c94aa95df2b
[mirror/dsa-puppet.git] / modules / postgres / manifests / backup_cluster.pp
1 # Backup this cluster
2 #
3 # @param pg_version      pg version of the cluster
4 # @param pg_cluster      cluster name
5 # @param pg_port         port of the postgres cluster
6 # @param db_backup_role  replication role username
7 # @param db_backup_role_password     password of the replication role
8 # @param do_role         create the role (requires setup with postgresql::server)
9 # @param do_hba          update pg_hba (requires setup with postgresql::server)
10 define postgres::backup_cluster(
11   String $pg_version,
12   String $pg_cluster = 'main',
13   Integer $pg_port = 5432,
14   String $db_backup_role = lookup('postgres::backup_cluster::db_backup_role'),
15   String $db_backup_role_password = hkdf('/etc/puppet/secret', "postgresql-${::hostname}-${$pg_cluster}-${pg_port}-backup_role}"),
16   Boolean $do_role = false,
17   Boolean $do_hba = false,
18 ) {
19   $datadir = "/var/lib/postgresql/${pg_version}/${pg_cluster}"
20   file { "${datadir}/.nobackup":
21     content  => ''
22   }
23
24   ## XXX - get these from the roles and ldap
25   # backuphost, storace
26   $backup_servers_addrs = ['5.153.231.12/32', '93.94.130.161/32', '2001:41c8:1000:21::21:12/128', '2a02:158:380:280::161/128']
27
28   if $do_role {
29     postgresql::server::role { $db_backup_role:
30       password_hash => postgresql_password($db_backup_role, $db_backup_role_password),
31       replication   => true,
32     }
33   }
34   if $do_hba {
35     $backup_servers_addrs.each |String $address| {
36       postgresql::server::pg_hba_rule { "debian_backup-${address}":
37         description => 'Open up PostgreSQL for backups',
38         type        => 'hostssl',
39         database    => 'replication',
40         user        => $db_backup_role,
41         address     => $address,
42         auth_method => 'md5',
43       }
44     }
45   }
46
47   # Send connections to the port to the pg-backup chain
48   # there, the register_backup_clienthost class will have
49   # realized the exported allows from the backup servers.
50   #
51   # Any non-matching traffic will fall through and it can
52   # be allowed elsewhere
53   ferm::rule::simple { "dsa-postgres-backup-${pg_port}":
54     description => 'Check for postgres access from backup host',
55     port        => $pg_port,
56     target      => 'pg-backup',
57   }
58
59   postgres::backup_server::register_backup_cluster { "backup-role-${::fqdn}}-${pg_port}":
60     pg_port     => $pg_port,
61     pg_role     => $db_backup_role,
62     pg_password => $db_backup_role_password,
63     pg_cluster  => $pg_cluster,
64     pg_version  => $pg_version,
65   }
66 }