diff --git a/manifests/backup.pp b/manifests/backup.pp index e51ad80..f7aeb37 100644 --- a/manifests/backup.pp +++ b/manifests/backup.pp @@ -3,11 +3,12 @@ # This module handles ... # # Parameters: -# [*backupuser*] - The name of the mysql backup user. -# [*backuppassword*] - The password of the mysql backup user. -# [*backupdir*] - The target directory of the mysqldump. -# [*backupcompress*] - Boolean to compress backup with bzip2. -# [*backuprotate*] - Number of backups to keep. Default 30 +# [*backupuser*] - The name of the mysql backup user. +# [*backuppassword*] - The password of the mysql backup user. +# [*backupdir*] - The target directory of the mysqldump. +# [*backupcompress*] - Boolean to compress backup with bzip2. +# [*backuprotate*] - Number of backups to keep. Default 30 +# [*backupdatabases*] - Specify databases to back up as array (default all) # [*delete_before_dump*] - Clean existing backups before creating new # # Actions: @@ -32,6 +33,7 @@ class mysql::backup ( $backupcompress = true, $backuprotate = 30, $delete_before_dump = false, + $backupdatabases = false, $ensure = 'present' ) { @@ -72,4 +74,5 @@ class mysql::backup ( owner => 'root', group => 'root', } + } diff --git a/spec/classes/mysql_backup_spec.rb b/spec/classes/mysql_backup_spec.rb index dd02ee5..dfb9101 100644 --- a/spec/classes/mysql_backup_spec.rb +++ b/spec/classes/mysql_backup_spec.rb @@ -62,4 +62,23 @@ describe 'mysql::backup' do ]) 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 = catalogue.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 end diff --git a/templates/mysqlbackup.sh.erb b/templates/mysqlbackup.sh.erb index 5624822..caadc06 100644 --- a/templates/mysqlbackup.sh.erb +++ b/templates/mysqlbackup.sh.erb @@ -31,8 +31,15 @@ cleanup() cleanup <% end -%> +<% if @backupdatabases -%> +<% @backupdatabases.each do |db| -%> +mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \ + <%= db %><% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}<%= db %>_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %> +<% end -%> +<% else -%> mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \ --all-databases <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %> +<% end -%> <% unless @delete_before_dump -%> if [ $? -eq 0 ] ; then