]> git.proxmox.com Git - pve-common.git/blame - src/PVE/PBSClient.pm
pbs: autogen key: adapt recent changes in storage module
[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
SI
7use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
8use IO::File;
9use JSON;
10use POSIX qw(strftime ENOENT);
11
0904f388 12use PVE::JSONSchema qw(get_standard_option);
243568ca 13use PVE::Tools qw(run_command file_set_contents file_get_contents file_read_firstline);
0904f388
SI
14
15sub new {
16 my ($class, $scfg, $storeid, $sdir) = @_;
17
18 die "no section config provided\n" if ref($scfg) eq '';
19 die "undefined store id\n" if !defined($storeid);
20
21 my $secret_dir = $sdir // '/etc/pve/priv/storage';
22
243568ca
TL
23 my $self = bless {
24 scfg => $scfg,
25 storeid => $storeid,
26 secret_dir => $secret_dir
27 }, $class;
28 return $self;
0904f388
SI
29}
30
31my sub password_file_name {
32 my ($self) = @_;
33
34 return "$self->{secret_dir}/$self->{storeid}.pw";
35}
36
37sub set_password {
38 my ($self, $password) = @_;
39
243568ca 40 my $pwfile = $self->password_file_name();
0904f388
SI
41 mkdir $self->{secret_dir};
42
43 PVE::Tools::file_set_contents($pwfile, "$password\n", 0600);
44};
45
46sub delete_password {
47 my ($self) = @_;
48
243568ca 49 my $pwfile = $self->password_file_name();
0904f388
SI
50
51 unlink $pwfile;
52};
53
54sub get_password {
55 my ($self) = @_;
56
243568ca 57 my $pwfile = $self->password_file_name();
0904f388
SI
58
59 return PVE::Tools::file_read_firstline($pwfile);
60}
61
62sub encryption_key_file_name {
63 my ($self) = @_;
64
65 return "$self->{secret_dir}/$self->{storeid}.enc";
66};
67
68sub set_encryption_key {
69 my ($self, $key) = @_;
70
243568ca 71 my $encfile = $self->encryption_key_file_name();
0904f388
SI
72 mkdir $self->{secret_dir};
73
74 PVE::Tools::file_set_contents($encfile, "$key\n", 0600);
75};
76
77sub delete_encryption_key {
78 my ($self) = @_;
79
243568ca 80 my $encfile = $self->encryption_key_file_name();
0904f388
SI
81
82 if (!unlink $encfile) {
83 return if $! == ENOENT;
84 die "failed to delete encryption key! $!\n";
85 }
86};
87
88# Returns a file handle if there is an encryption key, or `undef` if there is not. Dies on error.
89my sub open_encryption_key {
90 my ($self) = @_;
91
243568ca 92 my $encryption_key_file = $self->encryption_key_file_name();
0904f388
SI
93
94 my $keyfd;
95 if (!open($keyfd, '<', $encryption_key_file)) {
96 return undef if $! == ENOENT;
97 die "failed to open encryption key: $encryption_key_file: $!\n";
98 }
99
100 return $keyfd;
101}
102
103my $USE_CRYPT_PARAMS = {
104 backup => 1,
105 restore => 1,
106 'upload-log' => 1,
107};
108
109my sub do_raw_client_cmd {
110 my ($self, $client_cmd, $param, %opts) = @_;
111
112 my $use_crypto = $USE_CRYPT_PARAMS->{$client_cmd};
113
114 my $client_exe = '/usr/bin/proxmox-backup-client';
115 die "executable not found '$client_exe'! Proxmox backup client not installed?\n"
116 if ! -x $client_exe;
117
118 my $scfg = $self->{scfg};
119 my $server = $scfg->{server};
120 my $datastore = $scfg->{datastore};
121 my $username = $scfg->{username} // 'root@pam';
122
123 my $userns_cmd = delete $opts{userns_cmd};
124
125 my $cmd = [];
126
127 push @$cmd, @$userns_cmd if defined($userns_cmd);
128
129 push @$cmd, $client_exe, $client_cmd;
130
131 # This must live in the top scope to not get closed before the `run_command`
132 my $keyfd;
133 if ($use_crypto) {
243568ca 134 if (defined($keyfd = $self->open_encryption_key())) {
0904f388
SI
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);
140 } else {
141 push @$cmd, '--crypt-mode=none';
142 }
143 }
144
145 push @$cmd, @$param if defined($param);
146
147 push @$cmd, "--repository", "$username\@$server:$datastore";
148
243568ca 149 local $ENV{PBS_PASSWORD} = $self->get_password();
0904f388
SI
150
151 local $ENV{PBS_FINGERPRINT} = $scfg->{fingerprint};
152
153 # no ascii-art on task logs
154 local $ENV{PROXMOX_OUTPUT_NO_BORDER} = 1;
155 local $ENV{PROXMOX_OUTPUT_NO_HEADER} = 1;
156
157 if (my $logfunc = $opts{logfunc}) {
158 $logfunc->("run: " . join(' ', @$cmd));
159 }
160
161 run_command($cmd, %opts);
162}
163
164my sub run_raw_client_cmd {
165 my ($self, $client_cmd, $param, %opts) = @_;
243568ca 166 return $self->do_raw_client_cmd($client_cmd, $param, %opts);
0904f388
SI
167}
168
169my sub run_client_cmd {
170 my ($self, $client_cmd, $param, $no_output) = @_;
171
172 my $json_str = '';
173 my $outfunc = sub { $json_str .= "$_[0]\n" };
174
175 $param = [] if !defined($param);
176 $param = [ $param ] if !ref($param);
177
178 $param = [@$param, '--output-format=json'] if !$no_output;
179
243568ca
TL
180 $self->do_raw_client_cmd(
181 $client_cmd,
182 $param,
183 outfunc => $outfunc,
184 errmsg => 'proxmox-backup-client failed'
185 );
0904f388
SI
186
187 return undef if $no_output;
188
189 my $res = decode_json($json_str);
190
191 return $res;
192}
193
194sub autogen_encryption_key {
195 my ($self) = @_;
243568ca 196 my $encfile = $self->encryption_key_file_name();
0cc6c7e0
TL
197 run_command(
198 ['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile],
199 errmsg => 'failed to create encryption key'
200 );
201 return file_get_contents($encfile);
0904f388
SI
202};
203
204sub get_snapshots {
205 my ($self, $opts) = @_;
206
207 my $param = [];
208 if (defined($opts->{group})) {
209 push @$param, $opts->{group};
210 }
211
243568ca 212 return $self->run_client_cmd("snapshots", $param);
0904f388
SI
213};
214
215sub backup_tree {
216 my ($self, $opts) = @_;
217
218 my $type = delete $opts->{type};
219 die "backup-type not provided\n" if !defined($type);
220 my $id = delete $opts->{id};
221 die "backup-id not provided\n" if !defined($id);
222 my $root = delete $opts->{root};
223 die "root dir not provided\n" if !defined($root);
224 my $pxarname = delete $opts->{pxarname};
225 die "archive name not provided\n" if !defined($pxarname);
226 my $time = delete $opts->{time};
227
228 my $param = [];
229
230 push @$param, "$pxarname.pxar:$root";
231 push @$param, '--backup-type', $type;
232 push @$param, '--backup-id', $id;
233 push @$param, '--backup-time', $time if defined($time);
234
243568ca 235 return $self->run_raw_client_cmd('backup', $param, %$opts);
0904f388
SI
236};
237
238sub restore_pxar {
239 my ($self, $opts) = @_;
240
241 my $snapshot = delete $opts->{snapshot};
242 die "snapshot not provided\n" if !defined($snapshot);
243 my $pxarname = delete $opts->{pxarname};
244 die "archive name not provided\n" if !defined($pxarname);
245 my $target = delete $opts->{target};
246 die "restore-target not provided\n" if !defined($target);
247 #my $time = delete $opts->{time};
248
249 my $param = [];
250
251 push @$param, "$snapshot";
252 push @$param, "$pxarname.pxar";
253 push @$param, "$target";
254 push @$param, "--allow-existing-dirs", 0;
255
243568ca 256 return $self->run_raw_client_cmd('restore', $param, %$opts);
0904f388
SI
257};
258
259sub forget_snapshot {
260 my ($self, $snapshot) = @_;
261
262 die "snapshot not provided\n" if !defined($snapshot);
263
264 my $param = [];
265
266 push @$param, "$snapshot";
267
243568ca 268 return $self->run_raw_client_cmd('forget', $param);
0904f388
SI
269};
270
271sub prune_group {
272 my ($self, $opts, $prune_opts, $group) = @_;
273
274 die "group not provided\n" if !defined($group);
275
276 # do nothing if no keep options specified for remote
277 return [] if scalar(keys %$prune_opts) == 0;
278
279 my $param = [];
280
281 push @$param, "--quiet";
282
283 if (defined($opts->{'dry-run'}) && $opts->{'dry-run'}) {
284 push @$param, "--dry-run", $opts->{'dry-run'};
285 }
286
287 foreach my $keep_opt (keys %$prune_opts) {
288 push @$param, "--$keep_opt", $prune_opts->{$keep_opt};
289 }
290 push @$param, "$group";
291
243568ca 292 return $self->run_client_cmd('prune', $param);
0904f388
SI
293};
294
295sub status {
296 my ($self) = @_;
297
298 my $total = 0;
299 my $free = 0;
300 my $used = 0;
301 my $active = 0;
302
303 eval {
243568ca 304 my $res = $self->run_client_cmd("status");
0904f388
SI
305
306 $active = 1;
307 $total = $res->{total};
308 $used = $res->{used};
309 $free = $res->{avail};
310 };
311 if (my $err = $@) {
312 warn $err;
313 }
314
315 return ($total, $free, $used, $active);
316};
317
3181;