]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/PBSClient.pm
PBS client: backup tree: avoid over generic param has
[pve-common.git] / src / PVE / PBSClient.pm
index 1d9a9f4c9b484f850e95269a532ad057f026c70b..44e31764ef4d803a10cfe975f8e27c5d2d82ccce 100644 (file)
@@ -1,16 +1,16 @@
 package PVE::PBSClient;
-
-# utility functions for interaction with Proxmox Backup Server
+# utility functions for interaction with Proxmox Backup client CLI executable
 
 use strict;
 use warnings;
+
 use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
 use IO::File;
 use JSON;
 use POSIX qw(strftime ENOENT);
 
-use PVE::Tools qw(run_command file_set_contents file_get_contents file_read_firstline);
 use PVE::JSONSchema qw(get_standard_option);
+use PVE::Tools qw(run_command file_set_contents file_get_contents file_read_firstline);
 
 sub new {
     my ($class, $scfg, $storeid, $sdir) = @_;
@@ -20,7 +20,12 @@ sub new {
 
     my $secret_dir = $sdir // '/etc/pve/priv/storage';
 
-    my $self = bless { scfg => $scfg, storeid => $storeid, secret_dir => $secret_dir }, $class;
+    my $self = bless {
+       scfg => $scfg,
+       storeid => $storeid,
+       secret_dir => $secret_dir
+    }, $class;
+    return $self;
 }
 
 my sub password_file_name {
@@ -43,7 +48,7 @@ sub delete_password {
 
     my $pwfile = password_file_name($self);
 
-    unlink $pwfile;
+    unlink $pwfile or die "deleting password file failed - $!\n";
 };
 
 sub get_password {
@@ -63,7 +68,7 @@ sub encryption_key_file_name {
 sub set_encryption_key {
     my ($self, $key) = @_;
 
-    my $encfile = encryption_key_file_name($self);
+    my $encfile = $self->encryption_key_file_name();
     mkdir $self->{secret_dir};
 
     PVE::Tools::file_set_contents($encfile, "$key\n", 0600);
@@ -72,7 +77,7 @@ sub set_encryption_key {
 sub delete_encryption_key {
     my ($self) = @_;
 
-    my $encfile = encryption_key_file_name($self);
+    my $encfile = $self->encryption_key_file_name();
 
     if (!unlink $encfile) {
        return if $! == ENOENT;
@@ -84,7 +89,7 @@ sub delete_encryption_key {
 my sub open_encryption_key {
     my ($self) = @_;
 
-    my $encryption_key_file = encryption_key_file_name($self);
+    my $encryption_key_file = $self->encryption_key_file_name();
 
     my $keyfd;
     if (!open($keyfd, '<', $encryption_key_file)) {
@@ -141,7 +146,7 @@ my sub do_raw_client_cmd {
 
     push @$cmd, "--repository", "$username\@$server:$datastore";
 
-    local $ENV{PBS_PASSWORD} = get_password($self);
+    local $ENV{PBS_PASSWORD} = $self->get_password();
 
     local $ENV{PBS_FINGERPRINT} = $scfg->{fingerprint};
 
@@ -172,8 +177,13 @@ my sub run_client_cmd {
 
     $param = [@$param, '--output-format=json'] if !$no_output;
 
-    do_raw_client_cmd($self, $client_cmd, $param,
-                     outfunc => $outfunc, errmsg => 'proxmox-backup-client failed');
+    do_raw_client_cmd(
+        $self,
+        $client_cmd,
+        $param,
+        outfunc => $outfunc,
+        errmsg => 'proxmox-backup-client failed'
+    );
 
     return undef if $no_output;
 
@@ -184,63 +194,60 @@ my sub run_client_cmd {
 
 sub autogen_encryption_key {
     my ($self) = @_;
-    my $encfile = encryption_key_file_name($self);
-    run_command(['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile]);
+    my $encfile = $self->encryption_key_file_name();
+    run_command(
+        ['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile],
+        errmsg => 'failed to create encryption key'
+    );
+    return file_get_contents($encfile);
 };
 
+# lists all snapshots, optionally limited to a specific group
 sub get_snapshots {
-    my ($self, $opts) = @_;
+    my ($self, $group) = @_;
 
     my $param = [];
-    if (defined($opts->{group})) {
-       push @$param, $opts->{group};
-    }
+    push @$param, $group if defined($group);
 
     return run_client_cmd($self, "snapshots", $param);
 };
 
-sub backup_tree {
-    my ($self, $opts) = @_;
+# create a new PXAR backup of a FS directory tree - doesn't cross FS boundary
+# by default.
+sub backup_fs_tree {
+    my ($self, $root, $id, $pxarname, $cmd_opts) = @_;
 
-    my $type = delete $opts->{type};
-    die "backup-type not provided\n" if !defined($type);
-    my $id = delete $opts->{id};
     die "backup-id not provided\n" if !defined($id);
-    my $root = delete $opts->{root};
-    die "root dir not provided\n" if !defined($root);
-    my $pxarname = delete $opts->{pxarname};
+    die "backup root dir not provided\n" if !defined($root);
     die "archive name not provided\n" if !defined($pxarname);
-    my $time = delete $opts->{time};
 
-    my $param = [];
+    my $param = [
+       "$pxarname.pxar:$root",
+       '--backup-type', 'host',
+       '--backup-id', $id,
+    ];
 
-    push @$param, "$pxarname.pxar:$root";
-    push @$param, '--backup-type', $type;
-    push @$param, '--backup-id', $id;
-    push @$param, '--backup-time', $time if defined($time);
+    $cmd_opts //= {};
 
-    return run_raw_client_cmd($self, 'backup', $param, %$opts);
+    return run_raw_client_cmd($self, 'backup', $param, %$cmd_opts);
 };
 
 sub restore_pxar {
-    my ($self, $opts) = @_;
+    my ($self, $snapshot, $pxarname, $target, $cmd_opts) = @_;
 
-    my $snapshot = delete $opts->{snapshot};
     die "snapshot not provided\n" if !defined($snapshot);
-    my $pxarname = delete $opts->{pxarname};
     die "archive name not provided\n" if !defined($pxarname);
-    my $target = delete $opts->{target};
     die "restore-target not provided\n" if !defined($target);
-    #my $time = delete $opts->{time};
 
-    my $param = [];
-
-    push @$param, "$snapshot";
-    push @$param, "$pxarname.pxar";
-    push @$param, "$target";
-    push @$param, "--allow-existing-dirs", 0;
+    my $param = [
+       "$snapshot",
+       "$pxarname.pxar",
+       "$target",
+       "--allow-existing-dirs", 0,
+    ];
+    $cmd_opts //= {};
 
-    return run_raw_client_cmd($self, 'restore', $param, %$opts);
+    return run_raw_client_cmd($self, 'restore', $param, %$cmd_opts);
 };
 
 sub forget_snapshot {
@@ -248,11 +255,7 @@ sub forget_snapshot {
 
     die "snapshot not provided\n" if !defined($snapshot);
 
-    my $param = [];
-
-    push @$param, "$snapshot";
-
-    return run_raw_client_cmd($self, 'forget', $param);
+    return run_raw_client_cmd($self, 'forget', ["$snapshot"]);
 };
 
 sub prune_group {