diff --git a/files/puppet_enterprise_database_backup.sh b/files/puppet_enterprise_database_backup.sh new file mode 100755 index 0000000..850773a --- /dev/null +++ b/files/puppet_enterprise_database_backup.sh @@ -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] [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 diff --git a/manifests/backup.pp b/manifests/backup.pp index a7d41c7..ce65177 100644 --- a/manifests/backup.pp +++ b/manifests/backup.pp @@ -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'], + } + } } diff --git a/manifests/init.pp b/manifests/init.pp index e2015ef..1a52959 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -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, ) { @@ -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 } }