From 6a7d84369e194747ab9079eb779c66ed6c276db2 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Wed, 26 Sep 2012 13:40:20 -0700 Subject: [PATCH 1/3] Add spec tests for backup compression enabled/disabled --- spec/classes/mysql_backup_spec.rb | 59 ++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/spec/classes/mysql_backup_spec.rb b/spec/classes/mysql_backup_spec.rb index ed45ba3..ff5965b 100644 --- a/spec/classes/mysql_backup_spec.rb +++ b/spec/classes/mysql_backup_spec.rb @@ -2,32 +2,57 @@ require 'spec_helper' describe 'mysql::backup' do - let(:params) { + let(:default_params) { { 'backupuser' => 'testuser', 'backuppassword' => 'testpass', 'backupdir' => '/tmp', } } + context "standard conditions" do + let(:params) { default_params } - it { should contain_database_user('testuser@localhost')} + it { should contain_database_user('testuser@localhost')} - it { should contain_database_grant('testuser@localhost').with( - :privileges => [ 'Select_priv', 'Reload_priv', 'Lock_tables_priv', 'Show_view_priv' ] - )} + it { should contain_database_grant('testuser@localhost').with( + :privileges => [ 'Select_priv', 'Reload_priv', 'Lock_tables_priv', 'Show_view_priv' ] + )} - it { should contain_cron('mysql-backup').with( - :command => '/usr/local/sbin/mysqlbackup.sh', - :ensure => 'present' - )} + 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('mysqlbackup.sh').with( + :path => '/usr/local/sbin/mysqlbackup.sh', + :ensure => 'present' + ) } - it { should contain_file('mysqlbackupdir').with( - :path => '/tmp', - :ensure => 'directory' - )} + 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}/mysql_backup_`date +%Y%m%d-%H%M%S`.sql.bz2', + ]) + end + 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}/mysql_backup_`date +%Y%m%d-%H%M%S`.sql', + ]) + end + end end From af30a521821ad1bac2a08a4ea1f060b8a7e7c456 Mon Sep 17 00:00:00 2001 From: Justin Brown Date: Wed, 26 Sep 2012 13:50:55 -0500 Subject: [PATCH 2/3] Backup Compression Optional Added a parameter to mysql::backup to make bzip2 compression optional. Compression is enabled by default, so current behavior is not affected. Rationale: We are storing our MySQL backups on a system with deduplication. Compression and dedup do not play well together, so it's advantagous to be able to disable it. --- manifests/backup.pp | 3 +++ templates/mysqlbackup.sh.erb | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/manifests/backup.pp b/manifests/backup.pp index 5e8056b..7dfffca 100644 --- a/manifests/backup.pp +++ b/manifests/backup.pp @@ -6,6 +6,7 @@ # [*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. # # Actions: # GRANT SELECT, RELOAD, LOCK TABLES ON *.* TO 'user'@'localhost' @@ -19,12 +20,14 @@ # backupuser => 'myuser', # backuppassword => 'mypassword', # backupdir => '/tmp/backups', +# backupcompress => true, # } # class mysql::backup ( $backupuser, $backuppassword, $backupdir, + $backupcompress = true, $ensure = 'present' ) { diff --git a/templates/mysqlbackup.sh.erb b/templates/mysqlbackup.sh.erb index a2dba33..e7cc624 100644 --- a/templates/mysqlbackup.sh.erb +++ b/templates/mysqlbackup.sh.erb @@ -19,5 +19,5 @@ PATH=/usr/bin:/usr/sbin:/bin:/sbin find $DIR -mtime +30 -exec rm -f {} \; mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \ - --all-databases | bzcat -zc > ${DIR}/mysql_backup_`date +%Y%m%d-%H%M%S`.sql.bz2 + --all-databases <% if backupcompress %> | bzcat -zc <% end %> > ${DIR}/mysql_backup_`date +%Y%m%d-%H%M%S`.sql<% if backupcompress %>.bz2<% end %> From d212c87f0ae73f000238930cbf938c23f53463e0 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Wed, 26 Sep 2012 13:40:42 -0700 Subject: [PATCH 3/3] Tweak template spacing to pass spec tests with clean output --- templates/mysqlbackup.sh.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/mysqlbackup.sh.erb b/templates/mysqlbackup.sh.erb index e7cc624..b75ee4a 100644 --- a/templates/mysqlbackup.sh.erb +++ b/templates/mysqlbackup.sh.erb @@ -19,5 +19,5 @@ PATH=/usr/bin:/usr/sbin:/bin:/sbin find $DIR -mtime +30 -exec rm -f {} \; mysqldump -u${USER} -p${PASS} --opt --flush-logs --single-transaction \ - --all-databases <% if backupcompress %> | bzcat -zc <% end %> > ${DIR}/mysql_backup_`date +%Y%m%d-%H%M%S`.sql<% if backupcompress %>.bz2<% end %> + --all-databases <% if backupcompress %>| bzcat -zc <% end %>> ${DIR}/mysql_backup_`date +%Y%m%d-%H%M%S`.sql<% if backupcompress %>.bz2<% end %>