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