diff --git a/manifests/init.pp b/manifests/init.pp index 0d208d6..2b776d5 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -55,10 +55,18 @@ # Whether to install a cronjob to perform a scheduled backup. # Enabled by default, pass 'false' to disable. # (Optional, enabled by default) +# [*silentcron*] +# Generate backup output only if there was an error. Setting this +# to true will surpress all mails from cron unless the backup script +# failed. +# (Optional, defaults to send daily emails). # [*install_20*] # Whether to install Xtrabackup 2.0; if set to false installs the latest (2.1) # instead. # (Optional, disabled by default) +# [*statusfile*] +# File to touch in case the backup was successfull. Can be used for to monitor +# the backup status using check_file_age. # # === Examples # @@ -98,7 +106,9 @@ $safeslave = undef, # Disconnect clients from slave $addrepo = true, # Add the Percona yum/apt repo $cronjob = true, # Install a cron job + $silentcron = false, # Send emails always $install_20 = false, # Install 2.0 instead of latest + $statusfile = undef, # statusfile to touch if cronjob was successfull. ) { if ($addrepo) { @@ -141,8 +151,13 @@ if ( !$hour or !$minute ) { fail('Hour and minute parameters are mandatory when cronjob is true.') } + if $silentcron { + $command_add = ' silent' + } else { + $command_add = '' + } cron { 'xtrabackup': - command => '/usr/local/bin/mysql-backup', + command => "/usr/local/bin/mysql-backup${command_add}", hour => $hour, minute => $minute, } diff --git a/templates/backupscript.sh.erb b/templates/backupscript.sh.erb index b715c03..1217220 100644 --- a/templates/backupscript.sh.erb +++ b/templates/backupscript.sh.erb @@ -4,25 +4,64 @@ # (C) 2013 Bashton Ltd DATE=$( date +%Y-%m-%d ) +TMP=$(mktemp) +SILENT="$1" -/usr/bin/innobackupex \ - --stream=xbstream \ - --user=<%= @dbuser %> \ - --password=<%= @dbpass %> \ - --parallel=<%= @parallel %> \ -<%= " --slave-info \\\n" if @slaveinfo -%> -<%= " --safe-slave-backup \\\n" if @safeslave -%> -<%= " " + @workdir + " \\\n" -%> -<%= " | gzip -c9 \\\n" if @gzip -%> -<%= " | ssh " + @sshdest if @sshdest -%> -<%= " -i " + @sshkey + " \\\n" if @sshkey -%> -<%= " cat - \\\n" if @sshdest -%> - > <%= @outputdir -%>/mysql-backup-${DATE}.xbstream<%= ".gz" if @gzip %> +function cleanup() { + rm -f ${TMP} +} + +function backup() { + /usr/bin/innobackupex \ + --stream=xbstream \ + --user=<%= @dbuser %> \ + --password=<%= @dbpass %> \ + --parallel=<%= @parallel %> \ + <%= " --slave-info \\\n" if @slaveinfo -%> + <%= " --safe-slave-backup \\\n" if @safeslave -%> + <%= " " + @workdir + " \\\n" -%> + <%= " | gzip -c9 \\\n" if @gzip -%> + <%= " | ssh " + @sshdest if @sshdest -%> + <%= " -i " + @sshkey + " \\\n" if @sshkey -%> + <%= " cat - \\\n" if @sshdest -%> + > <%= @outputdir -%>/mysql-backup-${DATE}.xbstream<%= ".gz" if @gzip %> + + for i in ${PIPESTATUS[@]}; do + if [ $i -gt 0 ]; then + return $i + fi + done +} <% if @keepdays -%> -# Clean up backups older than <%= @keepdays %> -<%= "ssh " + @sshdest if @sshdest -%> -<%= " -i " + @sshkey + " \\\n " if @sshkey -%> -find <%= @outputdir %> -maxdepth 1 -name 'mysql-backup-*.xbstream*' \ - -type f -mtime +<%= @keepdays %> -delete +function keepdays() { + # Clean up backups older than <%= @keepdays %> + <%= "ssh " + @sshdest if @sshdest -%> + <%= " -i " + @sshkey + " \\\n " if @sshkey -%> + find <%= @outputdir %> -maxdepth 1 -name 'mysql-backup-*.xbstream*' \ + -type f -mtime +<%= @keepdays %> -delete +} <% end -%> + + +trap cleanup EXIT + +backup 2>${TMP} +ret=$? + +if [ "${SILENT}" != "silent" -o $ret -gt 0 ]; then + cat ${TMP} +fi + +<% if @statusfile -%> +if [ $ret -eq 0 ]; then touch <%= @statusfile -%>; fi +<% end -%> + +<% if @keepdays -%> +keepdays +<% end -%> + +trap - EXIT +cleanup + +exit $ret