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