|
| 1 | +# @summary Backup the core user settings for puppet infrastructure |
| 2 | +# |
| 3 | +# This plan can backup data as outlined at insert doc |
| 4 | +# |
| 5 | +plan peadm::backup ( |
| 6 | + # Standard |
| 7 | + Peadm::SingleTargetSpec $primary_host, |
| 8 | + Optional[Peadm::SingleTargetSpec] $replica_host = undef, |
| 9 | + |
| 10 | + # Large |
| 11 | + Optional[TargetSpec] $compiler_hosts = undef, |
| 12 | + |
| 13 | + # Extra Large |
| 14 | + Optional[Peadm::SingleTargetSpec] $primary_postgresql_host = undef, |
| 15 | + Optional[Peadm::SingleTargetSpec] $replica_postgresql_host = undef, |
| 16 | + |
| 17 | + # Which data to backup |
| 18 | + Boolean $backup_orchestrator = true, |
| 19 | + Boolean $backup_rbac = true, |
| 20 | + Boolean $backup_activity = true, |
| 21 | + Boolean $backup_ca_ssl = true, |
| 22 | + Boolean $backup_puppetdb = false, |
| 23 | + Boolean $backup_classification = true, |
| 24 | + String $output_directory = '/tmp', |
| 25 | +){ |
| 26 | + |
| 27 | + $timestamp = Timestamp.new().strftime('%F_%T') |
| 28 | + $backup_directory = "${output_directory}/pe-backup-${timestamp}" |
| 29 | + # Create backup folder |
| 30 | + apply_prep($primary_host) |
| 31 | + apply($primary_host){ |
| 32 | + file { $backup_directory : |
| 33 | + ensure => 'directory', |
| 34 | + owner => 'root', |
| 35 | + group => 'pe-postgres', |
| 36 | + mode => '0770' |
| 37 | + } |
| 38 | + } |
| 39 | + # Create an array of the names of databases and whether they have to be backed up to use in a lambda later |
| 40 | + $database_to_backup = [ $backup_orchestrator, $backup_activity, $backup_rbac, $backup_puppetdb] |
| 41 | + $database_names = [ 'pe-orchestrator' , 'pe-activity' , 'pe-rbac' , 'pe-puppetdb' ] |
| 42 | + |
| 43 | + peadm::assert_supported_bolt_version() |
| 44 | + |
| 45 | + # Ensure input valid for a supported architecture |
| 46 | + $arch = peadm::assert_supported_architecture( |
| 47 | + $primary_host, |
| 48 | + $replica_host, |
| 49 | + $primary_postgresql_host, |
| 50 | + $replica_postgresql_host, |
| 51 | + $compiler_hosts, |
| 52 | + ) |
| 53 | + |
| 54 | + if $backup_classification { |
| 55 | + out::message('# Backing up classification') |
| 56 | + run_task('peadm::backup_classification', $primary_host, |
| 57 | + directory => $backup_directory, |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + if $backup_ca_ssl { |
| 62 | + out::message('# Backing up ca and ssl certificates') |
| 63 | + run_command("/opt/puppetlabs/bin/puppet-backup create --dir=${backup_directory} --scope=certs", $primary_host) |
| 64 | + } |
| 65 | + |
| 66 | + # Check if /etc/puppetlabs/console-services/conf.d/secrets/keys.json exists and if so back it up |
| 67 | + out::message('# Backing up ldap secret key if it exists') |
| 68 | + run_command("test -f /etc/puppetlabs/console-services/conf.d/secrets/keys.json && cp -rp /etc/puppetlabs/console-services/conf.d/secrets/keys.json ${backup_directory} || echo secret ldap key doesnt exist" , $primary_host) # lint:ignore:140chars |
| 69 | + |
| 70 | + # IF backing up orchestrator back up the secrets too /etc/puppetlabs/orchestration-services/conf.d/secrets/ |
| 71 | + if $backup_orchestrator { |
| 72 | + out::message('# Backing up orchestrator secret keys') |
| 73 | + run_command("cp -rp /etc/puppetlabs/orchestration-services/conf.d/secrets ${backup_directory}/", $primary_host) |
| 74 | + } |
| 75 | + |
| 76 | + $database_to_backup.each |Integer $index, Boolean $value | { |
| 77 | + if $value { |
| 78 | + out::message("# Backing up database ${database_names[$index]}") |
| 79 | + # If the primary postgresql host is set then pe-puppetdb needs to be remotely backed up to primary. |
| 80 | + if $database_names[$index] == 'pe-puppetdb' and $primary_postgresql_host { |
| 81 | + run_command("sudo -u pe-puppetdb /opt/puppetlabs/server/bin/pg_dump \"sslmode=verify-ca host=${primary_postgresql_host} sslcert=/etc/puppetlabs/puppetdb/ssl/${primary_host}.cert.pem sslkey=/etc/puppetlabs/puppetdb/ssl/${primary_host}.private_key.pem sslrootcert=/etc/puppetlabs/puppet/ssl/certs/ca.pem dbname=pe-puppetdb\" -f /tmp/puppetdb_$(date +%F_%T).bin" , $primary_host) # lint:ignore:140chars |
| 82 | + } else { |
| 83 | + run_command("sudo -u pe-postgres /opt/puppetlabs/server/bin/pg_dump -Fc \"${database_names[$index]}\" -f \"${backup_directory}/${database_names[$index]}_$(date +%F_%T).bin\"" , $primary_host) # lint:ignore:140chars |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments