Skip to content

Commit f29d95c

Browse files
author
Ashley Penney
committed
Merge pull request #435 from b4ldr/master
Add logic to ignore mysql.events
2 parents 7caebdb + 10f346e commit f29d95c

File tree

3 files changed

+149
-114
lines changed

3 files changed

+149
-114
lines changed

manifests/server/backup.pp

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
$backupdirgroup = 'root',
99
$backupcompress = true,
1010
$backuprotate = 30,
11+
$ignore_events = true,
1112
$delete_before_dump = false,
1213
$backupdatabases = [],
1314
$file_per_database = false,

spec/classes/mysql_server_backup_spec.rb

+137-113
Original file line numberDiff line numberDiff line change
@@ -2,130 +2,154 @@
22

33
describe 'mysql::server::backup' do
44

5-
let(:default_params) {
6-
{ 'backupuser' => 'testuser',
7-
'backuppassword' => 'testpass',
8-
'backupdir' => '/tmp',
9-
'backuprotate' => '25',
10-
'delete_before_dump' => true,
5+
let(:default_params) {
6+
{ 'backupuser' => 'testuser',
7+
'backuppassword' => 'testpass',
8+
'backupdir' => '/tmp',
9+
'backuprotate' => '25',
10+
'delete_before_dump' => true,
11+
}
1112
}
12-
}
13-
context 'standard conditions' do
14-
let(:params) { default_params }
15-
16-
it { should contain_mysql_user('testuser@localhost').with(
17-
:require => 'Class[Mysql::Server::Root_password]'
18-
)}
19-
20-
it { should contain_mysql_grant('testuser@localhost/*.*').with(
21-
:privileges => ["SELECT", "RELOAD", "LOCK TABLES", "SHOW VIEW"]
22-
)}
23-
24-
it { should contain_cron('mysql-backup').with(
25-
:command => '/usr/local/sbin/mysqlbackup.sh',
26-
:ensure => 'present'
27-
)}
28-
29-
it { should contain_file('mysqlbackup.sh').with(
30-
:path => '/usr/local/sbin/mysqlbackup.sh',
31-
:ensure => 'present'
32-
) }
33-
34-
it { should contain_file('mysqlbackupdir').with(
35-
:path => '/tmp',
36-
:ensure => 'directory'
37-
)}
38-
39-
it 'should have compression by default' do
40-
verify_contents(subject, 'mysqlbackup.sh', [
41-
' --all-databases | bzcat -zc > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql.bz2',
42-
])
13+
context 'standard conditions' do
14+
let(:params) { default_params }
15+
16+
it { should contain_mysql_user('testuser@localhost').with(
17+
:require => 'Class[Mysql::Server::Root_password]'
18+
)}
19+
20+
it { should contain_mysql_grant('testuser@localhost/*.*').with(
21+
:privileges => ["SELECT", "RELOAD", "LOCK TABLES", "SHOW VIEW"]
22+
)}
23+
24+
it { should contain_cron('mysql-backup').with(
25+
:command => '/usr/local/sbin/mysqlbackup.sh',
26+
:ensure => 'present'
27+
)}
28+
29+
it { should contain_file('mysqlbackup.sh').with(
30+
:path => '/usr/local/sbin/mysqlbackup.sh',
31+
:ensure => 'present'
32+
) }
33+
34+
it { should contain_file('mysqlbackupdir').with(
35+
:path => '/tmp',
36+
:ensure => 'directory'
37+
)}
38+
39+
it 'should have compression by default' do
40+
verify_contents(subject, 'mysqlbackup.sh', [
41+
' --all-databases | bzcat -zc > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql.bz2',
42+
])
43+
end
44+
it 'should skip backing up events table by default' do
45+
verify_contents(subject, 'mysqlbackup.sh', [
46+
'EVENTS="--ignore-table=mysql.event"',
47+
])
48+
end
49+
50+
it 'should have 25 days of rotation' do
51+
# MySQL counts from 0 I guess.
52+
should contain_file('mysqlbackup.sh').with_content(/.*ROTATE=24.*/)
53+
end
4354
end
4455

45-
it 'should have 25 days of rotation' do
46-
# MySQL counts from 0 I guess.
47-
should contain_file('mysqlbackup.sh').with_content(/.*ROTATE=24.*/)
56+
context 'custom ownership and mode for backupdir' do
57+
let(:params) do
58+
{ :backupdirmode => '0750',
59+
:backupdirowner => 'testuser',
60+
:backupdirgroup => 'testgrp',
61+
}.merge(default_params)
62+
end
63+
64+
it { should contain_file('mysqlbackupdir').with(
65+
:path => '/tmp',
66+
:ensure => 'directory',
67+
:mode => '0750',
68+
:owner => 'testuser',
69+
:group => 'testgrp'
70+
) }
4871
end
49-
end
50-
51-
context 'custom ownership and mode for backupdir' do
52-
let(:params) do
53-
{ :backupdirmode => '0750',
54-
:backupdirowner => 'testuser',
55-
:backupdirgroup => 'testgrp',
56-
}.merge(default_params)
72+
73+
context 'with compression disabled' do
74+
let(:params) do
75+
{ :backupcompress => false }.merge(default_params)
76+
end
77+
78+
it { should contain_file('mysqlbackup.sh').with(
79+
:path => '/usr/local/sbin/mysqlbackup.sh',
80+
:ensure => 'present'
81+
) }
82+
83+
it 'should be able to disable compression' do
84+
verify_contents(subject, 'mysqlbackup.sh', [
85+
' --all-databases > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql',
86+
])
87+
end
5788
end
5889

59-
it { should contain_file('mysqlbackupdir').with(
60-
:path => '/tmp',
61-
:ensure => 'directory',
62-
:mode => '0750',
63-
:owner => 'testuser',
64-
:group => 'testgrp'
65-
) }
66-
end
67-
68-
context 'with compression disabled' do
69-
let(:params) do
70-
{ :backupcompress => false }.merge(default_params)
90+
context 'with mysql.events backedup' do
91+
let(:params) do
92+
{ :ignore_events => false }.merge(default_params)
93+
end
94+
95+
it { should contain_file('mysqlbackup.sh').with(
96+
:path => '/usr/local/sbin/mysqlbackup.sh',
97+
:ensure => 'present'
98+
) }
99+
100+
it 'should be able to backup events table' do
101+
verify_contents(subject, 'mysqlbackup.sh', [
102+
'EVENTS="--events"',
103+
])
104+
end
71105
end
72106

73-
it { should contain_file('mysqlbackup.sh').with(
74-
:path => '/usr/local/sbin/mysqlbackup.sh',
75-
:ensure => 'present'
76-
) }
77107

78-
it 'should be able to disable compression' do
79-
verify_contents(subject, 'mysqlbackup.sh', [
80-
' --all-databases > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql',
81-
])
82-
end
83-
end
108+
context 'with database list specified' do
109+
let(:params) do
110+
{ :backupdatabases => ['mysql'] }.merge(default_params)
111+
end
84112

85-
context 'with database list specified' do
86-
let(:params) do
87-
{ :backupdatabases => ['mysql'] }.merge(default_params)
88-
end
113+
it { should contain_file('mysqlbackup.sh').with(
114+
:path => '/usr/local/sbin/mysqlbackup.sh',
115+
:ensure => 'present'
116+
) }
89117

90-
it { should contain_file('mysqlbackup.sh').with(
91-
:path => '/usr/local/sbin/mysqlbackup.sh',
92-
:ensure => 'present'
93-
) }
94-
95-
it 'should have a backup file for each database' do
96-
content = subject.resource('file','mysqlbackup.sh').send(:parameters)[:content]
97-
content.should match(' mysql | bzcat -zc \${DIR}\\\${PREFIX}mysql_`date')
98-
# verify_contents(subject, 'mysqlbackup.sh', [
99-
# ' mysql | bzcat -zc ${DIR}/${PREFIX}mysql_`date +%Y%m%d-%H%M%S`.sql',
100-
# ])
101-
end
102-
end
103-
104-
context 'with file per database' do
105-
let(:params) do
106-
default_params.merge({ :file_per_database => true })
118+
it 'should have a backup file for each database' do
119+
content = subject.resource('file','mysqlbackup.sh').send(:parameters)[:content]
120+
content.should match(' mysql | bzcat -zc \${DIR}\\\${PREFIX}mysql_`date')
121+
# verify_contents(subject, 'mysqlbackup.sh', [
122+
# ' mysql | bzcat -zc ${DIR}/${PREFIX}mysql_`date +%Y%m%d-%H%M%S`.sql',
123+
# ])
124+
end
107125
end
108-
109-
it 'should loop through backup all databases' do
110-
verify_contents(subject, 'mysqlbackup.sh', [
111-
'mysql -s -r -N -e \'SHOW DATABASES\' | while read dbname',
112-
'do',
113-
' mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \\',
114-
' ${dbname} | bzcat -zc > ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql.bz2',
115-
'done',
116-
])
117-
end
118-
119-
context 'with compression disabled' do
120-
let(:params) do
121-
default_params.merge({ :file_per_database => true, :backupcompress => false })
122-
end
123-
124-
it 'should loop through backup all databases without compression' do
125-
verify_contents(subject, 'mysqlbackup.sh', [
126-
' ${dbname} > ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql',
127-
])
128-
end
126+
127+
context 'with file per database' do
128+
let(:params) do
129+
default_params.merge({ :file_per_database => true })
130+
end
131+
132+
it 'should loop through backup all databases' do
133+
verify_contents(subject, 'mysqlbackup.sh', [
134+
'mysql -s -r -N -e \'SHOW DATABASES\' | while read dbname',
135+
'do',
136+
' mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \\',
137+
' ${EVENTS} \\',
138+
' ${dbname} | bzcat -zc > ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql.bz2',
139+
'done',
140+
])
141+
end
142+
143+
context 'with compression disabled' do
144+
let(:params) do
145+
default_params.merge({ :file_per_database => true, :backupcompress => false })
146+
end
147+
148+
it 'should loop through backup all databases without compression' do
149+
verify_contents(subject, 'mysqlbackup.sh', [
150+
' ${dbname} > ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql',
151+
])
152+
end
153+
end
129154
end
130-
end
131155
end

templates/mysqlbackup.sh.erb

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ DIR=<%= @backupdir %>
1616
ROTATE=<%= [ Integer(@backuprotate) - 1, 0 ].max %>
1717

1818
PREFIX=mysql_backup_
19+
<% if @ignore_events %>
20+
EVENTS="--ignore-table=mysql.event"
21+
<% else %>
22+
EVENTS="--events"
23+
<% end %>
1924

2025
##### STOP CONFIG ####################################################
2126
PATH=/usr/bin:/usr/sbin:/bin:/sbin
2227

28+
29+
2330
set -o pipefail
2431

2532
cleanup()
@@ -36,15 +43,18 @@ cleanup
3643
mysql -s -r -N -e 'SHOW DATABASES' | while read dbname
3744
do
3845
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
46+
${EVENTS} \
3947
${dbname} <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
4048
done
4149
<% else -%>
4250
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
51+
${EVENTS} \
4352
--all-databases <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
4453
<% end -%>
4554
<% else -%>
4655
<% @backupdatabases.each do |db| -%>
47-
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
56+
mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \
57+
${EVENTS} \
4858
<%= db %><% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}<%= db %>_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
4959
<% end -%>
5060
<% end -%>

0 commit comments

Comments
 (0)