Merge branch 'fordsa' of https://git.adam-barratt.org.uk/git/mirror/dsa-puppet
[mirror/dsa-puppet.git] / modules / postgres / manifests / backup_cluster.pp
1 # Backup this cluster
2 #
3 # This define causes the cluster to be registered on the backupservers.
4 #
5 # Furthermore, if this cluster is managed with postgresql::server and
6 # do_role and do_hba are set, we create the role and modify the pg_hba.conf file.
7 #
8 # Since postgresql::server only supports a single cluster per host, we are moving
9 # towards our own postgres::cluster, and this define also exports a hba rule for
10 # those (regardless of the do_hba setting).  If the cluster is managed with
11 # postgres::cluster and has its manage_hba option set, this will then cause the
12 # backup hosts to be allowed to replacate.
13 #
14 # Regarless of how the cluster is managed, firewall rules are set up to allow
15 # access from the backup hosts.
16 #
17 # @param pg_version      pg version of the cluster
18 # @param pg_cluster      cluster name
19 # @param pg_port         port of the postgres cluster
20 # @param db_backup_role  replication role username
21 # @param db_backup_role_password     password of the replication role
22 # @param do_role         create the role (requires setup with postgresql::server)
23 # @param do_hba          update pg_hba   (requires setup with postgresql::server)
24 define postgres::backup_cluster(
25   String $pg_version,
26   String $pg_cluster = 'main',
27   Integer $pg_port = 5432,
28   String $db_backup_role = lookup('postgres::backup_cluster::db_backup_role'),
29   String $db_backup_role_password = hkdf('/etc/puppet/secret', "postgresql-${::hostname}-${$pg_cluster}-${pg_port}-backup_role}"),
30   Boolean $do_role = false,
31   Boolean $do_hba = false,
32 ) {
33   include postgres::backup_source
34
35   $datadir = "/var/lib/postgresql/${pg_version}/${pg_cluster}"
36   file { "${datadir}/.nobackup":
37     content  => ''
38   }
39
40   ## XXX - get these from the roles and ldap
41   # backuphost, storace
42   $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']
43
44   if $do_role {
45     postgresql::server::role { $db_backup_role:
46       password_hash => postgresql_password($db_backup_role, $db_backup_role_password),
47       replication   => true,
48     }
49   }
50   if $do_hba {
51     $backup_servers_addrs.each |String $address| {
52       postgresql::server::pg_hba_rule { "debian_backup-${address}":
53         description => 'Open up PostgreSQL for backups',
54         type        => 'hostssl',
55         database    => 'replication',
56         user        => $db_backup_role,
57         address     => $address,
58         auth_method => 'md5',
59       }
60     }
61   }
62
63   postgres::cluster::hba_entry { "backup-replication::${pg_version}::${pg_cluster}":
64     pg_version => $pg_version,
65     pg_cluster => $pg_cluster,
66     pg_port    => $pg_port,
67     database   => 'replication',
68     user       => $db_backup_role,
69     address    => $backup_servers_addrs,
70   }
71   postgres::backup_server::register_backup_cluster { "backup-role-${::fqdn}}-${pg_port}":
72     pg_port     => $pg_port,
73     pg_role     => $db_backup_role,
74     pg_password => $db_backup_role_password,
75     pg_cluster  => $pg_cluster,
76     pg_version  => $pg_version,
77   }
78 }