]>
Commit | Line | Data |
---|---|---|
1 | package PVE::PBSClient; | |
2 | # utility functions for interaction with Proxmox Backup client CLI executable | |
3 | ||
4 | use strict; | |
5 | use warnings; | |
6 | ||
7 | use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC); | |
8 | use File::Temp qw(tempdir); | |
9 | use IO::File; | |
10 | use JSON; | |
11 | use POSIX qw(mkfifo strftime ENOENT); | |
12 | ||
13 | use PVE::JSONSchema qw(get_standard_option); | |
14 | use 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 | # } | |
24 | sub 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 | } | |
43 | ||
44 | sub 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 | ||
52 | my $self = bless { | |
53 | scfg => $scfg, | |
54 | storeid => $storeid, | |
55 | secret_dir => $secret_dir | |
56 | }, $class; | |
57 | return $self; | |
58 | } | |
59 | ||
60 | my sub password_file_name { | |
61 | my ($self) = @_; | |
62 | ||
63 | return "$self->{secret_dir}/$self->{storeid}.pw"; | |
64 | } | |
65 | ||
66 | sub set_password { | |
67 | my ($self, $password) = @_; | |
68 | ||
69 | my $pwfile = password_file_name($self); | |
70 | mkdir $self->{secret_dir}; | |
71 | ||
72 | PVE::Tools::file_set_contents($pwfile, "$password\n", 0600); | |
73 | }; | |
74 | ||
75 | sub delete_password { | |
76 | my ($self) = @_; | |
77 | ||
78 | my $pwfile = password_file_name($self); | |
79 | ||
80 | unlink $pwfile or die "deleting password file failed - $!\n"; | |
81 | }; | |
82 | ||
83 | sub get_password { | |
84 | my ($self) = @_; | |
85 | ||
86 | my $pwfile = password_file_name($self); | |
87 | ||
88 | return PVE::Tools::file_read_firstline($pwfile); | |
89 | } | |
90 | ||
91 | sub encryption_key_file_name { | |
92 | my ($self) = @_; | |
93 | ||
94 | return "$self->{secret_dir}/$self->{storeid}.enc"; | |
95 | }; | |
96 | ||
97 | sub set_encryption_key { | |
98 | my ($self, $key) = @_; | |
99 | ||
100 | my $encfile = $self->encryption_key_file_name(); | |
101 | mkdir $self->{secret_dir}; | |
102 | ||
103 | PVE::Tools::file_set_contents($encfile, "$key\n", 0600); | |
104 | }; | |
105 | ||
106 | sub delete_encryption_key { | |
107 | my ($self) = @_; | |
108 | ||
109 | my $encfile = $self->encryption_key_file_name(); | |
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. | |
118 | my sub open_encryption_key { | |
119 | my ($self) = @_; | |
120 | ||
121 | my $encryption_key_file = $self->encryption_key_file_name(); | |
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 | ||
132 | my $USE_CRYPT_PARAMS = { | |
133 | backup => 1, | |
134 | restore => 1, | |
135 | 'upload-log' => 1, | |
136 | list => 1, | |
137 | extract => 1, | |
138 | }; | |
139 | ||
140 | my sub do_raw_client_cmd { | |
141 | my ($self, $client_cmd, $param, %opts) = @_; | |
142 | ||
143 | my $client_bin = (delete $opts{binary}) || 'proxmox-backup-client'; | |
144 | my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd}; | |
145 | ||
146 | my $client_exe = "/usr/bin/$client_bin"; | |
147 | die "executable not found '$client_exe'! $client_bin not installed?\n" if ! -x $client_exe; | |
148 | ||
149 | my $scfg = $self->{scfg}; | |
150 | my $repo = get_repository($scfg); | |
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) { | |
163 | if (defined($keyfd = open_encryption_key($self))) { | |
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 | ||
176 | push @$cmd, "--repository", $repo; | |
177 | ||
178 | local $ENV{PBS_PASSWORD} = $self->get_password(); | |
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 | ||
193 | my sub run_raw_client_cmd { | |
194 | my ($self, $client_cmd, $param, %opts) = @_; | |
195 | return do_raw_client_cmd($self, $client_cmd, $param, %opts); | |
196 | } | |
197 | ||
198 | my sub run_client_cmd { | |
199 | my ($self, $client_cmd, $param, $no_output, $binary) = @_; | |
200 | ||
201 | my $json_str = ''; | |
202 | my $outfunc = sub { $json_str .= "$_[0]\n" }; | |
203 | ||
204 | $binary //= 'proxmox-backup-client'; | |
205 | ||
206 | $param = [] if !defined($param); | |
207 | $param = [ $param ] if !ref($param); | |
208 | ||
209 | $param = [@$param, '--output-format=json'] if !$no_output; | |
210 | ||
211 | do_raw_client_cmd( | |
212 | $self, | |
213 | $client_cmd, | |
214 | $param, | |
215 | outfunc => $outfunc, | |
216 | errmsg => "$binary failed", | |
217 | binary => $binary, | |
218 | ); | |
219 | ||
220 | return undef if $no_output; | |
221 | ||
222 | my $res = decode_json($json_str); | |
223 | ||
224 | return $res; | |
225 | } | |
226 | ||
227 | sub autogen_encryption_key { | |
228 | my ($self) = @_; | |
229 | my $encfile = $self->encryption_key_file_name(); | |
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); | |
235 | }; | |
236 | ||
237 | # lists all snapshots, optionally limited to a specific group | |
238 | sub get_snapshots { | |
239 | my ($self, $group) = @_; | |
240 | ||
241 | my $param = []; | |
242 | push @$param, $group if defined($group); | |
243 | ||
244 | return run_client_cmd($self, "snapshots", $param); | |
245 | }; | |
246 | ||
247 | # create a new PXAR backup of a FS directory tree - doesn't cross FS boundary | |
248 | # by default. | |
249 | sub backup_fs_tree { | |
250 | my ($self, $root, $id, $pxarname, $cmd_opts) = @_; | |
251 | ||
252 | die "backup-id not provided\n" if !defined($id); | |
253 | die "backup root dir not provided\n" if !defined($root); | |
254 | die "archive name not provided\n" if !defined($pxarname); | |
255 | ||
256 | my $param = [ | |
257 | "$pxarname.pxar:$root", | |
258 | '--backup-type', 'host', | |
259 | '--backup-id', $id, | |
260 | ]; | |
261 | ||
262 | $cmd_opts //= {}; | |
263 | ||
264 | return run_raw_client_cmd($self, 'backup', $param, %$cmd_opts); | |
265 | }; | |
266 | ||
267 | sub restore_pxar { | |
268 | my ($self, $snapshot, $pxarname, $target, $cmd_opts) = @_; | |
269 | ||
270 | die "snapshot not provided\n" if !defined($snapshot); | |
271 | die "archive name not provided\n" if !defined($pxarname); | |
272 | die "restore-target not provided\n" if !defined($target); | |
273 | ||
274 | my $param = [ | |
275 | "$snapshot", | |
276 | "$pxarname.pxar", | |
277 | "$target", | |
278 | "--allow-existing-dirs", 0, | |
279 | ]; | |
280 | $cmd_opts //= {}; | |
281 | ||
282 | return run_raw_client_cmd($self, 'restore', $param, %$cmd_opts); | |
283 | }; | |
284 | ||
285 | sub forget_snapshot { | |
286 | my ($self, $snapshot) = @_; | |
287 | ||
288 | die "snapshot not provided\n" if !defined($snapshot); | |
289 | ||
290 | return run_raw_client_cmd($self, 'forget', ["$snapshot"]); | |
291 | }; | |
292 | ||
293 | sub 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 | ||
314 | return run_client_cmd($self, 'prune', $param); | |
315 | }; | |
316 | ||
317 | sub status { | |
318 | my ($self) = @_; | |
319 | ||
320 | my $total = 0; | |
321 | my $free = 0; | |
322 | my $used = 0; | |
323 | my $active = 0; | |
324 | ||
325 | eval { | |
326 | my $res = run_client_cmd($self, "status"); | |
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 | ||
340 | sub 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 | ||
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 | |
353 | sub 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 | |
372 | sub 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 | ||
404 | 1; |