Skip to content

Make an actual backup script #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions files/puppet_enterprise_database_backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

BACKUPDIR=/opt/puppetlabs/server/data/postgresql/9.4/backups
LOGDIR=/var/log/puppetlabs/pe_databases_backup

while [[ $# -gt 0 ]]; do
arg="$1"
case $arg in
-t)
BACKUPDIR="$2"
shift; shift
;;
-l)
LOGDIR="$2"
shift; shift
;;
*)
DATABASES="${DATABASES} $1"
shift
;;
esac
done

if [[ -z "${DATABASES}" ]]; then
echo "Usage: $0 [-l LOG_DIRECTORY] [-t BACKUP_TARGET] <DATABASE1> [DATABASE...]"
exit 1
fi

for db in $DATABASES; do
echo "Starting dump of database: ${db}" >> ${LOGDIR}/${db}.log 2>&1

if [ ${db} == "pe-classifier" ]; then
#Save space before backing up by clearing unused node_check_ins table
/opt/puppetlabs/server/bin/psql -d pe-classifier -c 'TRUNCATE TABLE node_check_ins'
if [ $? != 0 ]; then
echo "Failed to truncate node_check_ins table."
fi
fi

/opt/puppetlabs/server/bin/pg_dump -Fc ${db} -f ${BACKUPDIR}/${db}_`date +%m_%d_%y_%H_%M`.bin >> ${LOGDIR}/${db}.log 2>&1

result=$?
if [[ $result -eq 0 ]]; then
echo "Completed dump of database: ${db}" >> ${LOGDIR}/${db}.log 2>&1
else
echo "Failed to dump database ${db}. Exit code is: ${result}" >> ${LOGDIR}/${db}.log 2>&1
exit 1
fi
done
81 changes: 52 additions & 29 deletions manifests/backup.pp
Original file line number Diff line number Diff line change
@@ -1,37 +1,60 @@
define pe_databases::backup (
$db_name = $title,
$pg_dump_command = '/opt/puppetlabs/server/bin/pg_dump -Fc',
$dump_path = '/opt/puppetlabs/server/data/postgresql/9.4/backups/',
$script_directory = '/usr/local/bin',
$minute = '30',
$hour = '23',
$monthday = '*',
class pe_databases::backup (
Array[Hash] $databases_and_backup_schedule = [
{
'databases' => ['pe-activity', 'pe-classifier', 'pe-postgres', 'pe-rbac', 'pe-orchestrator'],
'schedule' =>
{
'minute' => '30',
'hour' => '22',
},
},
{
'databases' => ['pe-puppetdb'],
'schedule' =>
{
'minute' => '0',
'hour' => '2',
'weekday' => '7',
},
}
],
String $backup_directory = '/opt/puppetlabs/server/data/postgresql/9.4/backups',
String $backup_script_path = '/opt/puppetlabs/pe_databases/scripts/puppet_enterprise_database_backup.sh',
String $backup_logging_directory = '/var/log/puppetlabs/pe_databases_backup',
) {

validate_string($pg_dump_command)
validate_absolute_path($dump_path)
validate_absolute_path($script_directory)
validate_string($minute)
validate_string($hour)
validate_string($monthday)
file { ['/opt/puppetlabs/pe_databases', '/opt/puppetlabs/pe_databases/scripts', $backup_directory ] :
ensure => directory,
}

file { "${script_directory}/dump_${db_name}.sh":
ensure => file,
content => template('pe_databases/backup/db_dump.sh.erb'),
owner => 'pe-postgres',
group => 'pe-postgres',
mode => '0750',
before => Cron["${db_name}_db_dump"],
file { $backup_logging_directory :
ensure => 'directory',
owner => 'pe-postgres',
group => 'pe-postgres',
}

cron { "${db_name}_db_dump":
ensure => present,
command => "${script_directory}/dump_${db_name}.sh",
user => 'pe-postgres',
minute => $minute,
hour => $hour,
monthday => $monthday,
require => File['dump_directory'],
file { 'puppet_enterprise_db_backup_script' :
ensure => file,
owner => 'pe-postgres',
group => 'pe-postgres',
mode => '0750',
path => $backup_script_path,
source => 'puppet:///modules/pe_databases/puppet_enterprise_database_backup.sh',
}

$databases_and_backup_schedule.each | Hash $dbs_and_schedule | {

$databases_to_backup = $dbs_and_schedule['databases']
$db_string = join($databases_to_backup, ' ')

cron { "puppet_enterprise_database_backup_${databases_to_backup}":
ensure => present,
command => "${backup_script_path} -l ${backup_logging_directory} -t ${backup_directory} ${db_string}",
user => 'pe-postgres',
minute => $dbs_and_schedule['schedule']['minute'],
hour => $dbs_and_schedule['schedule']['hour'],
weekday => $dbs_and_schedule['schedule']['weekday'],
require => File['puppet_enterprise_db_backup_script'],
}
}
}
8 changes: 3 additions & 5 deletions manifests/init.pp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class pe_databases (
Array[String] $databases_to_backup = [ 'pe-activity', 'pe-classifier', 'pe-postgres', 'pe-puppetdb', 'pe-rbac', 'pe-orchestrator' ],
Boolean $manage_database_backups = true,
Boolean $manage_database_maintenance = true,
Boolean $manage_postgresql_settings = true,
) {
Expand All @@ -12,9 +12,7 @@
include pe_databases::postgresql_settings
}

if !empty($databases_to_backup) {
$databases_to_backup.each | String $db | {
pe_databases::backup{ $db :}
}
if $manage_database_backups {
include pe_databases::backup
}
}