And create state dir for postgres-make-base-backups
[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.1", { ensure => 'installed' })
20         ensure_packages ( "postgresql-client-9.4", { ensure => 'installed' })
21
22         concat { $postgres::backup_server::globals::make_base_backups:
23                 mode => '0555',
24         }
25         concat::fragment { 'make-base-backups-header':
26                 target => $postgres::backup_server::globals::make_base_backups,
27                 content => template('postgres/backup_server/postgres-make-base-backups.erb'),
28                 order  => '00',
29         }
30         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_base_backup |>>
31         concat::fragment { 'make-base-backups-tail':
32                 target => $postgres::backup_server::globals::make_base_backups,
33                 content  => @(EOTEMPLATE),
34                                 # EOF by make-base-backups-tail fragment
35                                 EOF
36                                 | EOTEMPLATE
37                 order  => '99',
38         }
39         file { '/etc/cron.d/puppet-postgres-make-base-backups': ensure => absent; }
40         file { '/var/lib/dsa/postgres-make-base-backups':
41                 ensure => directory,
42                 owner => 'debbackup',
43         }
44         concat::fragment { 'dsa-puppet-stuff--postgres-make_base_backups':
45                 target => '/etc/cron.d/dsa-puppet-stuff',
46                 content  => @("EOF")
47                         */10 * * * 0 debbackup 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 => ($::hostname in [backuphost]) ? {
105                                 true    => 'absent',
106                                 default => 'present'
107                         },
108                 content  => @(EOF),
109                                 --- 
110                                 backups:
111                                   moszumanska:
112                                     main:
113                                 | EOF
114                 notify => Exec['update dsa-check-backuppg-manual.conf']
115         }
116         File<<| tag == $postgres::backup_server::globals::tag_dsa_check_backupp |>>
117         ensure_packages ( "libhash-merge-simple-perl", { ensure => 'installed' })
118         exec { "update dsa-check-backuppg-manual.conf":
119                 command  => @(EOF),
120                                 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
121                                 | EOF
122                 provider => shell,
123                 refreshonly => true,
124         }
125
126         ####
127         # Maintain .pgpass file on backup servers
128         # #
129         concat { $postgres::backup_server::globals::pgpassfile:
130                 owner => 'debbackup',
131                 group => 'debbackup',
132                 mode  => '0400'
133         }
134         concat::fragment{ 'pgpass-local':
135                 target => $postgres::backup_server::globals::pgpassfile,
136                 source => '/home/debbackup/.pgpass-local',
137                 order  => '00'
138         }
139         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_source_pgpassline |>>
140 }
141
142 define postgres::backup_server::register_backup_clienthost (
143         $sshpubkey = $::postgresql_key,
144         $ipaddrlist = join(getfromhash($site::nodeinfo, 'ldap', 'ipHostNumber'), ","),
145         $hostname = $::hostname,
146 ) {
147         include postgres::backup_server::globals
148
149         if $sshpubkey {
150                 $addr = assert_type(String[1], $ipaddrlist)
151                 @@concat::fragment { "postgresql::server::backup-source-clienthost::$name::$fqdn":
152                         target => $postgres::backup_server::globals::sshkeys_sources ,
153                         content  => @("EOF"),
154                                         ${hostname} ${addr} ${sshpubkey}
155                                         | EOF
156                         tag     => $postgres::backup_server::globals::tag_source_sshkey,
157                 }
158         }
159 }
160
161 define postgres::backup_server::register_backup_cluster (
162         $hostname = $::hostname,
163         $fqdn = $::fqdn,
164         $pg_port,
165         $pg_role,
166         $pg_password,
167         $pg_cluster,
168         $pg_version,
169 ) {
170         include postgres::backup_server::globals
171
172         # foobar.debian.org:5432:*:debian-backup:swordfish
173         @@concat::fragment { "postgresql::server::backup-source-pgpassline::$hostname::$pg_port::$pg_role":
174                 target => $postgres::backup_server::globals::pgpassfile,
175                 content => @("EOF"),
176                                 ${fqdn}:${pg_port}:*:${pg_role}:${pg_password}
177                                 | EOF
178                 tag     => $postgres::backup_server::globals::tag_source_pgpassline,
179         }
180         #
181         # vittoria.debian.org   5432    debian-backup           main            9.6
182         @@concat::fragment { "postgresql::server::backup-source-make-base-backup-entry::$hostname::$pg_port::$pg_role":
183                 target => $postgres::backup_server::globals::make_base_backups,
184                 content => @("EOF"),
185                                 ${fqdn} ${pg_port}      ${pg_role}      ${pg_cluster}   ${pg_version}
186                                 | EOF
187                 tag     => $postgres::backup_server::globals::tag_base_backup,
188         }
189
190         @@file { "/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d/${hostname}-${pg_cluster}.conf":
191                 content  => @("EOF"),
192                                 --- 
193                                 backups:
194                                   ${hostname}:
195                                     ${pg_cluster}:
196                                 | EOF
197                 tag     => $postgres::backup_server::globals::tag_dsa_check_backupp,
198                 notify  => Exec['update dsa-check-backuppg-manual.conf']
199         }
200 }