39b7be00214ffdb25936a187a447ec04efb9fb7a
[mirror/dsa-puppet.git] / modules / postgres / manifests / backup_server.pp
1 #
2 class postgres::backup_server::globals {
3         $make_base_backups = '/usr/local/bin/postgres-make-base-backups'
4         $pgpassfile = '/home/debbackup/.pgpass'
5         $sshkeys_sources = '/etc/dsa/postgresql-backup/sshkeys-sources'
6
7         $tag_base_backup = "postgresql::server::backup-source-make-base-backup-entry"
8         $tag_source_sshkey = "postgresql::server::backup-source-sshkey"
9         $tag_source_pgpassline = "postgresql::server::backup-source-pgpassline"
10         $tag_dsa_check_backupp = "postgresql::server::backup-dsa-check-backuppg"
11 }
12
13 class postgres::backup_server {
14         include postgres::backup_server::globals
15
16         ####
17         # Regularly pull base backups
18         #
19         ensure_packages ( "postgresql-client-9.6", { ensure => 'installed' })
20
21         concat { $postgres::backup_server::globals::make_base_backups:
22                 mode => '0555',
23         }
24         concat::fragment { 'make-base-backups-header':
25                 target => $postgres::backup_server::globals::make_base_backups,
26                 content => template('postgres/backup_server/postgres-make-base-backups.erb'),
27                 order  => '00',
28         }
29         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_base_backup |>>
30         concat::fragment { 'make-base-backups-tail':
31                 target => $postgres::backup_server::globals::make_base_backups,
32                 content  => @(EOTEMPLATE),
33                                 # EOF by make-base-backups-tail fragment
34                                 EOF
35                                 | EOTEMPLATE
36                 order  => '99',
37         }
38         file { '/etc/cron.d/puppet-postgres-make-base-backups': ensure => absent; }
39         file { '/var/lib/dsa/postgres-make-base-backups':
40                 ensure => directory,
41                 owner => 'debbackup',
42                 mode => '0755',
43         }
44         concat::fragment { 'dsa-puppet-stuff--postgres-make_base_backups':
45                 target => '/etc/cron.d/dsa-puppet-stuff',
46                 content  => @("EOF")
47                         */30 * * * * debbackup sleep $(( RANDOM \% 1200 )); chronic ${$postgres::backup_server::globals::make_base_backups}
48                         | EOF
49         }
50
51         ####
52         # Maintain authorized_keys file on backup servers for WAL shipping
53         #
54         # do not let other hosts directly build our authorized_keys file,
55         # instead go via a script that somewhat validates intput
56         file { '/etc/dsa/postgresql-backup':
57                 ensure => 'directory',
58         }
59         file { '/usr/local/bin/postgres-make-backup-sshauthkeys':
60                 content => template('postgres/backup_server/postgres-make-backup-sshauthkeys.erb'),
61                 mode   => '0555',
62                 notify  => Exec['postgres-make-backup-sshauthkeys'],
63         }
64         file { '/usr/local/bin/postgres-make-one-base-backup':
65                 source  => 'puppet:///modules/postgres/backup_server/postgres-make-one-base-backup',
66                 mode   => '0555'
67         }
68         file { '/etc/dsa/postgresql-backup/sshkeys-manual':
69                 content => template('postgres/backup_server/sshkeys-manual.erb'),
70                 notify  => Exec['postgres-make-backup-sshauthkeys'],
71         }
72         concat { $postgres::backup_server::globals::sshkeys_sources:
73                 notify  => Exec['postgres-make-backup-sshauthkeys'],
74         }
75         concat::fragment { 'postgresql-backup/source-sshkeys-header':
76                 target => $postgres::backup_server::globals::sshkeys_sources ,
77                 content  => @(EOF),
78                                 # <name> <ip addresses> <key>
79                                 | EOF
80                 order  => '00',
81         }
82         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_source_sshkey |>>
83         exec { "postgres-make-backup-sshauthkeys":
84                 command => "/usr/local/bin/postgres-make-backup-sshauthkeys",
85                 refreshonly => true,
86         }
87
88         ####
89         # Maintain /etc/nagios/dsa-check-backuppg.conf
90         #
91         file { '/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d':
92                 ensure => 'directory',
93                 purge   => true,
94                 force   => true,
95                 recurse => true,
96                 source  => 'puppet:///files/empty/',
97                 notify => Exec['update dsa-check-backuppg-manual.conf'],
98         }
99         file { '/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d/manual.conf':
100                 content => template('postgres/backup_server/dsa-check-backuppg-manual.conf.erb'),
101                 notify => Exec['update dsa-check-backuppg-manual.conf']
102         }
103         file { '/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d/moszumanska.conf':
104                 ensure => 'absent',
105                 notify => Exec['update dsa-check-backuppg-manual.conf']
106         }
107         File<<| tag == $postgres::backup_server::globals::tag_dsa_check_backupp |>>
108         ensure_packages ( "libhash-merge-simple-perl", { ensure => 'installed' })
109         exec { "update dsa-check-backuppg-manual.conf":
110                 command  => @(EOF),
111                                 perl -MYAML=LoadFile,Dump -MHash::Merge::Simple=merge -E 'say Dump(merge(map{LoadFile($_)}@ARGV))' /etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d/*.conf > /etc/nagios/dsa-check-backuppg.conf
112                                 | EOF
113                 provider => shell,
114                 refreshonly => true,
115         }
116
117         ####
118         # Maintain .pgpass file on backup servers
119         # #
120         concat { $postgres::backup_server::globals::pgpassfile:
121                 owner => 'debbackup',
122                 group => 'debbackup',
123                 mode  => '0400'
124         }
125         concat::fragment{ 'pgpass-local':
126                 target => $postgres::backup_server::globals::pgpassfile,
127                 source => '/home/debbackup/.pgpass-local',
128                 order  => '00'
129         }
130         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_source_pgpassline |>>
131 }
132
133 define postgres::backup_server::register_backup_clienthost (
134         $sshpubkey = $::postgresql_key,
135         $ipaddrlist = join(getfromhash($site::nodeinfo, 'ldap', 'ipHostNumber'), ","),
136         $hostname = $::hostname,
137 ) {
138         include postgres::backup_server::globals
139
140         if $sshpubkey {
141                 $addr = assert_type(String[1], $ipaddrlist)
142                 @@concat::fragment { "postgresql::server::backup-source-clienthost::$name::$fqdn":
143                         target => $postgres::backup_server::globals::sshkeys_sources ,
144                         content  => @("EOF"),
145                                         ${hostname} ${addr} ${sshpubkey}
146                                         | EOF
147                         tag     => $postgres::backup_server::globals::tag_source_sshkey,
148                 }
149         }
150 }
151
152 define postgres::backup_server::register_backup_cluster (
153         $hostname = $::hostname,
154         $fqdn = $::fqdn,
155         $pg_port,
156         $pg_role,
157         $pg_password,
158         $pg_cluster,
159         $pg_version,
160 ) {
161         include postgres::backup_server::globals
162
163         # foobar.debian.org:5432:*:debian-backup:swordfish
164         @@concat::fragment { "postgresql::server::backup-source-pgpassline::$hostname::$pg_port::$pg_role":
165                 target => $postgres::backup_server::globals::pgpassfile,
166                 content => @("EOF"),
167                                 ${fqdn}:${pg_port}:*:${pg_role}:${pg_password}
168                                 | EOF
169                 tag     => $postgres::backup_server::globals::tag_source_pgpassline,
170         }
171         #
172         # vittoria.debian.org   5432    debian-backup           main            9.6
173         @@concat::fragment { "postgresql::server::backup-source-make-base-backup-entry::$hostname::$pg_port::$pg_role":
174                 target => $postgres::backup_server::globals::make_base_backups,
175                 content => @("EOF"),
176                                 ${fqdn} ${pg_port}      ${pg_role}      ${pg_cluster}   ${pg_version}
177                                 | EOF
178                 tag     => $postgres::backup_server::globals::tag_base_backup,
179         }
180
181         @@file { "/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d/${hostname}-${pg_cluster}.conf":
182                 content  => @("EOF"),
183                                 --- 
184                                 backups:
185                                   ${hostname}:
186                                     ${pg_cluster}:
187                                 | EOF
188                 tag     => $postgres::backup_server::globals::tag_dsa_check_backupp,
189                 notify  => Exec['update dsa-check-backuppg-manual.conf']
190         }
191 }