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) = @_;
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 {
sub set_password {
my ($self, $password) = @_;
- my $pwfile = password_file_name($self);
+ my $pwfile = $self->password_file_name();
mkdir $self->{secret_dir};
PVE::Tools::file_set_contents($pwfile, "$password\n", 0600);
sub delete_password {
my ($self) = @_;
- my $pwfile = password_file_name($self);
+ my $pwfile = $self->password_file_name();
unlink $pwfile;
};
sub get_password {
my ($self) = @_;
- my $pwfile = password_file_name($self);
+ my $pwfile = $self->password_file_name();
return PVE::Tools::file_read_firstline($pwfile);
}
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);
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;
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)) {
# This must live in the top scope to not get closed before the `run_command`
my $keyfd;
if ($use_crypto) {
- if (defined($keyfd = open_encryption_key($self))) {
+ if (defined($keyfd = $self->open_encryption_key())) {
my $flags = fcntl($keyfd, F_GETFD, 0)
// die "failed to get file descriptor flags: $!\n";
fcntl($keyfd, F_SETFD, $flags & ~FD_CLOEXEC)
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};
my sub run_raw_client_cmd {
my ($self, $client_cmd, $param, %opts) = @_;
- return do_raw_client_cmd($self, $client_cmd, $param, %opts);
+ return $self->do_raw_client_cmd($client_cmd, $param, %opts);
}
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');
+ $self->do_raw_client_cmd(
+ $client_cmd,
+ $param,
+ outfunc => $outfunc,
+ errmsg => 'proxmox-backup-client failed'
+ );
return undef if $no_output;
sub autogen_encryption_key {
my ($self) = @_;
- my $encfile = encryption_key_file_name($self);
+ my $encfile = $self->encryption_key_file_name();
run_command(['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile]);
};
push @$param, $opts->{group};
}
- return run_client_cmd($self, "snapshots", $param);
+ return $self->run_client_cmd("snapshots", $param);
};
sub backup_tree {
push @$param, '--backup-id', $id;
push @$param, '--backup-time', $time if defined($time);
- return run_raw_client_cmd($self, 'backup', $param, %$opts);
+ return $self->run_raw_client_cmd('backup', $param, %$opts);
};
sub restore_pxar {
push @$param, "$target";
push @$param, "--allow-existing-dirs", 0;
- return run_raw_client_cmd($self, 'restore', $param, %$opts);
+ return $self->run_raw_client_cmd('restore', $param, %$opts);
};
sub forget_snapshot {
push @$param, "$snapshot";
- return run_raw_client_cmd($self, 'forget', $param);
+ return $self->run_raw_client_cmd('forget', $param);
};
sub prune_group {
}
push @$param, "$group";
- return run_client_cmd($self, 'prune', $param);
+ return $self->run_client_cmd('prune', $param);
};
sub status {
my $active = 0;
eval {
- my $res = run_client_cmd($self, "status");
+ my $res = $self->run_client_cmd("status");
$active = 1;
$total = $res->{total};