Skip to content

Commit 2bf7c8f

Browse files
committed
Add backup script and defined type for using it
Prior to this commit, there was no backup script. After this commit, there is a backup script that will iterate through an array of database names to produce a script to backup each database. Using the defined type, you can backup an arbitrary set of databases on a different cron schedule.
1 parent 071cca3 commit 2bf7c8f

File tree

3 files changed

+80
-25
lines changed

3 files changed

+80
-25
lines changed

manifests/backup.pp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
define pe_databases::backup (
2-
$db_name = $title,
3-
$pg_dump_command = '/opt/puppetlabs/server/bin/pg_dump -Fc''
4-
$dump_path = '/opt/puppetlabs/server/data/postgresql/9.4/backups/',
5-
$script_directory = '/usr/local/bin',
6-
$minute = '30',
7-
$hour = '23',
8-
$monthday = '*',
2+
Array[String] $databases_to_backup = [ 'pe-activity', 'pe-classifier', 'pe-postgres', 'pe-puppetdb', 'pe-rbac', 'pe-orchestrator' ],
3+
String $backup_directory = '/opt/puppetlabs/server/data/postgresql/9.4/backups',
4+
String $script_directory = '/opt/puppetlabs/pe_databases/scripts',
5+
String $minute = '30',
6+
String $hour = '23',
7+
String $weekday = '*',
8+
String $logging_directory = '/var/log/puppetlabs/pe_databases_backup',
99
) {
1010

11-
validate_string($pg_dump_command)
12-
validate_absolute_path($dump_path)
13-
validate_absolute_path($script_directory)
14-
validate_string($minute)
15-
validate_string($hour)
16-
validate_string($monthday)
11+
ensure_resource( 'file', [ '/opt/puppetlabs/pe_databases', $script_directory, $backup_directory ],
12+
{ 'ensure' => 'directory' }
13+
)
1714

18-
file { "${script_directory}/dump_${db_name}.sh":
15+
ensure_resource( 'file', $logging_directory,
16+
{ 'ensure' => 'directory',
17+
'owner' => 'pe-postgres',
18+
'group' => 'pe-postgres', }
19+
)
20+
21+
$script_path = "${script_directory}/puppet_enterprise_database_${databases_to_backup}_backup.sh"
22+
23+
file { $script_path :
1924
ensure => file,
20-
content => template('pe_databases/backup/db_dump.sh.erb'),
25+
content => epp('pe_databases/puppet_enterprise_database_backup.sh.epp',
26+
{ databases_to_backup => $databases_to_backup,
27+
logging_directory => $logging_directory,
28+
backup_directory => $backup_directory,
29+
}),
2130
owner => 'pe-postgres',
2231
group => 'pe-postgres',
2332
mode => '0750',
24-
before => Cron["${db_name}_db_dump"],
33+
before => Cron["puppet_enterprise_database_backup_${databases_to_backup}"],
2534
}
2635

27-
cron { "${db_name}_db_dump":
36+
cron { "puppet_enterprise_database_backup_${databases_to_backup}":
2837
ensure => present,
29-
command => "${script_directory}/dump_${db_name}.sh",
38+
command => $script_path,
3039
user => 'pe-postgres',
3140
minute => $minute,
3241
hour => $hour,
33-
monthday => $monthday,
34-
require => File['dump_directory'],
42+
weekday => $weekday,
3543
}
36-
3744
}

manifests/init.pp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
class pe_databases (
2-
Array[String] $databases_to_backup = [ 'pe-activity', 'pe-classifier', 'pe-postgres', 'pe-puppetdb', 'pe-rbac', 'pe-orchestrator' ],
2+
Array[Hash] $databases_and_backup_schedule = [
3+
{
4+
'databases' => ['pe-activity', 'pe-classifier', 'pe-postgres', 'pe-rbac', 'pe-orchestrator'],
5+
'schedule' =>
6+
{
7+
'minute' => '30',
8+
'hour' => '22',
9+
},
10+
},
11+
{
12+
'databases' => ['pe-puppetdb'],
13+
'schedule' =>
14+
{
15+
'minute' => '0',
16+
'hour' => '2',
17+
'weekday' => '7',
18+
},
19+
}
20+
],
321
Boolean $manage_database_maintenance = true,
422
Boolean $manage_postgresql_settings = true,
523
) {
@@ -12,9 +30,14 @@
1230
include pe_databases::postgresql_settings
1331
}
1432

15-
if !empty($databases_to_backup) {
16-
$databases_to_backup.each | String $db | {
17-
pe_databases::backup{ $db :}
33+
if !empty($databases_and_backup_schedule) {
34+
$databases_and_backup_schedule.each | Hash $dbs_and_schedule | {
35+
pe_databases::backup{ "${dbs_and_schedule['databases']}" :
36+
databases_to_backup => $dbs_and_schedule['databases'],
37+
minute => $dbs_and_schedule['schedule']['minute'],
38+
hour => $dbs_and_schedule['schedule']['hour'],
39+
weekday => $dbs_and_schedule['schedule']['weekday'],
40+
}
1841
}
1942
}
2043
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<%- | Array[String] $databases_to_backup,
2+
String $logging_directory,
3+
String $backup_directory,
4+
| -%>
5+
6+
#!/bin/bash
7+
# This file is managed by Puppet
8+
9+
<% $databases_to_backup.each | String $database | { -%>
10+
echo "Starting dump of database: <%= $database -%>" >> <%= $logging_directory -%>/<%= $database -%>.log
11+
12+
<% if $database == 'pe-classifier' { -%>
13+
#Save space before backing up
14+
/opt/puppetlabs/server/bin/psql -d pe-classifier -c 'TRUNCATE TABLE node_check_ins'
15+
<% } -%>
16+
17+
/opt/puppetlabs/server/bin/pg_dump -Fc <%= $database %> -f <%= $backup_directory %>/<%= $database %>_`date +'%m_%d_%y_%R'`.bin
18+
19+
result="$?"
20+
if [[ $result -eq 0 ]]; then
21+
echo "Completed dump of database: <%= $database -%>" >> <%= $logging_directory -%>/<%= $database -%>.log
22+
else
23+
echo "Failed to dump database <%= $database -%>. Exit code is: ${result}" >> <%= $logging_directory -%>/<%= $database -%>.log
24+
fi
25+
<% } -%>

0 commit comments

Comments
 (0)