diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-05-30 13:41:09 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-05-30 13:43:25 +0300 |
commit | fbea83e22332724c7bff2f0a67dde810476f004b (patch) | |
tree | bebc165d9f6876cf156580d71c1a990e800cfb7a | |
parent | d669127d2efaf9969b081fbaff47ff8938388750 (diff) | |
download | beam-fbea83e22332724c7bff2f0a67dde810476f004b.tar.gz beam-fbea83e22332724c7bff2f0a67dde810476f004b.tar.bz2 |
Implement mail notification.
* backup.in (backup): New function.
Send mail notification if backup_notify_email is not empty.
* lib/beam/common.in (mail_report): New function.
* beam.conf: Provide examples for the new variables.
* beam.in: Fix typos.
* doc/beam.conf.5in: Document new variables.
-rw-r--r-- | backup.in | 69 | ||||
-rw-r--r-- | beam.conf | 56 | ||||
-rw-r--r-- | beam.in | 6 | ||||
-rw-r--r-- | doc/beam.conf.5in | 114 | ||||
-rw-r--r-- | lib/beam/common.in | 41 |
5 files changed, 257 insertions, 29 deletions
@@ -95,32 +95,53 @@ do shift done -if [ -n "$backup_logfile" ]; then - exec >>$backup_logfile - exec 2>&1 -fi -logit "started" - -if test -n "$backup_retain_interval" && test $backup_retain_interval -gt 0; then - @LIBEXECDIR@/beam-cleaner --retain $backup_retain_interval $verbose \ - --suffix .tar.bz2 $backup_archive_dir - @LIBEXECDIR@/beam-cleaner --retain $backup_retain_interval $verbose \ - --suffix .db $backup_snapshot_dir -fi - -runhook prologue_hook -trap "runhook epilogue_hook" EXIT INT QUIT TERM - -for item in $backup_items -do - eval type=\$${item}_type - ${type}_backup $item -done +backup() { + logit "started" + + if test -n "$backup_retain_interval" && + test $backup_retain_interval -gt 0; then + @LIBEXECDIR@/beam-cleaner --retain $backup_retain_interval $verbose \ + --suffix .tar.bz2 $backup_archive_dir + @LIBEXECDIR@/beam-cleaner --retain $backup_retain_interval $verbose \ + --suffix .db $backup_snapshot_dir + fi + + runhook prologue_hook + trap "runhook epilogue_hook" EXIT INT QUIT TERM + + for item in $backup_items + do + eval type=\$${item}_type + ${type}_backup $item + done + + trap - EXIT INT QUIT TERM + runhook epilogue_hook + + logit "finished" +} -trap - EXIT INT QUIT TERM -runhook epilogue_hook +if [ -z "$backup_notify_email" ]; then + if [ -n "$backup_logfile" ]; then + exec >>$backup_logfile + exec 2>&1 + fi + backup +else + u=$(umask) + umask 077 + report=$backup_snapshot_dir/report.$$ + touch $report + umask $u + if [ -n "$backup_logfile" ]; then + ( exec 2>&1; backup ) | tee $report >> $backup_logfile + else + ( exec 2>&1; backup ) | tee $report + fi + mail_report $report + rm $report +fi -logit "finished" # finis coronat opus @@ -111,10 +111,10 @@ backup_items="dbdump system" # # fs back up a file system # postgres back up a postgres database +# mysql backu up a mysql database # # You may define additional types, if you need. To do so, create an executable -# file named <type>.sh in the directory @LIBDIR@/backup. See backup_type(5), -# for a detailed description. +# file named <type>.sh in the directory @LIBDIR@/beam. # # Dump Postgres database "mydb". @@ -135,4 +135,56 @@ system_dir="/" system_files="etc var/spool" +########################################################################## +# Mail notification setup. +# +# Apart from usual logging, backup reports can be sent via email. To +# enable this, define the variable "backup_notify_email" to the list +# of emails that should receive the reports. Separate multiple addresses +# with commas. +# +# A set of variables is provided to customize report headers and contents. +########################################################################## + +# Comma-separated list of emails to send backup reports to. If emply, +# mail notifications are not sent. +backup_notify_email= + +# Sender email address. Backup reports will appear to be sent from this +# address. The default value is root@$(hostname). +# If set, this variable must contain a single email address, without +# personal part or comments, e.g. +# backup_sender_email=root@example.com +backup_sender_email= + +# You can supply personal part of the sender email using this variable. +# The personal part will be enclosed in double quotes and prepended to +# the value of $backup_sender_email to form a valid RFC-2822 From header. +# For example, if you have: +# backup_sender_email=root@example.com +# backup_sender_personal="Automatic backup report" +# you will see the following in the report headers: +# From: "Automatic backup report" <root@example.com> +backup_sender_personal= + +# Supply any additional headers for the report message. By default, the +# following headers are generated: +# From, To, Subject, X-Beam-Items, X-Beam-Round and X-Beam-Level. +# The three X- headers contain the backed up items (as set in the +# backup_items variable), backup round and backup level numbers, +# correspondingly. +# +# To supply multiple headers, delimit them with a single newline character. +backup_report_headers= + +# The value of this variable is output before the actual report. +backup_report_intro= + +# The value of this variable is added at the end of the report. +backup_report_signature= + +# Reports are sent using this program. If not set, it defaults to +# /usr/sbin/sendmail -oi -t -F $backup_sender_email +# The generated report is piped to the standard input of this program. +backup_mailer_program= @@ -1,17 +1,17 @@ #! /bin/bash -libdir=@LIBDIR@/bbe +libdir=@LIBDIR@/beam set -e . $libdir/common.sh set +e help() { cat <<EOT -usage: bbe COMMAND [OPTIONS] [ITEM [ITEM...]] +usage: beam COMMAND [OPTIONS] [ITEM [ITEM...]] COMMANDS are: EOT - for cmd in @LIBEXECDIR@/bbe-* + for cmd in @LIBEXECDIR@/beam-* do test -x $cmd && $cmd --wtf done diff --git a/doc/beam.conf.5in b/doc/beam.conf.5in index cf196e9..17d116b 100644 --- a/doc/beam.conf.5in +++ b/doc/beam.conf.5in @@ -217,6 +217,110 @@ The following variables must be defined for items of this type: .TP .B \fIitem\fB_database The database name. +.SH MYSQL BACKUP TYPE +The +.B mysql +backup type creates a dump of a MySQL database and archives it witj +.BR tar (1). +The database to dump and access credentials are specified using the +following variables: +.TP +.B \fIitem\fR_database +The database name. +.TP +.B \fIitem\fR_defaults_file +A full pathname of the MySQL defaults file containing credenitals for +accessing this database. This file must have at least the +.B mysqldump +and +.B mysql +sections, the former being used when dumping the database and the +latter when restoring it. +.SH MAIL NOTIFICATION +Apart from usual logging, backup reports can be sent via email to +selected recipients. To enable this feature, the variable +.B backup_notify_email +must be defined to a comma-separated list of recipient email +addresses. +.PP +A number of variables is provided to customize the email headers and +contents. +.TP +.B backup_notify_email +Sets a list of emails to receive backup report. Multiple emails must +be separated with commas. If not set, no notification will be sent. +.TP +.B backup_sender_email +Defines the sender email address. Backup reports will appear to be +sent from this address. The default value is +\fBroot\fR@\fIhostname\fR, when \fIhostname\fR is the name of the host +on which the backup is run. + +If set, this variable must contain a single email address, without +personal part or comments, e.g. +.sp +.nf +.in +2 +backup_sender_email=root@example.com +.in +.fi +.sp +.TP +.B backup_sender_personal +Defines personal part of the sender email. The personal part will be +enclosed in double quotes and prepended to the value of +.B backup_sender_email +to form a valid RFC-2822 +.B From +header. For example, if you have: +.sp +.nf +.in +2 +backup_sender_email=root@example.com +backup_sender_personal="Automatic backup report" +.in +.fi +.sp +then the resulting report will contain: +.sp +.nf +.in +2 +From: "Automatic backup report" <root@example.com> +.in +.fi +.sp +.TP +.B backup_report_headers +Defines additional headers for the report message. By default, the +following headers are generated: +.BR From , +.BR To , +.BR Subject , +.BR X-Beam-Items , +.BR X-Beam-Round , +and +.BR X-Beam-Level . The three \fBX-\fR headers contain the backed up +items (as set in the +.B backup_items +variable), backup round and backup level numbers, correspondingly. + +To supply multiple headers, delimit them with single newline characters. +.TP +.B backup_report_intro +Sets the introductory text to be displayed before the actual report. +.TP +.B backup_report_signature +Sets the signature text, which will be output after the report body. +.TP +.B backup_mailer_program +Defines the mailer program. The default is +.sp +.nf +.in +2 +/usr/sbin/sendmail -oi -t -F $backup_sender_email +.in +.fi +.sp .SH HOOKS Two special variables, called hooks, allow you to supply arbitrary procedures to be run before and after backup. @@ -261,6 +365,16 @@ dbdump_database=savane system_type=fs system_dir="/" system_files="etc home" + +# Notify root about the results. +backup_notify_email=root@example.com +backup_sender_email=devnull@example.com +backup_sender_personal="Automatic daily backup" +backup_report_intro="Today's daily backup produced the following +results." +backup_report_signature="\-\- +Best regards, +Beam Automatic Backup" .in .fi .sp diff --git a/lib/beam/common.in b/lib/beam/common.in index 49b41eb..c23baf5 100644 --- a/lib/beam/common.in +++ b/lib/beam/common.in @@ -133,3 +133,44 @@ wtf() { shift echo " $s $@" } + +# mail_report FILE +mail_report() { + if [ -z "$backup_sender_email" ]; then + backup_sender_email=root@$(hostname) + fi + case $backup_sender_email in + "<"*) ;; + *) backup_sender_email="<$backup_sender_email>" + esac + if [ -n "$backup_sender_personal" ]; then + case $backup_sender_personal in + \"*) ;; + *) backup_sender_personal="\"$backup_sender_personal\"" + esac + backup_sender_personal="$backup_sender_personal " + fi + if [ -z "$backup_report_subject" ]; then + backup_report_subject="Backup of $(hostname) on $(date)" + fi + test -n "$backup_report_signature" && + echo "$backup_report_signature" >> $report + (cat - <<EOF +From: ${backup_sender_personal}$backup_sender_email +To: $backup_notify_email +Subject: $backup_report_subject +X-Beam-Items: $backup_items +X-Beam-Round: $round +X-Beam-Level: $level +EOF + if [ -n "$backup_report_headers" ]; then + echo "$backup_report_headers" + fi + echo "" + if [ -n "$backup_report_intro" ]; then + echo "$backup_report_intro" + fi + cat $report +) | \ + ${backup_mailer_program:-/usr/sbin/sendmail -oi -t -F $backup_sender_email} +} |