From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api 3/5] backup: fix #3146 add email notifications
Date: Wed, 24 Feb 2021 19:31:00 +0100 [thread overview]
Message-ID: <20210224183109.29014-4-s.ivanov@proxmox.com> (raw)
In-Reply-To: <20210224183109.29014-1-s.ivanov@proxmox.com>
this patch addresses the missing email notification for scheduled
backup related tasks, which we have in all our other products, for our
mail product.
the parameter names are inspired by PBS' datastore config.
it uses the templateing system for the notification, because this
results in less code and a bit of added flexibility for the users.
the recipient address is currently hardcoded to the admin address in
pmg.conf as we also send the (admin) pmgreport there, and I did not
want to overengineer this (even more).
Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
src/Makefile | 3 ++-
src/PMG/API2/PBS/Job.pm | 40 +++++++++++++++++++++-------
src/PMG/Backup.pm | 31 +++++++++++++++++++++
src/PMG/PBSConfig.pm | 7 +++++
src/templates/backup-notification.tt | 19 +++++++++++++
5 files changed, 89 insertions(+), 11 deletions(-)
create mode 100644 src/templates/backup-notification.tt
diff --git a/src/Makefile b/src/Makefile
index 9d5c335..0ad805d 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -42,7 +42,8 @@ TEMPLATES = \
freshclam.conf.in \
clamd.conf.in \
postgresql.conf.in \
- pg_hba.conf.in
+ pg_hba.conf.in \
+ backup-notification.tt
TEMPLATES_FILES = $(addprefix templates/, ${TEMPLATES})
diff --git a/src/PMG/API2/PBS/Job.pm b/src/PMG/API2/PBS/Job.pm
index 2387422..279afbc 100644
--- a/src/PMG/API2/PBS/Job.pm
+++ b/src/PMG/API2/PBS/Job.pm
@@ -273,6 +273,13 @@ __PACKAGE__->register_method ({
optional => 1,
default => 1,
},
+ notify => {
+ description => "Specify when to notify via e-mail",
+ type => 'string',
+ enum => [ 'always', 'error', 'never' ],
+ optional => 1,
+ default => 'error',
+ },
},
},
returns => { type => "string" },
@@ -292,6 +299,7 @@ __PACKAGE__->register_method ({
die "PBS remote '$remote' is disabled\n" if $remote_config->{disable};
$param->{statistic} //= $remote_config->{'include-statistics'} // 1;
+ my $notify = $param->{notify} // $remote_config->{notify} // 'never';
my $pbs = PVE::PBSClient->new($remote_config, $remote, $conf->{secret_dir});
my $backup_dir = "/var/lib/pmg/backup/current";
@@ -299,30 +307,42 @@ __PACKAGE__->register_method ({
my $worker = sub {
my $upid = shift;
- print "starting update of current backup state\n";
+ my $log = "starting update of current backup state\n";
- -d $backup_dir || mkdir $backup_dir;
- PMG::Backup::pmg_backup($backup_dir, $param->{statistic});
+ eval {
+ -d $backup_dir || mkdir $backup_dir;
+ PMG::Backup::pmg_backup($backup_dir, $param->{statistic});
- $pbs->backup_fs_tree($backup_dir, $node, 'pmgbackup');
+ $pbs->backup_fs_tree($backup_dir, $node, 'pmgbackup');
- rmtree $backup_dir;
+ rmtree $backup_dir;
+ };
+ my $err = $@;
+ $log .= $err if $err;
- print "backup finished\n";
+ $log .= "backup finished\n";
my $group = "host/$node";
if (defined(my $prune_opts = $conf->prune_options($remote))) {
- print "starting prune of $group\n";
- my $res = $pbs->prune_group(undef, $prune_opts, $group);
+ $log .= "starting prune of $group\n";
+ my $res = eval { $pbs->prune_group(undef, $prune_opts, $group) };
+ $log .= $@ if $@;
+ $err //= $@ if $@;
foreach my $pruned (@$res){
my $time = strftime("%FT%TZ", gmtime($pruned->{'backup-time'}));
my $snap = $pruned->{'backup-type'} . '/' . $pruned->{'backup-id'} . '/' . $time;
- print "pruned snapshot: $snap\n";
+ $log .= "pruned snapshot: $snap\n";
}
- print "prune finished\n";
+ $log .= "prune finished\n";
}
+ PMG::Backup::send_backup_notification($notify, $remote, $log, $err);
+
+ die "$err\n" if $err;
+
+ print $log;
+
return;
};
diff --git a/src/PMG/Backup.pm b/src/PMG/Backup.pm
index 303bbb4..1883d02 100644
--- a/src/PMG/Backup.pm
+++ b/src/PMG/Backup.pm
@@ -5,6 +5,7 @@ use warnings;
use Data::Dumper;
use File::Basename;
use File::Path;
+use POSIX qw(strftime);
use PVE::JSONSchema qw(get_standard_option);
use PVE::Tools;
@@ -376,4 +377,34 @@ sub pmg_restore {
die $err if $err;
}
+sub send_backup_notification {
+ my ($when, $target, $log, $err) = @_;
+
+ return if $when eq 'never';
+ return if ($when eq 'error' && !$err);
+
+ my $cfg = PMG::Config->new();
+ my $email = $cfg->get ('admin', 'email');
+ die "no admin email configured\n" if !$email;
+
+ my $nodename = PVE::INotify::nodename();
+ my $fqdn = PVE::Tools::get_fqdn($nodename);
+
+
+ my $vars = {
+ hostname => $nodename,
+ fqdn => $fqdn,
+ date => strftime("%F", localtime()),
+ target => $target,
+ log => $log,
+ err => $err,
+ };
+
+ my $tt = PMG::Config::get_template_toolkit();
+
+ my $mailfrom = "Proxmox Mail Gateway <postmaster>";
+ PMG::Utils::finalize_report($tt, 'backup-notification.tt', $vars, $mailfrom, $email);
+
+}
+
1;
diff --git a/src/PMG/PBSConfig.pm b/src/PMG/PBSConfig.pm
index 29cff22..965d162 100644
--- a/src/PMG/PBSConfig.pm
+++ b/src/PMG/PBSConfig.pm
@@ -90,6 +90,12 @@ sub properties {
description => "Username or API token ID on the Proxmox Backup Server"
}),
fingerprint => get_standard_option('fingerprint-sha256'),
+ notify => {
+ description => "Specify when to notify via e-mail",
+ type => 'string',
+ enum => [ 'always', 'error', 'never' ],
+ optional => 1,
+ },
'include-statistics' => {
description => "Include statistics in scheduled backups",
type => 'boolean',
@@ -107,6 +113,7 @@ sub options {
username => { optional => 1 },
password => { optional => 1 },
fingerprint => { optional => 1 },
+ notify => { optional => 1 },
'include-statistics' => { optional => 1 },
'keep-last' => { optional => 1 },
'keep-hourly' => { optional => 1 },
diff --git a/src/templates/backup-notification.tt b/src/templates/backup-notification.tt
new file mode 100644
index 0000000..3f956b0
--- /dev/null
+++ b/src/templates/backup-notification.tt
@@ -0,0 +1,19 @@
+[%- IF err -%]
+[%- SET titleprefix = "Backup failed" -%]
+[%- ELSE %]
+[%- SET titleprefix = "Backup successful" -%]
+[%- END -%]
+[%- IF target -%]
+[%- SET titlesuffix = "to ${target} - ($fqdn)" -%]
+[%- ELSE %]
+[%- SET titlesuffix = "($fqdn)" -%]
+[%- END -%]
+<html>
+ <head>
+ <title>[% titleprefix %] [% titlesuffix %]</title>
+ </head>
+ <body>
+ Detailed backup logs:<br /><br />
+ <pre>[% log %]</pre>
+ </body>
+</html>
--
2.20.1
next prev parent reply other threads:[~2021-02-24 18:31 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-24 18:30 [pmg-devel] [PATCH pmg-api/gui/docs] small improvments for PBS integration Stoiko Ivanov
2021-02-24 18:30 ` [pmg-devel] [PATCH pmg-api 1/5] backup: fix die invocation Stoiko Ivanov
2021-02-25 9:40 ` [pmg-devel] applied: " Thomas Lamprecht
2021-02-24 18:30 ` [pmg-devel] [PATCH pmg-api 2/5] backup: fix #3154 add 'include-statistics' to pbs Stoiko Ivanov
2021-02-25 9:40 ` [pmg-devel] applied: " Thomas Lamprecht
2021-02-24 18:31 ` Stoiko Ivanov [this message]
2021-02-25 10:04 ` [pmg-devel] [PATCH pmg-api 3/5] backup: fix #3146 add email notifications Thomas Lamprecht
2021-02-24 18:31 ` [pmg-devel] [PATCH pmg-api 4/5] backup: add notify parameter to 'classic' backup Stoiko Ivanov
2021-02-25 10:06 ` Thomas Lamprecht
2021-02-24 18:31 ` [pmg-devel] [PATCH pmg-api 5/5] backup: pbs: prevent race in concurrent backups Stoiko Ivanov
2021-02-25 10:14 ` Thomas Lamprecht
2021-02-24 18:31 ` [pmg-devel] [PATCH pmg-gui 1/4] backup: pbs: add onlineHelp anchors Stoiko Ivanov
2021-02-24 18:31 ` [pmg-devel] [PATCH pmg-gui 2/4] backup: fix #3154: make statistic backup optional Stoiko Ivanov
2021-02-24 18:31 ` [pmg-devel] [PATCH pmg-gui 3/4] backup: pbs: fix #3154: add statistic setting to remote Stoiko Ivanov
2021-02-24 18:31 ` [pmg-devel] [PATCH pmg-gui 4/4] backup: pbs: fix #3146 add notify " Stoiko Ivanov
2021-02-24 18:31 ` [pmg-devel] [PATCH pmg-docs 1/3] backup: fix typos Stoiko Ivanov
2021-02-25 10:15 ` [pmg-devel] applied: " Thomas Lamprecht
2021-02-24 18:31 ` [pmg-devel] [PATCH pmg-docs 2/3] backup: add anchors to pbs sections Stoiko Ivanov
2021-02-25 10:15 ` [pmg-devel] applied: " Thomas Lamprecht
2021-02-24 18:31 ` [pmg-devel] [PATCH pmg-docs 3/3] backup: shortly document #3146 and #3154 Stoiko Ivanov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210224183109.29014-4-s.ivanov@proxmox.com \
--to=s.ivanov@proxmox.com \
--cc=pmg-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox