c9c3d470f2edb17798be2f353b9b2d74ee5d0632
[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         if $::hostname in [backuphost] {
41                 concat::fragment { 'dsa-puppet-stuff--porterbox-chroot-update':
42                         target => '/etc/cron.d/dsa-puppet-stuff',
43                         content  => @("EOF")
44                                 20 1 * * 0 debbackup chronic ${$postgres::backup_server::globals::make_base_backups}
45                                 | EOF
46                 }
47         } else  {
48                 concat::fragment { 'dsa-puppet-stuff--porterbox-chroot-update':
49                         target => '/etc/cron.d/dsa-puppet-stuff',
50                         content  => @("EOF")
51                                 20 0 * * 6 debbackup chronic ${$postgres::backup_server::globals::make_base_backups}
52                                 | EOF
53                 }
54         }
55
56         ####
57         # Maintain authorized_keys file on backup servers for WAL shipping
58         #
59         # do not let other hosts directly build our authorized_keys file,
60         # instead go via a script that somewhat validates intput
61         file { '/etc/dsa/postgresql-backup':
62                 ensure => 'directory',
63         }
64         file { '/usr/local/bin/postgres-make-backup-sshauthkeys':
65                 content => template('postgres/backup_server/postgres-make-backup-sshauthkeys.erb'),
66                 mode   => '0555',
67                 notify  => Exec['postgres-make-backup-sshauthkeys'],
68         }
69         file { '/usr/local/bin/postgres-make-one-base-backup':
70                 source  => 'puppet:///modules/postgres/backup_server/postgres-make-one-base-backup',
71                 mode   => '0555'
72         }
73         file { '/etc/dsa/postgresql-backup/sshkeys-manual':
74                 content => template('postgres/backup_server/sshkeys-manual.erb'),
75                 notify  => Exec['postgres-make-backup-sshauthkeys'],
76         }
77         concat { $postgres::backup_server::globals::sshkeys_sources:
78                 notify  => Exec['postgres-make-backup-sshauthkeys'],
79         }
80         concat::fragment { 'postgresql-backup/source-sshkeys-header':
81                 target => $postgres::backup_server::globals::sshkeys_sources ,
82                 content  => @(EOF),
83                                 # <name> <ip addresses> <key>
84                                 | EOF
85                 order  => '00',
86         }
87         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_source_sshkey |>>
88         exec { "postgres-make-backup-sshauthkeys":
89                 command => "/usr/local/bin/postgres-make-backup-sshauthkeys",
90                 refreshonly => true,
91         }
92
93         ####
94         # Maintain /etc/nagios/dsa-check-backuppg.conf
95         #
96         file { '/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d':
97                 ensure => 'directory',
98                 purge   => true,
99                 force   => true,
100                 recurse => true,
101                 source  => 'puppet:///files/empty/',
102                 notify => Exec['update dsa-check-backuppg-manual.conf'],
103         }
104         file { '/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d/manual.conf':
105                 content => template('postgres/backup_server/dsa-check-backuppg-manual.conf.erb'),
106                 notify => Exec['update dsa-check-backuppg-manual.conf']
107         }
108         file { '/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d/moszumanska.conf':
109                 ensure => ($::hostname in [backuphost]) ? {
110                                 true    => 'absent',
111                                 default => 'present'
112                         },
113                 content  => @(EOF),
114                                 --- 
115                                 backups:
116                                   moszumanska:
117                                     main:
118                                 | EOF
119                 notify => Exec['update dsa-check-backuppg-manual.conf']
120         }
121         File<<| tag == $postgres::backup_server::globals::tag_dsa_check_backupp |>>
122         ensure_packages ( "libhash-merge-simple-perl", { ensure => 'installed' })
123         exec { "update dsa-check-backuppg-manual.conf":
124                 command  => @(EOF),
125                                 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
126                                 | EOF
127                 provider => shell,
128                 refreshonly => true,
129         }
130
131         ####
132         # Maintain .pgpass file on backup servers
133         # #
134         concat { $postgres::backup_server::globals::pgpassfile:
135                 owner => 'debbackup',
136                 group => 'debbackup',
137                 mode  => '0400'
138         }
139         concat::fragment{ 'pgpass-local':
140                 target => $postgres::backup_server::globals::pgpassfile,
141                 source => '/home/debbackup/.pgpass-local',
142                 order  => '00'
143         }
144         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_source_pgpassline |>>
145 }
146
147 define postgres::backup_server::register_backup_clienthost (
148         $sshpubkey = $::postgresql_key,
149         $ipaddrlist = join(getfromhash($site::nodeinfo, 'ldap', 'ipHostNumber'), ","),
150         $hostname = $::hostname,
151 ) {
152         include postgres::backup_server::globals
153
154         if $sshpubkey {
155                 $addr = assert_type(String[1], $ipaddrlist)
156                 @@concat::fragment { "postgresql::server::backup-source-clienthost::$name::$fqdn":
157                         target => $postgres::backup_server::globals::sshkeys_sources ,
158                         content  => @("EOF"),
159                                         ${hostname} ${addr} ${sshpubkey}
160                                         | EOF
161                         tag     => $postgres::backup_server::globals::tag_source_sshkey,
162                 }
163         }
164 }
165
166 define postgres::backup_server::register_backup_cluster (
167         $hostname = $::hostname,
168         $fqdn = $::fqdn,
169         $pg_port,
170         $pg_role,
171         $pg_password,
172         $pg_cluster,
173         $pg_version,
174 ) {
175         include postgres::backup_server::globals
176
177         # foobar.debian.org:5432:*:debian-backup:swordfish
178         @@concat::fragment { "postgresql::server::backup-source-pgpassline::$hostname::$pg_port::$pg_role":
179                 target => $postgres::backup_server::globals::pgpassfile,
180                 content => @("EOF"),
181                                 ${fqdn}:${pg_port}:*:${pg_role}:${pg_password}
182                                 | EOF
183                 tag     => $postgres::backup_server::globals::tag_source_pgpassline,
184         }
185         #
186         # vittoria.debian.org   5432    debian-backup           main            9.6
187         @@concat::fragment { "postgresql::server::backup-source-make-base-backup-entry::$hostname::$pg_port::$pg_role":
188                 target => $postgres::backup_server::globals::make_base_backups,
189                 content => @("EOF"),
190                                 ${fqdn} ${pg_port}      ${pg_role}      ${pg_cluster}   ${pg_version}
191                                 | EOF
192                 tag     => $postgres::backup_server::globals::tag_base_backup,
193         }
194
195         @@file { "/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d/${hostname}-${pg_cluster}.conf":
196                 content  => @("EOF"),
197                                 --- 
198                                 backups:
199                                   ${hostname}:
200                                     ${pg_cluster}:
201                                 | EOF
202                 tag     => $postgres::backup_server::globals::tag_dsa_check_backupp,
203                 notify  => Exec['update dsa-check-backuppg-manual.conf']
204         }
205 }