From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <s.ivanov@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 8CBFD63D5A
 for <pmg-devel@pve.proxmox.com>; Wed, 28 Oct 2020 19:55:26 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 45C8A22AC6
 for <pmg-devel@pve.proxmox.com>; Wed, 28 Oct 2020 19:54:54 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id EBF59229E7
 for <pmg-devel@pve.proxmox.com>; Wed, 28 Oct 2020 19:54:48 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id B160F45F3A
 for <pmg-devel@pve.proxmox.com>; Wed, 28 Oct 2020 19:54:48 +0100 (CET)
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pmg-devel@pve.proxmox.com
Date: Wed, 28 Oct 2020 19:54:27 +0100
Message-Id: <20201028185432.23067-12-s.ivanov@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20201028185432.23067-1-s.ivanov@proxmox.com>
References: <20201028185432.23067-1-s.ivanov@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.089 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [pmgbackup.pm]
Subject: [pmg-devel] [PATCH pmg-api 09/11] pbs-integration: add CLI calls to
 pmgbackup
X-BeenThere: pmg-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Mail Gateway development discussion
 <pmg-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pmg-devel/>
List-Post: <mailto:pmg-devel@lists.proxmox.com>
List-Help: <mailto:pmg-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Wed, 28 Oct 2020 18:55:26 -0000

add two new categories for commands to pmgbackup:
* pmgbackup remote - for managing PBS instances' configuration, cluster-wide
* pmgbackup pbsjob - for managing backups, restores, pruning

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
 src/PMG/CLI/pmgbackup.pm | 96 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)

diff --git a/src/PMG/CLI/pmgbackup.pm b/src/PMG/CLI/pmgbackup.pm
index 69224e5..2a85b76 100644
--- a/src/PMG/CLI/pmgbackup.pm
+++ b/src/PMG/CLI/pmgbackup.pm
@@ -3,14 +3,20 @@ package PMG::CLI::pmgbackup;
 use strict;
 use warnings;
 use Data::Dumper;
+use POSIX qw(strftime);
 
 use PVE::Tools;
 use PVE::SafeSyslog;
 use PVE::INotify;
 use PVE::CLIHandler;
+use PVE::CLIFormatter;
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::PBSTools;
 
 use PMG::RESTEnvironment;
 use PMG::API2::Backup;
+use PMG::API2::PBS::Remote;
+use PMG::API2::PBS::Job;
 
 use base qw(PVE::CLIHandler);
 
@@ -28,10 +34,100 @@ my $format_backup_list = sub {
     }
 };
 
+__PACKAGE__->register_method ({
+    name => 'prune',
+    path => 'prune',
+    method => 'GET',
+    description => "Prune all remotes",
+    protected => 1,
+    permissions => { check => [ 'admin', 'audit' ] },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    remote => {
+		description => "only prune the provided Proxmox Backup Server.",
+		type => 'string', format => 'pve-configid',
+		optional => 1,
+	    },
+	    'dry-run' => { type => 'boolean', optional => 1, default => 0},
+	    'verbose' => { type => 'boolean', optional => 1, default => 0},
+	},
+    },
+    returns => { type => "string" },
+    code => sub {
+	my ($param) = @_;
+
+	my $remote = $param->{remote};
+	my $node = $param->{node};
+
+	my $verbose = defined($param->{verbose}) && $param->{verbose};
+
+	my $group = "host/$node";
+
+	my $conf = PMG::PBSConfig->new();
+
+	my $pbs_opts = {};
+	if (defined($param->{'dry-run'}) && $param->{'dry-run'}) {
+	    $pbs_opts->{'dry-run'} = 1;
+	}
+
+	my $run_prune = sub {
+	    my ($remote_config, $remote) = @_;
+
+	    print "running prune for $remote\n" if $verbose;
+
+	    my $prune_opts = $conf->prune_options($remote);
+	    my $res = PVE::PBSTools::prune_group($remote_config, $remote, $pbs_opts, $prune_opts, $group);
+
+	    if ($verbose) {
+		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";
+		}
+
+	    }
+	    print "finished pruning $remote\n" if $verbose;
+	};
+
+	if (defined($remote)) {
+	    my $remote_config = $conf->{ids}->{$remote};
+	    die "PBS remote '$remote' does not exist\n" if !$remote_config;
+	    die "PBS remote '$remote' is disabled\n" if $remote_config->{disable};
+	    $run_prune->($remote_config, $remote);
+	} else {
+	    foreach my $remote (sort keys %{$conf->{ids}}){
+		my $cfg = $conf->{ids}->{$remote};
+		next if $cfg->{disable};
+		$run_prune->($cfg, $remote);
+	    }
+	}
+    }});
+
 our $cmddef = {
     backup => [ 'PMG::API2::Backup', 'backup', undef, { node => $nodename } ],
     restore => [ 'PMG::API2::Backup', 'restore', undef, { node => $nodename } ],
     list => [ 'PMG::API2::Backup', 'list', undef, { node => $nodename }, $format_backup_list ],
+    remote => {
+	list => ['PMG::API2::PBS::Remote', 'list', undef, undef,  sub {
+	    my ($data, $schema, $options) = @_;
+	    PVE::CLIFormatter::print_api_result($data, $schema, ['remote', 'server', 'datastore', 'username' ], $options);
+	}, $PVE::RESTHandler::standard_output_options ],
+	add => ['PMG::API2::PBS::Remote', 'create', ['remote'] ],
+	remove => ['PMG::API2::PBS::Remote', 'delete', ['remote'] ],
+	set => ['PMG::API2::PBS::Remote', 'update_config', ['remote'] ],
+    },
+    pbsjob => {
+	list_backups => ['PMG::API2::PBS::Job', 'get_snapshots', ['remote'] , { node => $nodename },  sub {
+	    my ($data, $schema, $options) = @_;
+	    PVE::CLIFormatter::print_api_result($data, $schema, ['time', 'size'], $options);
+	}, $PVE::RESTHandler::standard_output_options ],
+	forget => ['PMG::API2::PBS::Job', 'forget_snapshot', ['remote', 'time'], { node => $nodename} ],
+	run => ['PMG::API2::PBS::Job', 'run_backup', ['remote'], { node => $nodename} ],
+	restore => ['PMG::API2::PBS::Job', 'restore', ['remote'], { node => $nodename} ],
+	prune => [__PACKAGE__, 'prune', undef , { node => $nodename } ],
+    },
 };
 
 1;
-- 
2.20.1