|
| 1 | +# @summary |
| 2 | +# "Provider" for pg_dump backup |
| 3 | +# |
| 4 | +# @api private |
| 5 | +# |
| 6 | +# @param compress |
| 7 | +# Whether or not to compress the backup. Support for compression also depends on other backup parameters. |
| 8 | +# @param databases |
| 9 | +# Databases to backup. By default `[]` will back up all databases. |
| 10 | +# @param db_user |
| 11 | +# PostgreSQL user to create with superuser privileges. |
| 12 | +# @param db_password |
| 13 | +# Password to create for `$db_user`. |
| 14 | +# @param dir |
| 15 | +# Directory to store backup. |
| 16 | +# @param dir_mode |
| 17 | +# Permissions applied to the backup directory. This parameter is passed directly to the file resource. |
| 18 | +# @param dir_owner |
| 19 | +# Owner for the backup directory. This parameter is passed directly to the file resource. |
| 20 | +# @param dir_group |
| 21 | +# Group owner for the backup directory. This parameter is passed directly to the file resource. |
| 22 | +# @param format |
| 23 | +# Backup format to use, must be supported by pg_dump or pg_dumpall. The choice will affect other options, i.e. compression. |
| 24 | +# @param install_cron |
| 25 | +# Manage installation of cron package. |
| 26 | +# @param manage_user |
| 27 | +# Manage creation of the backup user. |
| 28 | +# @param optional_args |
| 29 | +# Specifies an array of optional arguments which should be passed through to the backup tool. These options are not validated, unsupported options may break the backup. |
| 30 | +# @param postscript |
| 31 | +# One or more scripts that are executed when the backup is finished. This could be used to sync the backup to a central store. |
| 32 | +# @param prescript |
| 33 | +# One or more scripts that are executed before the backup begins. |
| 34 | +# @param rotate |
| 35 | +# Backup rotation interval in 24 hour periods. |
| 36 | +# @param success_file_path |
| 37 | +# Specify a path where upon successful backup a file should be created for checking purposes. |
| 38 | +# @param time |
| 39 | +# An array of two elements to set the backup time. Allows `['23', '5']` (i.e., 23:05) or `['3', '45']` (i.e., 03:45) for HH:MM times. |
| 40 | +# @param weekday |
| 41 | +# Weekdays on which the backup job should run. Defaults to `*`. This parameter is passed directly to the cron resource. |
| 42 | +# |
| 43 | +class postgresql::backup::pg_dump ( |
| 44 | + Boolean $compress = true, |
| 45 | + Array $databases = [], |
| 46 | + Boolean $delete_before_dump = false, |
| 47 | + String[1] $dir, |
| 48 | + String[1] $dir_group = '0', |
| 49 | + String[1] $dir_mode = '0700', |
| 50 | + String[1] $dir_owner = 'root', |
| 51 | + Enum['present','absent'] $ensure = 'present', |
| 52 | + Enum['plain','custom','directory','tar'] $format = 'plain', |
| 53 | + Boolean $install_cron = true, |
| 54 | + Boolean $manage_user = false, |
| 55 | + Array $optional_args = [], |
| 56 | + Stdlib::Absolutepath $pgpass_path = '/root/.pgpass', |
| 57 | + Integer $rotate = 30, |
| 58 | + Stdlib::Absolutepath $script_path = '/usr/local/sbin/pg_dump.sh', |
| 59 | + Stdlib::Absolutepath $success_file_path = '/tmp/pgbackup_success', |
| 60 | + String[1] $template = 'postgresql/pg_dump.sh.epp', |
| 61 | + Array $time = ['23', '5'], |
| 62 | + String[1] $weekday = '*', |
| 63 | + Optional[Variant[String, Sensitive[String]]] $db_password = undef, |
| 64 | + Optional[String[1]] $db_user = undef, |
| 65 | + Optional[String[1]] $package_name = undef, |
| 66 | + Optional[String[1]] $post_script = undef, |
| 67 | + Optional[String[1]] $pre_script = undef, |
| 68 | +) { |
| 69 | + # Install required packages |
| 70 | + if $package_name { |
| 71 | + ensure_packages($package_name) |
| 72 | + } |
| 73 | + if $install_cron { |
| 74 | + if $facts['os']['family'] == 'RedHat' { |
| 75 | + ensure_packages('cronie') |
| 76 | + } elsif $facts['os']['family'] != 'FreeBSD' { |
| 77 | + ensure_packages('cron') |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + # Setup db user with required permissions |
| 82 | + if $manage_user and $db_user and $db_password { |
| 83 | + # Create user with superuser privileges |
| 84 | + postgresql::server::role { $db_user: |
| 85 | + ensure => $ensure, |
| 86 | + password_hash => postgresql::postgresql_password($db_user, $db_password), |
| 87 | + superuser => true, |
| 88 | + } |
| 89 | + |
| 90 | + # Allow authentication from localhost |
| 91 | + postgresql::server::pg_hba_rule { 'local access as backup user': |
| 92 | + type => 'local', |
| 93 | + database => 'all', |
| 94 | + user => $db_user, |
| 95 | + auth_method => 'md5', |
| 96 | + order => 1, |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + # Create backup directory |
| 101 | + file { $dir: |
| 102 | + ensure => 'directory', |
| 103 | + mode => $dir_mode, |
| 104 | + owner => $dir_owner, |
| 105 | + group => $dir_group, |
| 106 | + } |
| 107 | + |
| 108 | + # Create backup script |
| 109 | + file { $script_path: |
| 110 | + ensure => $ensure, |
| 111 | + mode => '0700', |
| 112 | + owner => 'root', |
| 113 | + group => '0', # Use GID for compat with Linux and BSD. |
| 114 | + content => epp($template, { |
| 115 | + compress => $compress, |
| 116 | + databases => $databases, |
| 117 | + db_user => $db_user, |
| 118 | + delete_before_dump => $delete_before_dump, |
| 119 | + dir => $dir, |
| 120 | + format => $format, |
| 121 | + optional_args => $optional_args, |
| 122 | + post_script => $post_script, |
| 123 | + pre_script => $pre_script, |
| 124 | + rotate => $rotate, |
| 125 | + success_file_path => $success_file_path, |
| 126 | + }), |
| 127 | + } |
| 128 | + |
| 129 | + # Create password file for pg_dump |
| 130 | + file { $pgpass_path: |
| 131 | + ensure => $ensure, |
| 132 | + mode => '0600', |
| 133 | + owner => 'root', |
| 134 | + group => '0', # Use GID for compat with Linux and BSD. |
| 135 | + content => inline_epp('*:*:*:<%= $db_user %>:<%= $db_password %>',{ |
| 136 | + db_password => $db_password, |
| 137 | + db_user => $db_user, |
| 138 | + }), |
| 139 | + show_diff => false, |
| 140 | + } |
| 141 | + |
| 142 | + # Create cron job |
| 143 | + cron { 'pg_dump backup job': |
| 144 | + ensure => $ensure, |
| 145 | + command => $script_path, |
| 146 | + user => 'root', |
| 147 | + hour => $time[0], |
| 148 | + minute => $time[1], |
| 149 | + weekday => $weekday, |
| 150 | + } |
| 151 | +} |
0 commit comments