Skip to content

Commit 8dd86f6

Browse files
supercownpwalker
authored andcommitted
Refactor to use a single non-dynamic script (#1)
Prior to this commit, a separate backup script was generated for each database. This commit replaces all of that with a single bash script that can backup an arbitrary list of databases.
1 parent 983616b commit 8dd86f6

File tree

4 files changed

+80
-58
lines changed

4 files changed

+80
-58
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
BACKUPDIR=/opt/puppetlabs/server/data/postgresql/9.4/backups
4+
LOGDIR=/var/log/puppetlabs/pe_databases_backup
5+
6+
while [[ $# -gt 0 ]]; do
7+
arg="$1"
8+
case $arg in
9+
-t)
10+
BACKUPDIR="$2"
11+
shift; shift
12+
;;
13+
-l)
14+
LOGDIR="$2"
15+
shift; shift
16+
;;
17+
*)
18+
DATABASES="${DATABASES} $1"
19+
shift
20+
;;
21+
esac
22+
done
23+
24+
if [[ -z "${DATABASES}" ]]; then
25+
echo "Usage: $0 [-l LOG_DIRECTORY] [-t BACKUP_TARGET] <DATABASE1> [DATABASE...]"
26+
exit 1
27+
fi
28+
29+
for db in $DATABASES; do
30+
echo "Starting dump of database: ${db}" >> ${LOGDIR}/${db}.log 2>&1
31+
32+
if [ ${db} == "pe-classifier" ]; then
33+
#Save space before backing up by clearing unused node_check_ins table
34+
/opt/puppetlabs/server/bin/psql -d pe-classifier -c 'TRUNCATE TABLE node_check_ins'
35+
if [ $? != 0 ]; then
36+
echo "Failed to truncate node_check_ins table."
37+
fi
38+
fi
39+
40+
/opt/puppetlabs/server/bin/pg_dump -Fc ${db} -f ${BACKUPDIR}/${db}_`date +%m_%d_%y_%H_%M`.bin >> ${LOGDIR}/${db}.log 2>&1
41+
42+
result=$?
43+
if [[ $result -eq 0 ]]; then
44+
echo "Completed dump of database: ${db}" >> ${LOGDIR}/${db}.log 2>&1
45+
else
46+
echo "Failed to dump database ${db}. Exit code is: ${result}" >> ${LOGDIR}/${db}.log 2>&1
47+
exit 1
48+
fi
49+
done

manifests/backup.pp

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,19 @@
11
define pe_databases::backup (
22
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',
53
String $minute = '30',
64
String $hour = '23',
75
String $weekday = '*',
8-
String $logging_directory = '/var/log/puppetlabs/pe_databases_backup',
96
) {
107

11-
ensure_resource( 'file', [ '/opt/puppetlabs/pe_databases', $script_directory, $backup_directory ],
12-
{ 'ensure' => 'directory' }
13-
)
14-
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 :
24-
ensure => file,
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-
}),
30-
owner => 'pe-postgres',
31-
group => 'pe-postgres',
32-
mode => '0750',
33-
before => Cron["puppet_enterprise_database_backup_${databases_to_backup}"],
34-
}
8+
$db_string = join($databases_to_backup, " ")
359

3610
cron { "puppet_enterprise_database_backup_${databases_to_backup}":
37-
ensure => present,
38-
command => $script_path,
39-
user => 'pe-postgres',
40-
minute => $minute,
41-
hour => $hour,
42-
weekday => $weekday,
11+
ensure => present,
12+
command => "${pe_databases::backup_script_path} -l ${pe_databases::backup_logging_directory} -t ${pe_databases::backup_directory} ${db_string}",
13+
user => 'pe-postgres',
14+
minute => $minute,
15+
hour => $hour,
16+
weekday => $weekday,
17+
require => File['puppet_enterprise_db_backup_script'],
4318
}
4419
}

manifests/init.pp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
],
2121
Boolean $manage_database_maintenance = true,
2222
Boolean $manage_postgresql_settings = true,
23+
String $backup_directory = '/opt/puppetlabs/server/data/postgresql/9.4/backups',
24+
String $backup_script_path = '/opt/puppetlabs/pe_databases/scripts/puppet_enterprise_database_backup.sh',
25+
String $backup_logging_directory = '/var/log/puppetlabs/pe_databases_backup',
2326
) {
2427

2528
if $manage_database_maintenance {
@@ -31,6 +34,26 @@
3134
}
3235

3336
if !empty($databases_and_backup_schedule) {
37+
ensure_resource( 'file', [ '/opt/puppetlabs/pe_databases', '/opt/puppetlabs/pe_databases/scripts', $backup_directory ],
38+
{ 'ensure' => 'directory' }
39+
)
40+
41+
ensure_resource( 'file', $backup_logging_directory,
42+
{ 'ensure' => 'directory',
43+
'owner' => 'pe-postgres',
44+
'group' => 'pe-postgres', }
45+
)
46+
47+
48+
file { 'puppet_enterprise_db_backup_script' :
49+
ensure => file,
50+
owner => 'pe-postgres',
51+
group => 'pe-postgres',
52+
mode => '0750',
53+
path => $backup_script_path,
54+
source => 'puppet:///modules/pe_databases/puppet_enterprise_database_backup.sh',
55+
}
56+
3457
$databases_and_backup_schedule.each | Hash $dbs_and_schedule | {
3558
pe_databases::backup{ "${dbs_and_schedule['databases']}" :
3659
databases_to_backup => $dbs_and_schedule['databases'],

templates/puppet_enterprise_database_backup.sh.epp

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)