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