Puppet Class: pgprobackup::instance

Inherits:
pgprobackup
Defined in:
manifests/instance.pp

Summary

Configure a DB instance

Overview

Prepares PostgreSQL host for running backups.

Examples:

pgprobackup::instance::backups:
  common:
    FULL:
      hour: 3
      minute: 15
      weekday: 0
    DELTA:
      hour: 0
      minute: 45
include pgprobackup::instance

Parameters:

  • id (String) (defaults to: $facts['networking']['hostname'])

    Unique identifier within ‘host_group`

  • cluster (Optional[String]) (defaults to: undef)

    Could be used to group primary with standby servers

  • server_address (String) (defaults to: $facts['networking']['fqdn'])

    Address used for connecting to the DB server

  • server_port (Integer) (defaults to: 5432)

    DB port

  • db_name (String) (defaults to: $pgprobackup::db_name)

    Database used for backups

  • db_user (String) (defaults to: $pgprobackup::db_user)

    User connecting to database

  • db_cluster (String) (defaults to: 'main')

    Postgresql cluster e.g. ‘main`

  • db_dir (String) (defaults to: '/var/lib/postgresql')

    PostgreSQL home directory

  • manage_dbuser (Boolean) (defaults to: true)

    Whether role for running backups should be managed.

  • version (String) (defaults to: lookup('postgresql::globals::version'))

    Major PostgreSQL release version for installing pg_probackup package

  • backups (Optional[Pgprobackup::Config]) (defaults to: undef)

    Hash with backups schedule

  • retention_redundancy (Optional[Integer]) (defaults to: undef)

    The number of full backup copies to keep in the backup catalog.

  • retention_window (Optional[Integer]) (defaults to: undef)

    Defines the earliest point in time for which pg_probackup can complete the recovery.

  • delete_expired (Boolean) (defaults to: true)

    Delete expired backups when ‘retention_redundancy` or `retention_window` is set.

  • merge_expired (Boolean) (defaults to: false)

    Merge expired backups when ‘retention_redundancy` or `retention_window` is set.

  • threads (Optional[Integer]) (defaults to: undef)

    Number of parallel threads

  • temp_slot (Boolean) (defaults to: false)

    Use temporary replication slot

  • slot (Optional[String]) (defaults to: undef)

    Replication slot name

  • validate (Boolean) (defaults to: true)

    Whether backups should be validated after taking backup

  • compress_algorithm (Optional[String]) (defaults to: undef)

    Either ‘zlib`, `pglz` or `none` (default: `none`)

  • compress_level (Integer) (defaults to: 1)

    Integer between 0 and 9 (default: ‘1`)

  • archive_timeout (Optional[Integer]) (defaults to: undef)

    Timeout in seconds for copying all remaining WAL files.

  • remote_user (String) (defaults to: 'postgres')

    user used for ssh connection to the DB instance

  • remote_port (Integer) (defaults to: 22)

    ssh port used for connection to the DB instance from catalog server

  • binary (Optional[String]) (defaults to: undef)

    custom script to be executed as backup command

  • redirect_console (Boolean) (defaults to: false)

    Redirect console output to a log file (make sense especially with custom backup command)

  • log_console (Optional[String]) (defaults to: undef)

    File for storing console logs

  • log_rotation_size (Optional[String]) (defaults to: undef)

    rotate logfile if its size exceeds this value; 0 disables; (default: 0) available units: ‘kB’, ‘MB’, ‘GB’, ‘TB’ (default: kB)

  • log_rotation_age (Optional[String]) (defaults to: undef)

    rotate logfile if its age exceeds this value; 0 disables; (default: 0) available units: ‘ms’, ‘s’, ‘min’, ‘h’, ‘d’ (default: min)

  • db_password (Optional[Variant[String,Sensitive[String]]]) (defaults to: undef)
  • seed (Optional[String]) (defaults to: undef)
  • manage_ssh_keys (Boolean) (defaults to: $pgprobackup::manage_ssh_keys)
  • manage_host_keys (Boolean) (defaults to: $pgprobackup::manage_host_keys)
  • manage_pgpass (Boolean) (defaults to: $pgprobackup::manage_pgpass)
  • manage_hba (Boolean) (defaults to: $pgprobackup::manage_hba)
  • manage_cron (Boolean) (defaults to: $pgprobackup::manage_cron)
  • manage_grants (Boolean) (defaults to: true)

    Whether grants needed for backups are managed, default true

  • archive_wal (Boolean) (defaults to: false)
  • backup_dir (Stdlib::AbsolutePath) (defaults to: $pgprobackup::backup_dir)
  • backup_user (String) (defaults to: $pgprobackup::backup_user)
  • ssh_key_fact (String) (defaults to: $facts['pgprobackup_instance_key'])
  • log_dir (Optional[Stdlib::AbsolutePath]) (defaults to: $pgprobackup::log_dir)
  • log_file (Optional[String]) (defaults to: undef)
  • log_level_file (Optional[Pgprobackup::LogLevel]) (defaults to: undef)
  • log_level_console (Optional[Pgprobackup::LogLevel]) (defaults to: undef)
  • package_name (String) (defaults to: $pgprobackup::package_name)
  • package_ensure (String) (defaults to: $pgprobackup::package_ensure)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'manifests/instance.pp', line 98

class pgprobackup::instance (
  String                            $id                   = $facts['networking']['hostname'],
  Optional[String]                  $cluster              = undef,
  String                            $server_address       = $facts['networking']['fqdn'],
  Integer                           $server_port          = 5432,
  Boolean                           $manage_dbuser        = true,
  String                            $db_dir               = '/var/lib/postgresql',
  String                            $db_name              = $pgprobackup::db_name,
  String                            $db_user              = $pgprobackup::db_user,
  String                            $db_cluster           = 'main',
  Optional[Variant[String,Sensitive[String]]] $db_password = undef,
  Optional[String]                  $seed                 = undef,
  String                            $remote_user          = 'postgres',
  Integer                           $remote_port          = 22,
  Boolean                           $manage_ssh_keys      = $pgprobackup::manage_ssh_keys,
  Boolean                           $manage_host_keys     = $pgprobackup::manage_host_keys,
  Boolean                           $manage_pgpass        = $pgprobackup::manage_pgpass,
  Boolean                           $manage_hba           = $pgprobackup::manage_hba,
  Boolean                           $manage_cron          = $pgprobackup::manage_cron,
  Boolean                           $manage_grants        = true,
  Boolean                           $archive_wal          = false,
  Stdlib::AbsolutePath              $backup_dir           = $pgprobackup::backup_dir,
  String                            $backup_user          = $pgprobackup::backup_user,
  String                            $ssh_key_fact         = $facts['pgprobackup_instance_key'],
  Optional[Stdlib::AbsolutePath]    $log_dir              = $pgprobackup::log_dir,
  Optional[String]                  $log_file             = undef,
  Optional[Pgprobackup::LogLevel]   $log_level_file       = undef,
  Optional[Pgprobackup::LogLevel]   $log_level_console    = undef,
  Optional[Pgprobackup::Config]     $backups              = undef,
  # lint:ignore:lookup_in_parameter
  String                            $version              = lookup('postgresql::globals::version'),
  # lint:endignore
  String                            $package_name         = $pgprobackup::package_name,
  String                            $package_ensure       = $pgprobackup::package_ensure,
  Optional[Integer]                 $retention_redundancy = undef,
  Optional[Integer]                 $retention_window     = undef,
  Boolean                           $delete_expired       = true,
  Boolean                           $merge_expired        = false,
  Optional[Integer]                 $threads              = undef,
  Boolean                           $temp_slot            = false,
  Optional[String]                  $slot                 = undef,
  Boolean                           $validate             = true,
  Optional[String]                  $compress_algorithm   = undef,
  Integer                           $compress_level       = 1,
  Optional[Integer]                 $archive_timeout      = undef,
  Optional[String]                  $binary               = undef,
  Boolean                           $redirect_console     = false,
  Optional[String]                  $log_console          = undef,
  Optional[String]                  $log_rotation_size    = undef,
  Optional[String]                  $log_rotation_age     = undef,
) inherits pgprobackup {
  $_cluster = $cluster ? {
    undef   => $id,
    default => $cluster
  }

  class { 'pgprobackup::install':
    versions       => [$version],
    package_name   => $package_name,
    package_ensure => $package_ensure,
  }

  $_seed = $seed ? {
    undef   => stdlib::fqdn_rand_string(64),
    default => $seed,
  }

  # Generate password if not defined
  $real_password = $db_password ? {
    undef   => stdlib::fqdn_rand_string(64, undef, $_seed),
    default => $db_password =~ Sensitive ? {
      true  => $db_password.unwrap,
      false => $db_password
    },
  }

  if $manage_dbuser {
    postgresql::server::role { $db_user:
      login         => true,
      password_hash => postgresql::postgresql_password($db_user, $real_password),
      superuser     => false,
      replication   => true,
    }

    postgresql::server::database { $db_name:
      owner   => $db_user,
      require => Postgresql::Server::Role[$db_user],
    }

    if $manage_grants {
      case $version {
        '9.6': {
          fail("PostgreSQL ${version} not supported")
        }
        default: {
          # grants for postgresql 10 and newer
          class { 'pgprobackup::grants':
            db_name => $db_name,
            db_user => $db_user,
            version => $version,
            require => Postgresql::Server::Database[$db_name],
          }
        }
      }
    }
  }

  # tag all target catalogs
  if(!empty($backups)) {
    $tags = $backups.map|$group, $config| {
      "pgprobackup-${group}"
    }
  } else {
    $tags = ["pgprobackup-${pgprobackup::host_group}"]
  }

  if $manage_host_keys {
    # Export own host key
    @@sshkey { "postgres-${server_address}":
      ensure       => present,
      host_aliases => [$facts['networking']['hostname'], $facts['networking']['fqdn'], $facts['networking']['ip'], $server_address],
      key          => $facts['ssh']['ecdsa']['key'],
      type         => $pgprobackup::host_key_type,
      target       => "${backup_dir}/.ssh/known_hosts",
      tag          => $tags,
    }
  }

  if $manage_ssh_keys {
    # Export own public SSH key
    if ($ssh_key_fact != undef and $ssh_key_fact != '') {
      $ssh_key_split = split($ssh_key_fact, ' ')
      @@ssh_authorized_key { "${remote_user}-${facts['networking']['fqdn']}":
        ensure => present,
        user   => $remote_user,
        type   => $ssh_key_split[0],
        key    => $ssh_key_split[1],
        tag    => $tags,
      }
    }
  }

  if $manage_pgpass {
    # Export .pgpass content to pgprobackup catalog
    @@file_line { "pgprobackup_pgpass_content-${id}":
      path  => "${backup_dir}/.pgpass",
      line  => "${server_address}:${server_port}:${db_name}:${db_user}:${real_password}",
      match => "^${regexpescape($server_address)}:${server_port}:${db_name}:${db_user}",
      tag   => $tags,
    }

    @@file_line { "pgprobackup_pgpass_replication-${id}":
      path  => "${backup_dir}/.pgpass",
      line  => "${server_address}:${server_port}:replication:${db_user}:${real_password}",
      match => "^${regexpescape($server_address)}:${server_port}:replication:${db_user}",
      tag   => $tags,
    }
  }

  if !empty($backups) {
    $backups.each |String $host_group, Hash $config| {
      # lint:ignore:strict_indent
      @@exec { "pgprobackup_add_instance_${facts['networking']['fqdn']}-${host_group}":
        command => @("CMD"/L),
        pg_probackup-${version} add-instance -B ${backup_dir} --instance ${_cluster} \
        --remote-host=${server_address} --remote-user=${remote_user} \
        --remote-port=${remote_port} -D ${db_dir}/${version}/${db_cluster}
        | -CMD
        path    => ['/usr/bin'],
        cwd     => $backup_dir,
        onlyif  => "test ! -d ${backup_dir}/backups/${_cluster}",
        tag     => "pgprobackup_add_instance-${host_group}",
        user    => $backup_user, # note: error output might not be captured
        require => Package["${package_name}-${version}"],
      }
      # lint:endignore

      # Collect resources exported by pgprobackup::catalog
      Postgresql::Server::Pg_hba_rule <<| tag == "pgprobackup-${host_group}" |>>

      if $manage_ssh_keys {
        # Import public key from backup server as authorized
        Ssh_authorized_key <<| tag == "pgprobackup-catalog-${host_group}" |>> {
          require => Class['postgresql::server'],
        }
      }

      if $manage_host_keys {
        # Import backup server host key
        Sshkey <<| tag == "pgprobackup-catalog-${host_group}" |>>
      }

      if $manage_cron {
        $config.each |$backup_type, $schedule| {
          # declare cron job, use defaults from instance
          create_resources(pgprobackup::cron_backup, { "cron_backup-${host_group}-${server_address}-${backup_type}" => $schedule }, {
              id                   => $id,
              cluster              => $_cluster,
              db_name              => $db_name,
              db_user              => $db_user,
              version              => $version,
              host_group           => $host_group,
              backup_dir           => $backup_dir,
              backup_type          => $backup_type,
              backup_user          => $backup_user,
              server_address       => $server_address,
              delete_expired       => $delete_expired,
              retention_redundancy => $retention_redundancy,
              retention_window     => $retention_window,
              merge_expired        => $merge_expired,
              threads              => $threads,
              temp_slot            => $temp_slot,
              slot                 => $slot,
              validate             => $validate,
              compress_algorithm   => $compress_algorithm,
              compress_level       => $compress_level,
              archive_timeout      => $archive_timeout,
              archive_wal          => $archive_wal,
              log_dir              => $log_dir,
              log_file             => $log_file,
              log_level_file       => $log_level_file,
              log_level_console    => $log_level_console,
              remote_user          => $remote_user,
              remote_port          => $remote_port,
              binary               => $binary,
              redirect_console     => $redirect_console,
              log_rotation_size    => $log_rotation_size,
              log_rotation_age     => $log_rotation_age,
          })
        }
      } # manage_cron
    } # host_group
  }
}