]> git.proxmox.com Git - pve-common.git/blame - src/PVE/PBSClient.pm
pbs: rework client exe handling and error message
[pve-common.git] / src / PVE / PBSClient.pm
CommitLineData
0904f388 1package PVE::PBSClient;
243568ca 2# utility functions for interaction with Proxmox Backup client CLI executable
0904f388
SI
3
4use strict;
5use warnings;
243568ca 6
0904f388 7use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
77e402f0 8use File::Temp qw(tempdir);
0904f388
SI
9use IO::File;
10use JSON;
77e402f0 11use POSIX qw(mkfifo strftime ENOENT);
0904f388 12
0904f388 13use PVE::JSONSchema qw(get_standard_option);
5640c3db
DC
14use PVE::Tools qw(run_command file_set_contents file_get_contents file_read_firstline $IPV6RE);
15
16# returns a repository string suitable for proxmox-backup-client, pbs-restore, etc.
17# $scfg must have the following structure:
18# {
19# datastore
20# server
21# port (optional defaults to 8007)
22# username (optional defaults to 'root@pam')
23# }
24sub get_repository {
25 my ($scfg) = @_;
26
27 my $server = $scfg->{server};
28 die "no server given\n" if !defined($server);
29
30 $server = "[$server]" if $server =~ /^$IPV6RE$/;
31
32 if (my $port = $scfg->{port}) {
33 $server .= ":$port" if $port != 8007;
34 }
35
36 my $datastore = $scfg->{datastore};
37 die "no datastore given\n" if !defined($datastore);
38
39 my $username = $scfg->{username} // 'root@pam';
40
41 return "$username\@$server:$datastore";
42}
0904f388
SI
43
44sub new {
45 my ($class, $scfg, $storeid, $sdir) = @_;
46
47 die "no section config provided\n" if ref($scfg) eq '';
48 die "undefined store id\n" if !defined($storeid);
49
50 my $secret_dir = $sdir // '/etc/pve/priv/storage';
51
243568ca
TL
52 my $self = bless {
53 scfg => $scfg,
54 storeid => $storeid,
55 secret_dir => $secret_dir
56 }, $class;
57 return $self;
0904f388
SI
58}
59
60my sub password_file_name {
61 my ($self) = @_;
62
63 return "$self->{secret_dir}/$self->{storeid}.pw";
64}
65
66sub set_password {
67 my ($self, $password) = @_;
68
69a3a585 69 my $pwfile = password_file_name($self);
0904f388
SI
70 mkdir $self->{secret_dir};
71
72 PVE::Tools::file_set_contents($pwfile, "$password\n", 0600);
73};
74
75sub delete_password {
76 my ($self) = @_;
77
69a3a585 78 my $pwfile = password_file_name($self);
0904f388 79
69a3a585 80 unlink $pwfile or die "deleting password file failed - $!\n";
0904f388
SI
81};
82
83sub get_password {
84 my ($self) = @_;
85
69a3a585 86 my $pwfile = password_file_name($self);
0904f388
SI
87
88 return PVE::Tools::file_read_firstline($pwfile);
89}
90
91sub encryption_key_file_name {
92 my ($self) = @_;
93
94 return "$self->{secret_dir}/$self->{storeid}.enc";
95};
96
97sub set_encryption_key {
98 my ($self, $key) = @_;
99
243568ca 100 my $encfile = $self->encryption_key_file_name();
0904f388
SI
101 mkdir $self->{secret_dir};
102
103 PVE::Tools::file_set_contents($encfile, "$key\n", 0600);
104};
105
106sub delete_encryption_key {
107 my ($self) = @_;
108
243568ca 109 my $encfile = $self->encryption_key_file_name();
0904f388
SI
110
111 if (!unlink $encfile) {
112 return if $! == ENOENT;
113 die "failed to delete encryption key! $!\n";
114 }
115};
116
117# Returns a file handle if there is an encryption key, or `undef` if there is not. Dies on error.
118my sub open_encryption_key {
119 my ($self) = @_;
120
243568ca 121 my $encryption_key_file = $self->encryption_key_file_name();
0904f388
SI
122
123 my $keyfd;
124 if (!open($keyfd, '<', $encryption_key_file)) {
125 return undef if $! == ENOENT;
126 die "failed to open encryption key: $encryption_key_file: $!\n";
127 }
128
129 return $keyfd;
130}
131
132my $USE_CRYPT_PARAMS = {
133 backup => 1,
134 restore => 1,
135 'upload-log' => 1,
f7c02541
SR
136 list => 1,
137 extract => 1,
0904f388
SI
138};
139
140my sub do_raw_client_cmd {
141 my ($self, $client_cmd, $param, %opts) = @_;
142
76ddb876 143 my $client_bin = (delete $opts{binary}) || 'proxmox-backup-client';
0904f388
SI
144 my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd};
145
76ddb876
TL
146 my $client_exe = "/usr/bin/$client_bin";
147 die "executable not found '$client_exe'! $client_bin not installed?\n" if ! -x $client_exe;
0904f388
SI
148
149 my $scfg = $self->{scfg};
5640c3db 150 my $repo = get_repository($scfg);
0904f388
SI
151
152 my $userns_cmd = delete $opts{userns_cmd};
153
154 my $cmd = [];
155
156 push @$cmd, @$userns_cmd if defined($userns_cmd);
157
158 push @$cmd, $client_exe, $client_cmd;
159
160 # This must live in the top scope to not get closed before the `run_command`
161 my $keyfd;
162 if ($use_crypto) {
69a3a585 163 if (defined($keyfd = open_encryption_key($self))) {
0904f388
SI
164 my $flags = fcntl($keyfd, F_GETFD, 0)
165 // die "failed to get file descriptor flags: $!\n";
166 fcntl($keyfd, F_SETFD, $flags & ~FD_CLOEXEC)
167 or die "failed to remove FD_CLOEXEC from encryption key file descriptor\n";
168 push @$cmd, '--crypt-mode=encrypt', '--keyfd='.fileno($keyfd);
169 } else {
170 push @$cmd, '--crypt-mode=none';
171 }
172 }
173
174 push @$cmd, @$param if defined($param);
175
5640c3db 176 push @$cmd, "--repository", $repo;
0904f388 177
243568ca 178 local $ENV{PBS_PASSWORD} = $self->get_password();
0904f388
SI
179
180 local $ENV{PBS_FINGERPRINT} = $scfg->{fingerprint};
181
182 # no ascii-art on task logs
183 local $ENV{PROXMOX_OUTPUT_NO_BORDER} = 1;
184 local $ENV{PROXMOX_OUTPUT_NO_HEADER} = 1;
185
186 if (my $logfunc = $opts{logfunc}) {
187 $logfunc->("run: " . join(' ', @$cmd));
188 }
189
190 run_command($cmd, %opts);
191}
192
193my sub run_raw_client_cmd {
194 my ($self, $client_cmd, $param, %opts) = @_;
69a3a585 195 return do_raw_client_cmd($self, $client_cmd, $param, %opts);
0904f388
SI
196}
197
198my sub run_client_cmd {
b15abdfe 199 my ($self, $client_cmd, $param, $no_output, $binary) = @_;
0904f388
SI
200
201 my $json_str = '';
202 my $outfunc = sub { $json_str .= "$_[0]\n" };
203
b15abdfe
SR
204 $binary //= 'proxmox-backup-client';
205
0904f388
SI
206 $param = [] if !defined($param);
207 $param = [ $param ] if !ref($param);
208
209 $param = [@$param, '--output-format=json'] if !$no_output;
210
69a3a585 211 do_raw_client_cmd(
b15abdfe
SR
212 $self,
213 $client_cmd,
214 $param,
215 outfunc => $outfunc,
216 errmsg => "$binary failed",
217 binary => $binary,
243568ca 218 );
0904f388
SI
219
220 return undef if $no_output;
221
222 my $res = decode_json($json_str);
223
224 return $res;
225}
226
227sub autogen_encryption_key {
228 my ($self) = @_;
243568ca 229 my $encfile = $self->encryption_key_file_name();
0cc6c7e0
TL
230 run_command(
231 ['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile],
232 errmsg => 'failed to create encryption key'
233 );
234 return file_get_contents($encfile);
0904f388
SI
235};
236
2113c7e8 237# lists all snapshots, optionally limited to a specific group
0904f388 238sub get_snapshots {
2113c7e8 239 my ($self, $group) = @_;
0904f388
SI
240
241 my $param = [];
2113c7e8 242 push @$param, $group if defined($group);
0904f388 243
69a3a585 244 return run_client_cmd($self, "snapshots", $param);
0904f388
SI
245};
246
6674eb1e
TL
247# create a new PXAR backup of a FS directory tree - doesn't cross FS boundary
248# by default.
249sub backup_fs_tree {
250 my ($self, $root, $id, $pxarname, $cmd_opts) = @_;
0904f388 251
0904f388 252 die "backup-id not provided\n" if !defined($id);
6674eb1e 253 die "backup root dir not provided\n" if !defined($root);
0904f388 254 die "archive name not provided\n" if !defined($pxarname);
0904f388 255
ad6b3237
TL
256 my $param = [
257 "$pxarname.pxar:$root",
6674eb1e 258 '--backup-type', 'host',
ad6b3237
TL
259 '--backup-id', $id,
260 ];
0904f388 261
6674eb1e
TL
262 $cmd_opts //= {};
263
264 return run_raw_client_cmd($self, 'backup', $param, %$cmd_opts);
0904f388
SI
265};
266
267sub restore_pxar {
8b88b2f6 268 my ($self, $snapshot, $pxarname, $target, $cmd_opts) = @_;
0904f388 269
0904f388 270 die "snapshot not provided\n" if !defined($snapshot);
0904f388 271 die "archive name not provided\n" if !defined($pxarname);
0904f388 272 die "restore-target not provided\n" if !defined($target);
0904f388 273
ad6b3237
TL
274 my $param = [
275 "$snapshot",
276 "$pxarname.pxar",
277 "$target",
278 "--allow-existing-dirs", 0,
279 ];
8b88b2f6 280 $cmd_opts //= {};
0904f388 281
69a3a585 282 return run_raw_client_cmd($self, 'restore', $param, %$cmd_opts);
0904f388
SI
283};
284
285sub forget_snapshot {
286 my ($self, $snapshot) = @_;
287
288 die "snapshot not provided\n" if !defined($snapshot);
289
69a3a585 290 return run_raw_client_cmd($self, 'forget', ["$snapshot"]);
0904f388
SI
291};
292
293sub prune_group {
294 my ($self, $opts, $prune_opts, $group) = @_;
295
296 die "group not provided\n" if !defined($group);
297
298 # do nothing if no keep options specified for remote
299 return [] if scalar(keys %$prune_opts) == 0;
300
301 my $param = [];
302
303 push @$param, "--quiet";
304
305 if (defined($opts->{'dry-run'}) && $opts->{'dry-run'}) {
306 push @$param, "--dry-run", $opts->{'dry-run'};
307 }
308
309 foreach my $keep_opt (keys %$prune_opts) {
310 push @$param, "--$keep_opt", $prune_opts->{$keep_opt};
311 }
312 push @$param, "$group";
313
69a3a585 314 return run_client_cmd($self, 'prune', $param);
0904f388
SI
315};
316
317sub status {
318 my ($self) = @_;
319
320 my $total = 0;
321 my $free = 0;
322 my $used = 0;
323 my $active = 0;
324
325 eval {
69a3a585 326 my $res = run_client_cmd($self, "status");
0904f388
SI
327
328 $active = 1;
329 $total = $res->{total};
330 $used = $res->{used};
331 $free = $res->{avail};
332 };
333 if (my $err = $@) {
334 warn $err;
335 }
336
337 return ($total, $free, $used, $active);
338};
339
67252649
SR
340sub file_restore_list {
341 my ($self, $snapshot, $filepath, $base64) = @_;
342 return run_client_cmd(
343 $self,
344 "list",
345 [ $snapshot, $filepath, "--base64", $base64 ? 1 : 0 ],
346 0,
347 "proxmox-file-restore",
348 );
349}
350
77e402f0
SR
351# call sync from API, returns a fifo path for streaming data to clients,
352# pass it to file_restore_extract to start transfering data
353sub file_restore_extract_prepare {
354 my ($self) = @_;
355
356 my $tmpdir = tempdir();
357 mkfifo("$tmpdir/fifo", 0600)
358 or die "creating file download fifo '$tmpdir/fifo' failed: $!\n";
359
360 # allow reading data for proxy user
361 my $wwwid = getpwnam('www-data') ||
362 die "getpwnam failed";
363 chown $wwwid, -1, "$tmpdir"
364 or die "changing permission on fifo dir '$tmpdir' failed: $!\n";
365 chown $wwwid, -1, "$tmpdir/fifo"
366 or die "changing permission on fifo '$tmpdir/fifo' failed: $!\n";
367
368 return "$tmpdir/fifo";
369}
370
371# this blocks while data is transfered, call this from a background worker
372sub file_restore_extract {
373 my ($self, $output_file, $snapshot, $filepath, $base64) = @_;
374
375 my $ret = eval {
376 local $SIG{ALRM} = sub { die "got timeout\n" };
377 alarm(30);
378 sysopen(my $fh, "$output_file", O_WRONLY)
379 or die "open target '$output_file' for writing failed: $!\n";
380 alarm(0);
381
382 my $fn = fileno($fh);
383 my $errfunc = sub { print $_[0], "\n"; };
384
385 return run_raw_client_cmd(
386 $self,
387 "extract",
388 [ $snapshot, $filepath, "-", "--base64", $base64 ? 1 : 0 ],
389 binary => "proxmox-file-restore",
390 errfunc => $errfunc,
391 output => ">&$fn",
392 );
393 };
394 my $err = $@;
395
396 unlink($output_file);
397 $output_file =~ s/fifo$//;
398 rmdir($output_file) if -d $output_file;
399
400 die "file restore task failed: $err" if $err;
401 return $ret;
402}
403
0904f388 4041;