|
| 1 | +# @api private |
| 2 | +# @summary Backup the core user settings for puppet infrastructure |
| 3 | +# |
| 4 | +# This plan can backup data as outlined at insert doc |
| 5 | +# |
| 6 | +plan peadm::backup ( |
| 7 | + # This plan should be run on the primary server |
| 8 | + Peadm::SingleTargetSpec $targets, |
| 9 | + |
| 10 | + # Which data to backup |
| 11 | + Peadm::Recovery_opts $backup = {}, |
| 12 | + |
| 13 | + # Where to put the backup folder |
| 14 | + String $output_directory = '/tmp', |
| 15 | +) { |
| 16 | + peadm::assert_supported_bolt_version() |
| 17 | + |
| 18 | + $recovery_opts = (peadm::recovery_opts_default() + $backup) |
| 19 | + $cluster = run_task('peadm::get_peadm_config', $targets).first.value |
| 20 | + $arch = peadm::assert_supported_architecture( |
| 21 | + getvar('cluster.params.primary_host'), |
| 22 | + getvar('cluster.params.replica_host'), |
| 23 | + getvar('cluster.params.primary_postgresql_host'), |
| 24 | + getvar('cluster.params.replica_postgresql_host'), |
| 25 | + getvar('cluster.params.compiler_hosts'), |
| 26 | + ) |
| 27 | + |
| 28 | + $timestamp = Timestamp.new().strftime('%Y-%m-%dT%H%M%SZ') |
| 29 | + $backup_directory = "${output_directory}/pe-backup-${timestamp}" |
| 30 | + |
| 31 | + $primary_target = getvar('cluster.params.primary_host') |
| 32 | + $puppetdb_postgresql_target = getvar('cluster.params.primary_postgresql_host') ? { |
| 33 | + undef => getvar('cluster.params.primary_host'), |
| 34 | + default => getvar('cluster.params.primary_postgresql_host'), |
| 35 | + } |
| 36 | + |
| 37 | + $backup_databases = { |
| 38 | + 'orchestrator' => $primary_target, |
| 39 | + 'activity' => $primary_target, |
| 40 | + 'rbac' => $primary_target, |
| 41 | + 'puppetdb' => $puppetdb_postgresql_target, |
| 42 | + }.filter |$key,$_| { |
| 43 | + $recovery_opts[$key] == true |
| 44 | + } |
| 45 | + |
| 46 | + # Create backup folders |
| 47 | + apply($primary_target) { |
| 48 | + file { $backup_directory : |
| 49 | + ensure => 'directory', |
| 50 | + owner => 'root', |
| 51 | + group => 'root', |
| 52 | + mode => '0700', |
| 53 | + } |
| 54 | + |
| 55 | + # Create a subdir for each backup type selected |
| 56 | + $recovery_opts.filter |$_,$val| { $val == true }.each |$dir,$_| { |
| 57 | + file { "${backup_directory}/${dir}": |
| 58 | + ensure => 'directory', |
| 59 | + owner => 'root', |
| 60 | + group => 'root', |
| 61 | + mode => '0700', |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if getvar('recovery_opts.classifier') { |
| 67 | + out::message('# Backing up classification') |
| 68 | + run_task('peadm::backup_classification', $primary_target, |
| 69 | + directory => "${backup_directory}/classifier", |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + if getvar('recovery_opts.ca') { |
| 74 | + out::message('# Backing up ca and ssl certificates') |
| 75 | +# lint:ignore:strict_indent |
| 76 | + run_command(@("CMD"), $primary_target) |
| 77 | + /opt/puppetlabs/bin/puppet-backup create --dir=${shellquote($backup_directory)}/ca --scope=certs |
| 78 | + | CMD |
| 79 | + } |
| 80 | + |
| 81 | + # Check if /etc/puppetlabs/console-services/conf.d/secrets/keys.json exists and if so back it up |
| 82 | + if getvar('recovery_opts.rbac') { |
| 83 | + out::message('# Backing up ldap secret key if it exists') |
| 84 | +# lint:ignore:140chars |
| 85 | + run_command(@("CMD"/L), $primary_target) |
| 86 | + test -f /etc/puppetlabs/console-services/conf.d/secrets/keys.json \ |
| 87 | + && cp -rp /etc/puppetlabs/console-services/conf.d/secrets ${shellquote($backup_directory)}/rbac/ \ |
| 88 | + || echo secret ldap key doesnt exist |
| 89 | + | CMD |
| 90 | +# lint:endignore |
| 91 | + } |
| 92 | +# lint:ignore:140chars |
| 93 | + # IF backing up orchestrator back up the secrets too /etc/puppetlabs/orchestration-services/conf.d/secrets/ |
| 94 | + if getvar('recovery_opts.orchestrator') { |
| 95 | + out::message('# Backing up orchestrator secret keys') |
| 96 | + run_command(@("CMD"), $primary_target) |
| 97 | + cp -rp /etc/puppetlabs/orchestration-services/conf.d/secrets ${shellquote($backup_directory)}/orchestrator/ |
| 98 | + | CMD |
| 99 | + } |
| 100 | +# lint:endignore |
| 101 | + $backup_databases.each |$name,$database_target| { |
| 102 | + run_command(@("CMD"/L), $primary_target) |
| 103 | + /opt/puppetlabs/server/bin/pg_dump -Fd -Z3 -j4 \ |
| 104 | + -f ${shellquote($backup_directory)}/${shellquote($name)}/pe-${shellquote($name)}.dump.d \ |
| 105 | + "sslmode=verify-ca \ |
| 106 | + host=${shellquote($database_target.peadm::certname())} \ |
| 107 | + user=pe-${shellquote($name)} \ |
| 108 | + sslcert=/etc/puppetlabs/puppetdb/ssl/${shellquote($primary_target.peadm::certname())}.cert.pem \ |
| 109 | + sslkey=/etc/puppetlabs/puppetdb/ssl/${shellquote($primary_target.peadm::certname())}.private_key.pem \ |
| 110 | + sslrootcert=/etc/puppetlabs/puppet/ssl/certs/ca.pem \ |
| 111 | + dbname=pe-${shellquote($name)}" |
| 112 | + | CMD |
| 113 | + } |
| 114 | + |
| 115 | + run_command(@("CMD"/L), $primary_target) |
| 116 | + umask 0077 \ |
| 117 | + && cd ${shellquote(dirname($backup_directory))} \ |
| 118 | + && tar -czf ${shellquote($backup_directory)}.tar.gz ${shellquote(basename($backup_directory))} \ |
| 119 | + && rm -rf ${shellquote($backup_directory)} |
| 120 | + | CMD |
| 121 | +# lint:endignore |
| 122 | + return({ 'path' => "${backup_directory}.tar.gz" }) |
| 123 | +} |
0 commit comments