forked from puppetlabs/puppetlabs-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_server_backup_spec.rb
188 lines (158 loc) · 6.1 KB
/
mysql_server_backup_spec.rb
1
2
3
4
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
require 'spec_helper'
describe 'mysql::server::backup' do
let(:default_params) {
{ 'backupuser' => 'testuser',
'backuppassword' => 'testpass',
'backupdir' => '/tmp',
'backuprotate' => '25',
'delete_before_dump' => true,
'execpath' => '/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin',
}
}
context 'standard conditions' do
let(:params) { default_params }
it { should contain_mysql_user('testuser@localhost').with(
:require => 'Class[Mysql::Server::Root_password]'
)}
it { should contain_mysql_grant('testuser@localhost/*.*').with(
:privileges => ["SELECT", "RELOAD", "LOCK TABLES", "SHOW VIEW", "PROCESS"]
)}
it { should contain_cron('mysql-backup').with(
:command => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
)}
it { should contain_file('mysqlbackup.sh').with(
:path => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
) }
it { should contain_file('mysqlbackupdir').with(
:path => '/tmp',
:ensure => 'directory'
)}
it 'should have compression by default' do
verify_contents(subject, 'mysqlbackup.sh', [
' --all-databases | bzcat -zc > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql.bz2',
])
end
it 'should skip backing up events table by default' do
verify_contents(subject, 'mysqlbackup.sh', [
'EVENTS="--ignore-table=mysql.event"',
])
end
it 'should have 25 days of rotation' do
# MySQL counts from 0 I guess.
should contain_file('mysqlbackup.sh').with_content(/.*ROTATE=24.*/)
end
it 'should have a standard PATH' do
should contain_file('mysqlbackup.sh').with_content(%r{PATH=/usr/bin:/usr/sbin:/bin:/sbin:/opt/zimbra/bin})
end
end
context 'custom ownership and mode for backupdir' do
let(:params) do
{ :backupdirmode => '0750',
:backupdirowner => 'testuser',
:backupdirgroup => 'testgrp',
}.merge(default_params)
end
it { should contain_file('mysqlbackupdir').with(
:path => '/tmp',
:ensure => 'directory',
:mode => '0750',
:owner => 'testuser',
:group => 'testgrp'
) }
end
context 'with compression disabled' do
let(:params) do
{ :backupcompress => false }.merge(default_params)
end
it { should contain_file('mysqlbackup.sh').with(
:path => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
) }
it 'should be able to disable compression' do
verify_contents(subject, 'mysqlbackup.sh', [
' --all-databases > ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql',
])
end
end
context 'with mysql.events backedup' do
let(:params) do
{ :ignore_events => false }.merge(default_params)
end
it { should contain_file('mysqlbackup.sh').with(
:path => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
) }
it 'should be able to backup events table' do
verify_contents(subject, 'mysqlbackup.sh', [
'EVENTS="--events"',
])
end
end
context 'with database list specified' do
let(:params) do
{ :backupdatabases => ['mysql'] }.merge(default_params)
end
it { should contain_file('mysqlbackup.sh').with(
:path => '/usr/local/sbin/mysqlbackup.sh',
:ensure => 'present'
) }
it 'should have a backup file for each database' do
content = subject.resource('file','mysqlbackup.sh').send(:parameters)[:content]
content.should match(' mysql | bzcat -zc \${DIR}\\\${PREFIX}mysql_`date')
# verify_contents(subject, 'mysqlbackup.sh', [
# ' mysql | bzcat -zc ${DIR}/${PREFIX}mysql_`date +%Y%m%d-%H%M%S`.sql',
# ])
end
end
context 'with file per database' do
let(:params) do
default_params.merge({ :file_per_database => true })
end
it 'should loop through backup all databases' do
verify_contents(subject, 'mysqlbackup.sh', [
'mysql -s -r -N -e \'SHOW DATABASES\' | while read dbname',
'do',
' mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \\',
' ${EVENTS} \\',
' ${dbname} | bzcat -zc > ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql.bz2',
'done',
])
end
context 'with compression disabled' do
let(:params) do
default_params.merge({ :file_per_database => true, :backupcompress => false })
end
it 'should loop through backup all databases without compression' do
verify_contents(subject, 'mysqlbackup.sh', [
' ${dbname} > ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql',
])
end
end
end
context 'with postscript' do
let(:params) do
default_params.merge({ :postscript => 'rsync -a /tmp backup01.local-lan:' })
end
it 'should be add postscript' do
verify_contents(subject, 'mysqlbackup.sh', [
'rsync -a /tmp backup01.local-lan:',
])
end
end
context 'with postscripts' do
let(:params) do
default_params.merge({ :postscript => [
'rsync -a /tmp backup01.local-lan:',
'rsync -a /tmp backup02.local-lan:',
]})
end
it 'should be add postscript' do
verify_contents(subject, 'mysqlbackup.sh', [
'rsync -a /tmp backup01.local-lan:',
'rsync -a /tmp backup02.local-lan:',
])
end
end
end