Skip to content

Commit

Permalink
[BC] Support grouping backups by cluster name (#5)
Browse files Browse the repository at this point in the history
* Rename instance attribute `cluster` to `db_cluster`
* Support grouping backups by cluster name
  • Loading branch information
deric committed May 3, 2022
1 parent 95a4079 commit 54cb28c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
3 changes: 2 additions & 1 deletion manifests/cron_backup.pp
Expand Up @@ -3,6 +3,7 @@
# Typically on a catalog (backup) server.
define pgprobackup::cron_backup(
String $id,
String $cluster,
String $host_group,
Pgprobackup::Backup_type $backup_type,
String $server_address,
Expand Down Expand Up @@ -123,7 +124,7 @@

@@cron { "pgprobackup_${backup_type}_${server_address}-${host_group}":
command => @("CMD"/L),
${binary} ${backup_cmd} --instance ${id} -b ${backup_type} ${stream}--remote-host=${server_address}\
${binary} ${backup_cmd} --instance ${cluster} -b ${backup_type} ${stream}--remote-host=${server_address}\
--remote-user=${remote_user} --remote-port=${remote_port} -U ${db_user} -d ${db_name}\
${logging}${retention}${_threads}${_temp_slot}${_slot}${_validate}${_compress}${_timeout}
| -CMD
Expand Down
23 changes: 19 additions & 4 deletions manifests/instance.pp
Expand Up @@ -4,10 +4,18 @@
#
# @param id
# Unique identifier within `host_group`
# @param cluster
# Could be used to group primary with standby servers
# @param server_address
# Address used for connecting to the DB server
# @param server_port
# DB port
# @param db_name
# Database used for backups
# @param db_user
# User connecting to database
# @param db_cluster
# Postgresql cluster e.g. `main`
# @param db_dir
# PostgreSQL home directory
# @param manage_dbuser
Expand Down Expand Up @@ -58,13 +66,14 @@
# include pgprobackup::instance
class pgprobackup::instance(
String $id = $::hostname,
Optional[String] $cluster = undef,
String $server_address = $::fqdn,
String $cluster = 'main',
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',
Variant[String,Sensitive[String]] $db_password = '',
Optional[String] $seed = undef,
String $remote_user = 'postgres',
Expand Down Expand Up @@ -98,6 +107,11 @@
Optional[Integer] $archive_timeout = undef,
) inherits pgprobackup {

$_cluster = $cluster ? {
undef => $id,
default => $cluster
}

class {'pgprobackup::install':
versions => [$version],
package_name => $package_name,
Expand Down Expand Up @@ -204,12 +218,12 @@

@@exec { "pgprobackup_add_instance_${::fqdn}-${host_group}":
command => @("CMD"/L),
pg_probackup-${version} add-instance -B ${backup_dir} --instance ${id} \
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}/${cluster}
--remote-port=${remote_port} -D ${db_dir}/${version}/${db_cluster}
| -CMD
path => ['/usr/bin'],
onlyif => "test ! -d ${backup_dir}/backups/${id}",
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}"],
Expand All @@ -236,6 +250,7 @@
# 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,
Expand Down
37 changes: 36 additions & 1 deletion spec/classes/instance_spec.rb
Expand Up @@ -56,13 +56,14 @@
}
},
version: '12',
db_cluster: 'dev',
}
end

it {
expect(exported_resources).to contain_exec('pgprobackup_add_instance_psql.localhost-common').with(
tag: 'pgprobackup_add_instance-common',
command: 'pg_probackup-12 add-instance -B /var/lib/pgbackup --instance foo --remote-host=psql.localhost --remote-user=postgres --remote-port=22 -D /var/lib/postgresql/12/main',
command: 'pg_probackup-12 add-instance -B /var/lib/pgbackup --instance foo --remote-host=psql.localhost --remote-user=postgres --remote-port=22 -D /var/lib/postgresql/12/dev',
)
}

Expand Down Expand Up @@ -633,6 +634,40 @@
end
end

context 'with cluster grouping' do
let(:params) do
{
backups: {
common: {
DELTA: {},
FULL: {},
}
},
version: '13',
id: 'psql01a',
cluster: 'psql01',
}
end

['DELTA', 'FULL'].each do |backup|
cmd = '[ -x /usr/bin/pg_probackup-13 ] && /usr/bin/pg_probackup-13 backup'\
" -B /var/lib/pgbackup --instance psql01 -b #{backup} --stream"\
' --remote-host=psql.localhost --remote-user=postgres --remote-port=22'\
' -U backup -d backup --log-filename=psql01a.log'\
' --log-level-file=info --log-directory=/var/lib/pgbackup/log'

it {
expect(exported_resources).to contain_cron("pgprobackup_#{backup}_psql.localhost-common")
.with(
command: cmd,
user: 'pgbackup',
hour: '4',
minute: '0',
)
}
end
end

context 'install specific package version' do
let(:params) do
{
Expand Down

0 comments on commit 54cb28c

Please sign in to comment.