Skip to content

Commit cb142f4

Browse files
committed
Merge pull request #649 from dveeden/newbackupprovider
Use backup providers
2 parents 3f18713 + 58508b7 commit cb142f4

File tree

6 files changed

+306
-54
lines changed

6 files changed

+306
-54
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,34 @@ An array of two elements to set the backup time. Allows ['23', '5'] or ['3', '4
353353

354354
A script that is executed at when the backup is finished. This could be used to (r)sync the backup to a central store. This script can be either a single line that is directly executed or a number of lines, when supplied as an array. It could also be one or more externally managed (executable) files.
355355

356+
#####`provider`
357+
358+
Set backup implementation
359+
360+
* `mysqldump`
361+
* `mysqlbackup`: MySQL Enterprise Backup
362+
* `xtrabackup`: Percona XtraBackup
363+
364+
####mysql::backup::mysqldump
365+
366+
Implements mysql::server::backup with mysqldump
367+
368+
Backup type: Logical
369+
370+
####mysql::backup::mysqlbackup
371+
372+
Implements mysql::server::backup with MySQL Enterprise Backup from Oracle
373+
374+
Backup type: Physical
375+
376+
For this you need the meb package, which is available in RPM and TAR format from Oracle. For Ubuntu you can use [meb-deb](https://github.com/dveeden/meb-deb) to create a package from an official tarball.
377+
378+
####mysql::backup::xtrabackup
379+
380+
Implements mysql::server::backup with XtraBackup from Percona
381+
382+
Backup type: Physical
383+
356384
####mysql::server::monitor
357385

358386
#####`mysql_monitor_username`

manifests/backup/mysqlbackup.pp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# See README.me for usage.
2+
class mysql::backup::mysqlbackup (
3+
$backupuser,
4+
$backuppassword,
5+
$backupdir,
6+
$backupdirmode = '0700',
7+
$backupdirowner = 'root',
8+
$backupdirgroup = 'root',
9+
$backupcompress = true,
10+
$backuprotate = 30,
11+
$ignore_events = true,
12+
$delete_before_dump = false,
13+
$backupdatabases = [],
14+
$file_per_database = false,
15+
$ensure = 'present',
16+
$time = ['23', '5'],
17+
$postscript = false,
18+
$execpath = '/usr/bin:/usr/sbin:/bin:/sbin',
19+
) {
20+
21+
mysql_user { "${backupuser}@localhost":
22+
ensure => $ensure,
23+
password_hash => mysql_password($backuppassword),
24+
provider => 'mysql',
25+
require => Class['mysql::server::root_password'],
26+
}
27+
28+
package { 'meb':
29+
ensure => $ensure,
30+
}
31+
32+
# http://dev.mysql.com/doc/mysql-enterprise-backup/3.11/en/mysqlbackup.privileges.html
33+
mysql_grant { "${backupuser}@localhost/*.*":
34+
ensure => $ensure,
35+
user => "${backupuser}@localhost",
36+
table => '*.*',
37+
privileges => [ 'RELOAD', 'SUPER', 'REPLICATION CLIENT' ],
38+
require => Mysql_user["${backupuser}@localhost"],
39+
}
40+
41+
mysql_grant { "${backupuser}@localhost/mysql.backup_progress":
42+
ensure => $ensure,
43+
user => "${backupuser}@localhost",
44+
table => 'mysql.backup_progress',
45+
privileges => [ 'CREATE', 'INSERT', 'DROP', 'UPDATE' ],
46+
require => Mysql_user["${backupuser}@localhost"],
47+
}
48+
49+
mysql_grant { "${backupuser}@localhost/mysql.backup_history":
50+
ensure => $ensure,
51+
user => "${backupuser}@localhost",
52+
table => 'mysql.backup_history',
53+
privileges => [ 'CREATE', 'INSERT', 'SELECT', 'DROP', 'UPDATE' ],
54+
require => Mysql_user["${backupuser}@localhost"],
55+
}
56+
57+
cron { 'mysqlbackup-weekly':
58+
ensure => $ensure,
59+
command => 'mysqlbackup backup',
60+
user => 'root',
61+
hour => $time[0],
62+
minute => $time[1],
63+
weekday => 0,
64+
require => Package['meb'],
65+
}
66+
67+
cron { 'mysqlbackup-daily':
68+
ensure => $ensure,
69+
command => 'mysqlbackup --incremental backup',
70+
user => 'root',
71+
hour => $time[0],
72+
minute => $time[1],
73+
weekday => 1-6,
74+
require => Package['meb'],
75+
}
76+
77+
$default_options = {
78+
'mysqlbackup' => {
79+
'backup-dir' => $backupdir,
80+
'with-timestamp' => true,
81+
'incremental_base' => 'history:last_backup',
82+
'incremental_backup_dir' => $backupdir,
83+
'user' => $backupuser,
84+
'password' => $backuppassword,
85+
}
86+
}
87+
$options = mysql_deepmerge($default_options, $override_options)
88+
89+
file { 'mysqlbackup-config-file':
90+
path => '/etc/mysql/conf.d/meb.cnf',
91+
content => template('mysql/meb.cnf.erb'),
92+
mode => '0600',
93+
}
94+
95+
file { 'mysqlbackupdir':
96+
ensure => 'directory',
97+
path => $backupdir,
98+
mode => $backupdirmode,
99+
owner => $backupdirowner,
100+
group => $backupdirgroup,
101+
}
102+
103+
}

manifests/backup/mysqldump.pp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# See README.me for usage.
2+
class mysql::backup::mysqldump (
3+
$backupuser,
4+
$backuppassword,
5+
$backupdir,
6+
$backupdirmode = '0700',
7+
$backupdirowner = 'root',
8+
$backupdirgroup = 'root',
9+
$backupcompress = true,
10+
$backuprotate = 30,
11+
$ignore_events = true,
12+
$delete_before_dump = false,
13+
$backupdatabases = [],
14+
$file_per_database = false,
15+
$ensure = 'present',
16+
$time = ['23', '5'],
17+
$postscript = false,
18+
$execpath = '/usr/bin:/usr/sbin:/bin:/sbin',
19+
) {
20+
21+
mysql_user { "${backupuser}@localhost":
22+
ensure => $ensure,
23+
password_hash => mysql_password($backuppassword),
24+
provider => 'mysql',
25+
require => Class['mysql::server::root_password'],
26+
}
27+
28+
mysql_grant { "${backupuser}@localhost/*.*":
29+
ensure => $ensure,
30+
user => "${backupuser}@localhost",
31+
table => '*.*',
32+
privileges => [ 'SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS' ],
33+
require => Mysql_user["${backupuser}@localhost"],
34+
}
35+
36+
cron { 'mysql-backup':
37+
ensure => $ensure,
38+
command => '/usr/local/sbin/mysqlbackup.sh',
39+
user => 'root',
40+
hour => $time[0],
41+
minute => $time[1],
42+
require => File['mysqlbackup.sh'],
43+
}
44+
45+
file { 'mysqlbackup.sh':
46+
ensure => $ensure,
47+
path => '/usr/local/sbin/mysqlbackup.sh',
48+
mode => '0700',
49+
owner => 'root',
50+
group => 'root',
51+
content => template('mysql/mysqlbackup.sh.erb'),
52+
}
53+
54+
file { 'mysqlbackupdir':
55+
ensure => 'directory',
56+
path => $backupdir,
57+
mode => $backupdirmode,
58+
owner => $backupdirowner,
59+
group => $backupdirgroup,
60+
}
61+
62+
}

manifests/backup/xtrabackup.pp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# See README.me for usage.
2+
class mysql::backup::xtrabackup (
3+
$backupuser,
4+
$backuppassword,
5+
$backupdir,
6+
$backupmethod = 'mysqldump',
7+
$backupdirmode = '0700',
8+
$backupdirowner = 'root',
9+
$backupdirgroup = 'root',
10+
$backupcompress = true,
11+
$backuprotate = 30,
12+
$ignore_events = true,
13+
$delete_before_dump = false,
14+
$backupdatabases = [],
15+
$file_per_database = false,
16+
$ensure = 'present',
17+
$time = ['23', '5'],
18+
$postscript = false,
19+
$execpath = '/usr/bin:/usr/sbin:/bin:/sbin',
20+
) {
21+
22+
mysql_user { "${backupuser}@localhost":
23+
ensure => $ensure,
24+
password_hash => mysql_password($backuppassword),
25+
provider => 'mysql',
26+
require => Class['mysql::server::root_password'],
27+
}
28+
29+
package{ 'percona-xtrabackup':
30+
ensure => $ensure,
31+
}
32+
cron { 'xtrabackup-weekly':
33+
ensure => $ensure,
34+
command => 'innobackupex $backupdir',
35+
user => 'root',
36+
hour => $time[0],
37+
minute => $time[1],
38+
weekday => 0,
39+
require => Package['percona-xtrabackup'],
40+
}
41+
cron { 'xtrabackup-daily':
42+
ensure => $ensure,
43+
command => 'innobackupex --incremental $backupdir',
44+
user => 'root',
45+
hour => $time[0],
46+
minute => $time[1],
47+
weekday => 1-6,
48+
require => Package['percona-xtrabackup'],
49+
}
50+
51+
file { 'mysqlbackupdir':
52+
ensure => 'directory',
53+
path => $backupdir,
54+
mode => $backupdirmode,
55+
owner => $backupdirowner,
56+
group => $backupdirgroup,
57+
}
58+
59+
}

manifests/server/backup.pp

+36-54
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,43 @@
11
# See README.me for usage.
22
class mysql::server::backup (
3-
$backupuser,
4-
$backuppassword,
5-
$backupdir,
6-
$backupdirmode = '0700',
7-
$backupdirowner = 'root',
8-
$backupdirgroup = 'root',
9-
$backupcompress = true,
10-
$backuprotate = 30,
11-
$ignore_events = true,
3+
$backupuser = undef,
4+
$backuppassword = undef,
5+
$backupdir = undef,
6+
$backupdirmode = '0700',
7+
$backupdirowner = 'root',
8+
$backupdirgroup = 'root',
9+
$backupcompress = true,
10+
$backuprotate = 30,
11+
$ignore_events = true,
1212
$delete_before_dump = false,
13-
$backupdatabases = [],
14-
$file_per_database = false,
15-
$ensure = 'present',
16-
$time = ['23', '5'],
17-
$postscript = false,
18-
$execpath = '/usr/bin:/usr/sbin:/bin:/sbin',
13+
$backupdatabases = [],
14+
$file_per_database = false,
15+
$ensure = 'present',
16+
$time = ['23', '5'],
17+
$postscript = false,
18+
$execpath = '/usr/bin:/usr/sbin:/bin:/sbin',
19+
$provider = 'mysqldump',
1920
) {
2021

21-
mysql_user { "${backupuser}@localhost":
22-
ensure => $ensure,
23-
password_hash => mysql_password($backuppassword),
24-
require => Class['mysql::server::root_password'],
25-
}
26-
27-
mysql_grant { "${backupuser}@localhost/*.*":
28-
ensure => $ensure,
29-
user => "${backupuser}@localhost",
30-
table => '*.*',
31-
privileges => [ 'SELECT', 'RELOAD', 'LOCK TABLES', 'SHOW VIEW', 'PROCESS' ],
32-
require => Mysql_user["${backupuser}@localhost"],
33-
}
34-
35-
cron { 'mysql-backup':
36-
ensure => $ensure,
37-
command => '/usr/local/sbin/mysqlbackup.sh',
38-
user => 'root',
39-
hour => $time[0],
40-
minute => $time[1],
41-
require => File['mysqlbackup.sh'],
42-
}
43-
44-
file { 'mysqlbackup.sh':
45-
ensure => $ensure,
46-
path => '/usr/local/sbin/mysqlbackup.sh',
47-
mode => '0700',
48-
owner => 'root',
49-
group => 'root',
50-
content => template('mysql/mysqlbackup.sh.erb'),
51-
}
52-
53-
file { 'mysqlbackupdir':
54-
ensure => 'directory',
55-
path => $backupdir,
56-
mode => $backupdirmode,
57-
owner => $backupdirowner,
58-
group => $backupdirgroup,
59-
}
22+
create_resources("class", {
23+
"mysql::backup::${provider}" => {
24+
'backupuser' => $backupuser,
25+
'backuppassword' => $backuppassword,
26+
'backupdir' => $backupdir,
27+
'backupdirmode' => $backupdirmode,
28+
'backupdirowner' => $backupdirowner,
29+
'backupdirgroup' => $backupdirgroup,
30+
'backupcompress' => $backupcompress,
31+
'backuprotate' => $backuprotate,
32+
'ignore_events' => $ignore_events,
33+
'delete_before_dump' => $delete_before_dump,
34+
'backupdatabases' => $backupdatabases,
35+
'file_per_database' => $file_per_database,
36+
'ensure' => $ensure,
37+
'time' => $time,
38+
'postscript' => $postscript,
39+
'execpath' => $execpath,
40+
}
41+
})
6042

6143
}

templates/meb.cnf.erb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
### MANAGED BY PUPPET ###
2+
3+
<% @options.sort.map do |k,v| -%>
4+
<% if v.is_a?(Hash) -%>
5+
[<%= k %>]
6+
<% v.sort.map do |ki, vi| -%>
7+
<% if vi == true or v == '' -%>
8+
<%= ki %>
9+
<% elsif vi.is_a?(Array) -%>
10+
<% vi.each do |vii| -%>
11+
<%= ki %> = <%= vii %>
12+
<% end -%>
13+
<% elsif ![nil, '', :undef].include?(vi) -%>
14+
<%= ki %> = <%= vi %>
15+
<% end -%>
16+
<% end -%>
17+
<% end %>
18+
<% end -%>

0 commit comments

Comments
 (0)