From 6913cd00e51f2d9be746c77e27df0cc2414cdf57 Mon Sep 17 00:00:00 2001 From: Bernd Zeimetz Date: Sun, 22 Feb 2015 13:42:10 +0100 Subject: [PATCH 1/2] Add option to enable silent cron runs. If activated, cron will send emails on errors only. --- manifests/init.pp | 43 +++++++++++++-------- templates/backupscript.sh.erb | 71 ++++++++++++++++++++++++++--------- 2 files changed, 80 insertions(+), 34 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index a5a0bb2..be4b175 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -55,6 +55,11 @@ # 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). # # === Examples # @@ -79,21 +84,22 @@ # # Copyright 2013 Bashton Ltd # -class xtrabackup ($dbuser, # Database username - $dbpass, # Database password - $hour = undef, # Cron hour - $minute = undef, # Cron minute - $workdir = '/tmp', # Working directory - $outputdir, # Directory to output to - $sshdest = undef, # SSH destination - $sshkey = undef, # SSH private key to use - $keepdays = undef, # Keep the last x days of backups - $gzip = true, # Compress using gzip - $parallel = 1, # Threads to use - $slaveinfo = undef, # Record master log pos if true - $safeslave = undef, # Disconnect clients from slave - $addrepo = true, # Add the Percona yum/apt repo - $cronjob = true, # Install a cron job +class xtrabackup ($dbuser, # Database username + $dbpass, # Database password + $hour = undef, # Cron hour + $minute = undef, # Cron minute + $workdir = '/tmp', # Working directory + $outputdir, # Directory to output to + $sshdest = undef, # SSH destination + $sshkey = undef, # SSH private key to use + $keepdays = undef, # Keep the last x days of backups + $gzip = true, # Compress using gzip + $parallel = 1, # Threads to use + $slaveinfo = undef, # Record master log pos if true + $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 ) { if ($addrepo) { @@ -131,8 +137,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..151d6d5 100644 --- a/templates/backupscript.sh.erb +++ b/templates/backupscript.sh.erb @@ -4,25 +4,60 @@ # (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 @keepdays -%> +keepdays +<% end -%> + +trap - EXIT +cleanup + +exit $ret From 2c4e6d886242a483faf194ee285e5cde0aa42e3b Mon Sep 17 00:00:00 2001 From: Bernd Zeimetz Date: Tue, 7 Jul 2015 10:32:34 +0200 Subject: [PATCH 2/2] Add statusfile option. This allows to monitor the backup status using check_file_age. --- manifests/init.pp | 4 ++++ templates/backupscript.sh.erb | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/manifests/init.pp b/manifests/init.pp index 3b0d317..2b776d5 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -64,6 +64,9 @@ # 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 # @@ -105,6 +108,7 @@ $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) { diff --git a/templates/backupscript.sh.erb b/templates/backupscript.sh.erb index 151d6d5..1217220 100644 --- a/templates/backupscript.sh.erb +++ b/templates/backupscript.sh.erb @@ -53,6 +53,10 @@ 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 -%>