Skip to content

Commit

Permalink
Merge pull request #1363 from dodevops/feature/compressutility
Browse files Browse the repository at this point in the history
Support compression command and extension
  • Loading branch information
david22swan committed Feb 15, 2021
2 parents 69772a4 + 3b0bdf7 commit f0933c0
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions manifests/backup/mysqlbackup.pp
Expand Up @@ -29,6 +29,8 @@
$optional_args = [],
$incremental_backups = false,
$install_cron = true,
$compression_command = undef,
$compression_extension = undef,
) inherits mysql::params {
mysql_user { "${backupuser}@localhost":
ensure => $ensure,
Expand Down
4 changes: 3 additions & 1 deletion manifests/backup/mysqldump.pp
Expand Up @@ -30,9 +30,11 @@
$mysqlbackupdir_target = undef,
$incremental_backups = false,
$install_cron = true,
$compression_command = 'bzcat -zc',
$compression_extension = '.bz2'
) inherits mysql::params {
unless $::osfamily == 'FreeBSD' {
if $backupcompress {
if $backupcompress and $compression_command == 'bzcat -zc' {
ensure_packages(['bzip2'])
Package['bzip2'] -> File['mysqlbackup.sh']
}
Expand Down
2 changes: 2 additions & 0 deletions manifests/backup/xtrabackup.pp
Expand Up @@ -31,6 +31,8 @@
$additional_cron_args = '--backup',
$incremental_backups = true,
$install_cron = true,
$compression_command = undef,
$compression_extension = undef,
) inherits mysql::params {
ensure_packages($xtrabackup_package_name)

Expand Down
9 changes: 9 additions & 0 deletions manifests/server/backup.pp
Expand Up @@ -67,6 +67,11 @@
# Specifies an array of optional arguments which should be passed through to the backup tool. (Supported by the xtrabackup and mysqldump providers.)
# @param install_cron
# Manage installation of cron package
# @param compression_command
# Configure the command used to compress the backup (when using the mysqldump provider). Make sure the command exists
# on the target system. Packages for it are NOT automatically installed.
# @param compression_extension
# Configure the file extension for the compressed backup (when using the mysqldump provider)
class mysql::server::backup (
$backupuser = undef,
$backuppassword = undef,
Expand Down Expand Up @@ -94,6 +99,8 @@
$optional_args = [],
$incremental_backups = true,
$install_cron = true,
$compression_command = undef,
$compression_extension = undef
) inherits mysql::params {
if $prescript and $provider =~ /(mysqldump|mysqlbackup)/ {
warning(translate("The 'prescript' option is not currently implemented for the %{provider} backup provider.",
Expand Down Expand Up @@ -127,6 +134,8 @@
'optional_args' => $optional_args,
'incremental_backups' => $incremental_backups,
'install_cron' => $install_cron,
'compression_command' => $compression_command,
'compression_extension' => $compression_extension,
}
})
}
19 changes: 19 additions & 0 deletions spec/classes/mysql_backup_mysqldump_spec.rb
Expand Up @@ -52,6 +52,25 @@ class { 'mysql::server': }
)
}
end

context 'with compression_command' do
let(:params) do
{
compression_command: 'TEST -TEST',
compression_extension: '.TEST'
}.merge(default_params)
end

it {
is_expected.to contain_file('mysqlbackup.sh').with_content(
%r{(\| TEST -TEST)},
)
is_expected.to contain_file('mysqlbackup.sh').with_content(
%r{(\.TEST)},
)
is_expected.not_to contain_package('bzip2')
}
end
end
end
# rubocop:enable RSpec/NestedGroups
Expand Down
6 changes: 3 additions & 3 deletions templates/mysqlbackup.sh.erb
Expand Up @@ -93,18 +93,18 @@ mysql --defaults-extra-file=$TMPFILE -s -r -N -e 'SHOW DATABASES' | while read d
do
<%= @backupmethod -%> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
${ADDITIONAL_OPTIONS} \
${dbname} <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
${dbname} <% if @backupcompress %>| <%= @compression_command %> <% end %>> ${DIR}/${PREFIX}${dbname}_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %><%= @compression_extension %><% end %>
done
<% else -%>
<%= @backupmethod -%> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
${ADDITIONAL_OPTIONS} \
--all-databases <% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
--all-databases <% if @backupcompress %>| <%= @compression_command %> <% end %>> ${DIR}/${PREFIX}`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %><%= @compression_extension %><% end %>
<% end -%>
<% else -%>
<% @backupdatabases.each do |db| -%>
<%= @backupmethod -%> --defaults-extra-file=$TMPFILE --opt --flush-logs --single-transaction \
${ADDITIONAL_OPTIONS} \
<%= db %><% if @backupcompress %>| bzcat -zc <% end %>> ${DIR}/${PREFIX}<%= db %>_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %>.bz2<% end %>
<%= db %><% if @backupcompress %>| <%= @compression_command %> <% end %>> ${DIR}/${PREFIX}<%= db %>_`date +%Y%m%d-%H%M%S`.sql<% if @backupcompress %><%= @compression_extension %><% end %>
<% end -%>
<% end -%>
Expand Down

0 comments on commit f0933c0

Please sign in to comment.