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