]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/PBSClient.pm
1 package PVE
::PBSClient
;
2 # utility functions for interaction with Proxmox Backup client CLI executable
7 use Fcntl
qw(F_GETFD F_SETFD FD_CLOEXEC);
10 use POSIX
qw(strftime ENOENT);
12 use PVE
::JSONSchema
qw(get_standard_option);
13 use PVE
::Tools
qw(run_command file_set_contents file_get_contents file_read_firstline);
16 my ($class, $scfg, $storeid, $sdir) = @_;
18 die "no section config provided\n" if ref($scfg) eq '';
19 die "undefined store id\n" if !defined($storeid);
21 my $secret_dir = $sdir // '/etc/pve/priv/storage';
26 secret_dir
=> $secret_dir
31 my sub password_file_name
{
34 return "$self->{secret_dir}/$self->{storeid}.pw";
38 my ($self, $password) = @_;
40 my $pwfile = password_file_name
($self);
41 mkdir $self->{secret_dir
};
43 PVE
::Tools
::file_set_contents
($pwfile, "$password\n", 0600);
49 my $pwfile = password_file_name
($self);
51 unlink $pwfile or die "deleting password file failed - $!\n";
57 my $pwfile = password_file_name
($self);
59 return PVE
::Tools
::file_read_firstline
($pwfile);
62 sub encryption_key_file_name
{
65 return "$self->{secret_dir}/$self->{storeid}.enc";
68 sub set_encryption_key
{
69 my ($self, $key) = @_;
71 my $encfile = $self->encryption_key_file_name();
72 mkdir $self->{secret_dir
};
74 PVE
::Tools
::file_set_contents
($encfile, "$key\n", 0600);
77 sub delete_encryption_key
{
80 my $encfile = $self->encryption_key_file_name();
82 if (!unlink $encfile) {
83 return if $! == ENOENT
;
84 die "failed to delete encryption key! $!\n";
88 # Returns a file handle if there is an encryption key, or `undef` if there is not. Dies on error.
89 my sub open_encryption_key
{
92 my $encryption_key_file = $self->encryption_key_file_name();
95 if (!open($keyfd, '<', $encryption_key_file)) {
96 return undef if $! == ENOENT
;
97 die "failed to open encryption key: $encryption_key_file: $!\n";
103 my $USE_CRYPT_PARAMS = {
109 my sub do_raw_client_cmd
{
110 my ($self, $client_cmd, $param, %opts) = @_;
112 my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd};
114 my $client_exe = '/usr/bin/proxmox-backup-client';
115 die "executable not found '$client_exe'! Proxmox backup client not installed?\n"
118 my $scfg = $self->{scfg
};
119 my $server = $scfg->{server
};
120 my $datastore = $scfg->{datastore
};
121 my $username = $scfg->{username
} // 'root@pam';
123 my $userns_cmd = delete $opts{userns_cmd
};
127 push @$cmd, @$userns_cmd if defined($userns_cmd);
129 push @$cmd, $client_exe, $client_cmd;
131 # This must live in the top scope to not get closed before the `run_command`
134 if (defined($keyfd = open_encryption_key
($self))) {
135 my $flags = fcntl($keyfd, F_GETFD
, 0)
136 // die "failed to get file descriptor flags: $!\n";
137 fcntl($keyfd, F_SETFD
, $flags & ~FD_CLOEXEC
)
138 or die "failed to remove FD_CLOEXEC from encryption key file descriptor\n";
139 push @$cmd, '--crypt-mode=encrypt', '--keyfd='.fileno($keyfd);
141 push @$cmd, '--crypt-mode=none';
145 push @$cmd, @$param if defined($param);
147 push @$cmd, "--repository", "$username\@$server:$datastore";
149 local $ENV{PBS_PASSWORD
} = $self->get_password();
151 local $ENV{PBS_FINGERPRINT
} = $scfg->{fingerprint
};
153 # no ascii-art on task logs
154 local $ENV{PROXMOX_OUTPUT_NO_BORDER
} = 1;
155 local $ENV{PROXMOX_OUTPUT_NO_HEADER
} = 1;
157 if (my $logfunc = $opts{logfunc
}) {
158 $logfunc->("run: " . join(' ', @$cmd));
161 run_command
($cmd, %opts);
164 my sub run_raw_client_cmd
{
165 my ($self, $client_cmd, $param, %opts) = @_;
166 return do_raw_client_cmd
($self, $client_cmd, $param, %opts);
169 my sub run_client_cmd
{
170 my ($self, $client_cmd, $param, $no_output) = @_;
173 my $outfunc = sub { $json_str .= "$_[0]\n" };
175 $param = [] if !defined($param);
176 $param = [ $param ] if !ref($param);
178 $param = [@$param, '--output-format=json'] if !$no_output;
185 errmsg
=> 'proxmox-backup-client failed'
188 return undef if $no_output;
190 my $res = decode_json
($json_str);
195 sub autogen_encryption_key
{
197 my $encfile = $self->encryption_key_file_name();
199 ['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile],
200 errmsg
=> 'failed to create encryption key'
202 return file_get_contents
($encfile);
205 # lists all snapshots, optionally limited to a specific group
207 my ($self, $group) = @_;
210 push @$param, $group if defined($group);
212 return run_client_cmd
($self, "snapshots", $param);
215 # create a new PXAR backup of a FS directory tree - doesn't cross FS boundary
218 my ($self, $root, $id, $pxarname, $cmd_opts) = @_;
220 die "backup-id not provided\n" if !defined($id);
221 die "backup root dir not provided\n" if !defined($root);
222 die "archive name not provided\n" if !defined($pxarname);
225 "$pxarname.pxar:$root",
226 '--backup-type', 'host',
232 return run_raw_client_cmd
($self, 'backup', $param, %$cmd_opts);
236 my ($self, $snapshot, $pxarname, $target, $cmd_opts) = @_;
238 die "snapshot not provided\n" if !defined($snapshot);
239 die "archive name not provided\n" if !defined($pxarname);
240 die "restore-target not provided\n" if !defined($target);
246 "--allow-existing-dirs", 0,
250 return run_raw_client_cmd
($self, 'restore', $param, %$cmd_opts);
253 sub forget_snapshot
{
254 my ($self, $snapshot) = @_;
256 die "snapshot not provided\n" if !defined($snapshot);
258 return run_raw_client_cmd
($self, 'forget', ["$snapshot"]);
262 my ($self, $opts, $prune_opts, $group) = @_;
264 die "group not provided\n" if !defined($group);
266 # do nothing if no keep options specified for remote
267 return [] if scalar(keys %$prune_opts) == 0;
271 push @$param, "--quiet";
273 if (defined($opts->{'dry-run'}) && $opts->{'dry-run'}) {
274 push @$param, "--dry-run", $opts->{'dry-run'};
277 foreach my $keep_opt (keys %$prune_opts) {
278 push @$param, "--$keep_opt", $prune_opts->{$keep_opt};
280 push @$param, "$group";
282 return run_client_cmd
($self, 'prune', $param);
294 my $res = run_client_cmd
($self, "status");
297 $total = $res->{total
};
298 $used = $res->{used
};
299 $free = $res->{avail
};
305 return ($total, $free, $used, $active);