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);
8 use File
::Temp
qw(tempdir);
11 use POSIX
qw(mkfifo strftime ENOENT);
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);
16 # returns a repository string suitable for proxmox-backup-client, pbs-restore, etc.
17 # $scfg must have the following structure:
21 # port (optional defaults to 8007)
22 # username (optional defaults to 'root@pam')
27 my $server = $scfg->{server
};
28 die "no server given\n" if !defined($server);
30 $server = "[$server]" if $server =~ /^$IPV6RE$/;
32 if (my $port = $scfg->{port
}) {
33 $server .= ":$port" if $port != 8007;
36 my $datastore = $scfg->{datastore
};
37 die "no datastore given\n" if !defined($datastore);
39 my $username = $scfg->{username
} // 'root@pam';
41 return "$username\@$server:$datastore";
45 my ($class, $scfg, $storeid, $sdir) = @_;
47 die "no section config provided\n" if ref($scfg) eq '';
48 die "undefined store id\n" if !defined($storeid);
50 my $secret_dir = $sdir // '/etc/pve/priv/storage';
55 secret_dir
=> $secret_dir
60 my sub password_file_name
{
63 return "$self->{secret_dir}/$self->{storeid}.pw";
67 my ($self, $password) = @_;
69 my $pwfile = password_file_name
($self);
70 mkdir $self->{secret_dir
};
72 PVE
::Tools
::file_set_contents
($pwfile, "$password\n", 0600);
78 my $pwfile = password_file_name
($self);
80 unlink $pwfile or die "deleting password file failed - $!\n";
86 my $pwfile = password_file_name
($self);
88 return PVE
::Tools
::file_read_firstline
($pwfile);
91 sub encryption_key_file_name
{
94 return "$self->{secret_dir}/$self->{storeid}.enc";
97 sub set_encryption_key
{
98 my ($self, $key) = @_;
100 my $encfile = $self->encryption_key_file_name();
101 mkdir $self->{secret_dir
};
103 PVE
::Tools
::file_set_contents
($encfile, "$key\n", 0600);
106 sub delete_encryption_key
{
109 my $encfile = $self->encryption_key_file_name();
111 if (!unlink $encfile) {
112 return if $! == ENOENT
;
113 die "failed to delete encryption key! $!\n";
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
{
121 my $encryption_key_file = $self->encryption_key_file_name();
124 if (!open($keyfd, '<', $encryption_key_file)) {
125 return undef if $! == ENOENT
;
126 die "failed to open encryption key: $encryption_key_file: $!\n";
132 my $USE_CRYPT_PARAMS = {
140 my sub do_raw_client_cmd
{
141 my ($self, $client_cmd, $param, %opts) = @_;
143 my $client_bin = (delete $opts{binary
}) || 'proxmox-backup-client';
144 my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd};
146 my $client_exe = "/usr/bin/$client_bin";
147 die "executable not found '$client_exe'! $client_bin not installed?\n" if ! -x
$client_exe;
149 my $scfg = $self->{scfg
};
150 my $repo = get_repository
($scfg);
152 my $userns_cmd = delete $opts{userns_cmd
};
156 push @$cmd, @$userns_cmd if defined($userns_cmd);
158 push @$cmd, $client_exe, $client_cmd;
160 # This must live in the top scope to not get closed before the `run_command`
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);
170 push @$cmd, '--crypt-mode=none';
174 push @$cmd, @$param if defined($param);
176 push @$cmd, "--repository", $repo;
178 local $ENV{PBS_PASSWORD
} = $self->get_password();
180 local $ENV{PBS_FINGERPRINT
} = $scfg->{fingerprint
};
182 # no ascii-art on task logs
183 local $ENV{PROXMOX_OUTPUT_NO_BORDER
} = 1;
184 local $ENV{PROXMOX_OUTPUT_NO_HEADER
} = 1;
186 if (my $logfunc = $opts{logfunc
}) {
187 $logfunc->("run: " . join(' ', @$cmd));
190 run_command
($cmd, %opts);
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);
198 my sub run_client_cmd
{
199 my ($self, $client_cmd, $param, $no_output, $binary) = @_;
202 my $outfunc = sub { $json_str .= "$_[0]\n" };
204 $binary //= 'proxmox-backup-client';
206 $param = [] if !defined($param);
207 $param = [ $param ] if !ref($param);
209 $param = [@$param, '--output-format=json'] if !$no_output;
216 errmsg
=> "$binary failed",
220 return undef if $no_output;
222 my $res = decode_json
($json_str);
227 sub autogen_encryption_key
{
229 my $encfile = $self->encryption_key_file_name();
231 ['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile],
232 errmsg
=> 'failed to create encryption key'
234 return file_get_contents
($encfile);
237 # lists all snapshots, optionally limited to a specific group
239 my ($self, $group) = @_;
242 push @$param, $group if defined($group);
244 return run_client_cmd
($self, "snapshots", $param);
247 # create a new PXAR backup of a FS directory tree - doesn't cross FS boundary
250 my ($self, $root, $id, $pxarname, $cmd_opts) = @_;
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);
257 "$pxarname.pxar:$root",
258 '--backup-type', 'host',
264 return run_raw_client_cmd
($self, 'backup', $param, %$cmd_opts);
268 my ($self, $snapshot, $pxarname, $target, $cmd_opts) = @_;
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);
278 "--allow-existing-dirs", 0,
282 return run_raw_client_cmd
($self, 'restore', $param, %$cmd_opts);
285 sub forget_snapshot
{
286 my ($self, $snapshot) = @_;
288 die "snapshot not provided\n" if !defined($snapshot);
290 return run_raw_client_cmd
($self, 'forget', ["$snapshot"]);
294 my ($self, $opts, $prune_opts, $group) = @_;
296 die "group not provided\n" if !defined($group);
298 # do nothing if no keep options specified for remote
299 return [] if scalar(keys %$prune_opts) == 0;
303 push @$param, "--quiet";
305 if (defined($opts->{'dry-run'}) && $opts->{'dry-run'}) {
306 push @$param, "--dry-run", $opts->{'dry-run'};
309 foreach my $keep_opt (keys %$prune_opts) {
310 push @$param, "--$keep_opt", $prune_opts->{$keep_opt};
312 push @$param, "$group";
314 return run_client_cmd
($self, 'prune', $param);
326 my $res = run_client_cmd
($self, "status");
329 $total = $res->{total
};
330 $used = $res->{used
};
331 $free = $res->{avail
};
337 return ($total, $free, $used, $active);
340 sub file_restore_list
{
341 my ($self, $snapshot, $filepath, $base64) = @_;
342 return run_client_cmd
(
345 [ $snapshot, $filepath, "--base64", $base64 ?
1 : 0 ],
347 "proxmox-file-restore",
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
{
356 my $tmpdir = tempdir
();
357 mkfifo
("$tmpdir/fifo", 0600)
358 or die "creating file download fifo '$tmpdir/fifo' failed: $!\n";
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";
368 return "$tmpdir/fifo";
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) = @_;
376 local $SIG{ALRM
} = sub { die "got timeout\n" };
378 sysopen(my $fh, "$output_file", O_WRONLY
)
379 or die "open target '$output_file' for writing failed: $!\n";
382 my $fn = fileno($fh);
383 my $errfunc = sub { print $_[0], "\n"; };
385 return run_raw_client_cmd
(
388 [ $snapshot, $filepath, "-", "--base64", $base64 ?
1 : 0 ],
389 binary
=> "proxmox-file-restore",
396 unlink($output_file);
397 $output_file =~ s/fifo$//;
398 rmdir($output_file) if -d
$output_file;
400 die "file restore task failed: $err" if $err;