[pmg-devel] [PATCH pmg-api v2 3/3] backup: add notify parameter to 'classic' backup

Stoiko Ivanov s.ivanov at proxmox.com
Mon Mar 1 15:12:20 CET 2021


for feature-parity, and since we recently had a user in our community
forum, who does regular backups via cron+rsync (small site w/o
dedicated backup host). Those setups could also benefit from this.

Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
 src/PMG/API2/Backup.pm | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/PMG/API2/Backup.pm b/src/PMG/API2/Backup.pm
index 4ea28d1..2248fa5 100644
--- a/src/PMG/API2/Backup.pm
+++ b/src/PMG/API2/Backup.pm
@@ -110,6 +110,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 => 'never',
+	    },
 	},
     },
     returns => { type => "string" },
@@ -129,11 +136,21 @@ __PACKAGE__->register_method ({
 	my $worker = sub {
 	    my $upid = shift;
 
-	    print "starting backup to: $filename\n";
+	    my $full_log = "";
+	    my $log = sub { print "$_[0]\n"; $full_log .= "$_[0]\n"; };
+
+	    $log->("starting backup to: $filename\n");
+
+	    eval { PMG::Backup::pmg_backup_pack($filename, $param->{statistic}) };
+	    if (my $err = $@) {
+		$log->($err);
+		PMG::Backup::send_backup_notification($param->{notify}, undef, $full_log, $err);
+		die "backup failed: $err\n";
+	    }
 
-	    PMG::Backup::pmg_backup_pack($filename, $param->{statistic});
+	    $log->("backup finished\n");
 
-	    print "backup finished\n";
+	    PMG::Backup::send_backup_notification($param->{notify}, undef, $full_log, undef);
 
 	    return;
 	};
-- 
2.20.1





More information about the pmg-devel mailing list