Puppet Class: xtrabackup

Defined in:
manifests/init.pp

Overview

Class: xtrabackup

setup xtrabackup crons

Parameters:

  • rsync_opts (Any) (defaults to: '-aqzP --bwlimit=256 -e "ssh"')
  • backup_server (Any) (defaults to: "backup.${::domain}")
  • type (Any) (defaults to: 'incremental')
  • master_name (Any) (defaults to: $::domain)
  • encrypt (Any) (defaults to: false)
  • gpg_pub_key (Any) (defaults to: undef)
  • recipient (Any) (defaults to: "root@${::domain}")
  • checkpoint_dir (Any) (defaults to: '/srv/xtrabackup_checkpoints')
  • base_dir (Any) (defaults to: '/srv/xtrabackup')
  • remote_base_dir (Any) (defaults to: '/srv/xtrabackup_remote')
  • archive_dir (Any) (defaults to: '/srv/xtrabackup_archive')
  • inc_hours (Any) (defaults to: hiera_array('xtrabackup::inc_hours', []))
  • diff_hours (Any) (defaults to: hiera_array('xtrabackup::diff_hours', []))
  • full_hours (Any) (defaults to: hiera_array('xtrbackup::full_hours', []))
  • inc_days (Any) (defaults to: hiera_array('xtrabackup::inc_days', ['*']))
  • diff_days (Any) (defaults to: hiera_array('xtrabackup::diff_days', ['*']))
  • full_days (Any) (defaults to: hiera_array('xtrbackup::full_days', ['*']))
  • full_keep (Any) (defaults to: 0)
  • inc_keep (Any) (defaults to: 0)
  • diff_keep (Any) (defaults to: 0)
  • remote_hours (Any) (defaults to: hiera_array('xtrabackup::remote_hours', []))
  • remote_days (Any) (defaults to: hiera_array('xtrabackup::remote_days', ['*']))


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'manifests/init.pp', line 5

class xtrabackup (
  $rsync_opts = '-aqzP --bwlimit=256 -e "ssh"',
  $backup_server = "backup.${::domain}",
  $type = 'incremental',
  $master_name = $::domain,
  $encrypt = false,
  $gpg_pub_key = undef,
  $recipient = "root@${::domain}",
  $checkpoint_dir = '/srv/xtrabackup_checkpoints',
  $base_dir = '/srv/xtrabackup',
  $remote_base_dir = '/srv/xtrabackup_remote',
  $archive_dir = '/srv/xtrabackup_archive',
  $inc_hours = hiera_array('xtrabackup::inc_hours', []),
  $diff_hours = hiera_array('xtrabackup::diff_hours', []),
  $full_hours = hiera_array('xtrbackup::full_hours', []),
  $inc_days = hiera_array('xtrabackup::inc_days', ['*']),
  $diff_days = hiera_array('xtrabackup::diff_days', ['*']),
  $full_days = hiera_array('xtrbackup::full_days', ['*']),
  $full_keep = 0,
  $inc_keep = 0,
  $diff_keep = 0,
  $remote_hours = hiera_array('xtrabackup::remote_hours', []),
  $remote_days = hiera_array('xtrabackup::remote_days', ['*']),
) {
  validate_re($type, '^incremental|differential|both$')
  if $encrypt {
    warn('Please ensure gpg key is imported and trusted otherwise backups will fail')
  }

  $packages = ['percona-xtrabackup','gnupg2']
  if (! defined(Package['percona-xtrabackup']) ) {
    package { 'percona-xtrabackup':
      ensure => installed,
    }
  }
  if (! defined(Package['gnupg2']) ) {
    package { 'gnupg2':
      ensure => installed,
    }
  }

  file { [$archive_dir,$checkpoint_dir]:
    ensure => directory,
    owner  => 'root',
    group  => 'root',
    mode   => '0700',
  }
  $year = strftime('%Y')
  $month = strftime('%m')
  $day = strftime('%d')
  $today_dir = "${archive_dir}/${year}/${month}/${day}"

  $date_cmd = '`date +"\%F_\%R"`'
  $year_cmd = '`date +"\%Y"`'
  $month_cmd = '`date +"\%m"`'
  $day_cmd = '`date +"\%d"`'
  $backup_cmd = 'innobackupex --stream=xbstream '
  $archive_cmd = $encrypt ? { true => " gpg --batch --yes --no-tty -e -r ${recipient} -o ", false => ' gzip - > ' }
  $extension = $encrypt ? { true => 'xbstream.gz.gpg', false => 'xbstream.gz' }

  $full_backup = "mkdir -p ${archive_dir}/full/${year_cmd}/${month_cmd}/${day_cmd} && ${backup_cmd} --extra-lsndir=${checkpoint_dir} /tmp | ${archive_cmd} ${archive_dir}/full/${year_cmd}/${month_cmd}/${day_cmd}/${master_name}_${date_cmd}_full.${extension}"
  $inc_backup = "mkdir -p ${archive_dir}/incremental/${year_cmd}/${month_cmd}/${day_cmd} && ${backup_cmd} --extra-lsndir=${checkpoint_dir} --incremental --incremental-basedir=${checkpoint_dir} /tmp | ${archive_cmd} ${archive_dir}/incremental/${year_cmd}/${month_cmd}/${day_cmd}/${master_name}_${date_cmd}_incremental.${extension}"
  $diff_backup = "mkdir -p ${archive_dir}/diffierential/${year_cmd}/${month_cmd}/${day_cmd} && ${backup_cmd} --incremental --incremental-basedir=${checkpoint_dir} /tmp | ${archive_cmd} ${archive_dir}/differential/${year_cmd}/${month_cmd}/${day_cmd}/${master_name}_${date_cmd}_differntial.${extension}"

  if ( empty($full_hours) ) {
    warn('not setting full backup cron, full_hours in empty')
    cron {"xtrabackup ${master_name} full backup":
      ensure  => absent,
      command => $full_backup,
      user    => root,
      hour    => $full_hours,
      minute  => 0,
      weekday => $full_days,
    }
  } else {
    # full backup cron
    cron {"xtrabackup ${master_name} full backup":
      command => $full_backup,
      user    => root,
      hour    => $full_hours,
      minute  => 0,
      weekday => $full_days,
    }
    if $type =~ /^(incremental|both)$/ {
      # incremental backup cron
      if ( empty($inc_hours) ) {
        err('not setting incremental cron, inc_hours in empty')
      } else {
        cron {"xtrabackup ${master_name} incremental backup":
          command => $inc_backup,
          user    => root,
          hour    => $inc_hours,
          minute  => 0,
          weekday => $inc_days,
        }
      }
    } else {
      cron {"xtrabackup ${master_name} incremental backup":
        ensure  => absent,
        command => $inc_backup,
        user    => root,
        hour    => $inc_hours,
        minute  => 0,
        weekday => $inc_days,
      }
    }
    if $type =~ /^(differential|both)$/ {
      # differential backup cron
      if ( empty($diff_hours) ) {
        err('not setting differential cron, diff_hours in empty')
      } else {
        cron {"xtrabackup ${master_name} differential backup":
          command => $diff_backup,
          user    => root,
          hour    => $diff_hours,
          minute  => 0,
          weekday => $diff_days,
        }
      }
    } else {
      cron {"xtrabackup ${master_name} differential backup":
        ensure  => absent,
        command => $diff_backup,
        user    => root,
        hour    => $diff_hours,
        minute  => 0,
        weekday => $diff_days,
      }
    }


    if ( empty($remote_hours) ) {
      err('disabling remote backup cron, remote_hours is empty')
      @@cron { "xtrabackup_${::fqdn}_${master_name}":
        ensure  => absent,
        command => "rsync ${xtrabackup::rsync_opts} ${::fqdn}:${archive_dir}/ ${remote_base_dir}/${master_name}/",
        user    => root,
        hour    => $remote_hours,
        minute  => 10,
        weekday => $remote_days,
        tag     => "xtrabackup_${xtrabackup::backup_server}",
      }
    } else {
      # exported cron to rsync to backup server
      @@cron { "xtrabackup_${::fqdn}_${master_name}":
        command => "rsync ${xtrabackup::rsync_opts} ${::fqdn}:${archive_dir}/ ${remote_base_dir}/${master_name}/",
        user    => root,
        hour    => $remote_hours,
        minute  => 10,
        weekday => $remote_days,
        tag     => "xtrabackup_${xtrabackup::backup_server}",
      }
    }

    if $full_keep != 0 {
      ## tidy up old backups
      tidy { 'xtrabackup_cleanup_full':
        path    => "${archive_dir}/full",
        recurse => true,
        rmdirs  => true,
        matches => "${master_name}_*_full.${extension}",
        type    => mtime,
        age     => $full_keep,
        backup  => false,
      }
    }

    if $inc_keep != 0 {
      ## tidy up old incremental backups
      tidy { 'xtrabackup_cleanup_inc':
        path    => "${archive_dir}/incremental",
        recurse => true,
        rmdirs  => true,
        matches => "${master_name}_*_incremental.${extension}",
        type    => mtime,
        age     => $inc_keep,
        backup  => false,
      }
    }
    if $diff_keep != 0 {
      ## tidy up old differential backups
      tidy { 'xtrabackup_cleanup_diff':
        path    => "${archive_dir}/differential",
        recurse => true,
        rmdirs  => true,
        matches => "${master_name}_*_differential.${extension}",
        type    => mtime,
        age     => $diff_keep,
        backup  => false,
      }
    }

  }

}