102f264a69aa5cccfcb4e5e83613d3f861685ce0
[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   ferm::rule::simple { "dsa-postgres-backup-${pg_port}":
47     description => 'Allow postgress access from backup host',
48     port        => $pg_port,
49     saddr       => $backup_servers_addrs,
50   }
51
52   postgres::backup_server::register_backup_cluster { "backup-role-${::fqdn}}-${pg_port}":
53     pg_port     => $pg_port,
54     pg_role     => $db_backup_role,
55     pg_password => $db_backup_role_password,
56     pg_cluster  => $pg_cluster,
57     pg_version  => $pg_version,
58   }
59 }