]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/VZDump.pm
13b6cd4666f1852815aa802568190370883d7e9d
[pve-manager.git] / PVE / API2 / VZDump.pm
1 package PVE::API2::VZDump;
2
3 use strict;
4 use warnings;
5 use PVE::Exception qw(raise_param_exc);
6 use PVE::Tools qw(extract_param);
7 use PVE::Cluster qw(cfs_register_file cfs_read_file);
8 use PVE::INotify;
9 use PVE::RPCEnvironment;
10 use PVE::AccessControl;
11 use PVE::JSONSchema qw(get_standard_option);
12 use PVE::Storage;
13 use PVE::VZDump;
14 use PVE::VZDump::Common;
15 use PVE::API2Tools;
16
17 use Data::Dumper; # fixme: remove
18
19
20 use base qw(PVE::RESTHandler);
21
22 __PACKAGE__->register_method ({
23 name => 'vzdump',
24 path => '',
25 method => 'POST',
26 description => "Create backup.",
27 permissions => {
28 description => "The user needs 'VM.Backup' permissions on any VM, and 'Datastore.AllocateSpace'"
29 ." on the backup storage. The 'maxfiles', 'prune-backups', 'tmpdir', 'dumpdir', 'script',"
30 ." 'bwlimit' and 'ionice' parameters are restricted to the 'root\@pam' user.",
31 user => 'all',
32 },
33 protected => 1,
34 proxyto => 'node',
35 parameters => {
36 additionalProperties => 0,
37 properties => PVE::VZDump::Common::json_config_properties({
38 stdout => {
39 type => 'boolean',
40 description => "Write tar to stdout, not to a file.",
41 optional => 1,
42 },
43 }),
44 },
45 returns => { type => 'string' },
46 code => sub {
47 my ($param) = @_;
48
49 my $rpcenv = PVE::RPCEnvironment::get();
50
51 my $user = $rpcenv->get_user();
52
53 my $nodename = PVE::INotify::nodename();
54
55 if ($rpcenv->{type} ne 'cli') {
56 raise_param_exc({ node => "option is only allowed on the command line interface."})
57 if $param->{node} && $param->{node} ne $nodename;
58
59 raise_param_exc({ stdout => "option is only allowed on the command line interface."})
60 if $param->{stdout};
61 }
62
63 foreach my $key (qw(maxfiles prune-backups tmpdir dumpdir script bwlimit ionice)) {
64 raise_param_exc({ $key => "Only root may set this option."})
65 if defined($param->{$key}) && ($user ne 'root@pam');
66 }
67
68 PVE::VZDump::verify_vzdump_parameters($param, 1);
69
70 # silent exit if we run on wrong node
71 return 'OK' if $param->{node} && $param->{node} ne $nodename;
72
73 my $cmdline = PVE::VZDump::Common::command_line($param);
74
75 my $vmids_per_node = PVE::VZDump::get_included_guests($param);
76
77 my $local_vmids = delete $vmids_per_node->{$nodename} // [];
78
79 # include IDs for deleted guests, and visibly fail later
80 my $orphaned_vmids = delete $vmids_per_node->{''} // [];
81 push @{$local_vmids}, @{$orphaned_vmids};
82
83 my $skiplist = [ map { @$_ } values $vmids_per_node->%* ];
84
85 if($param->{stop}){
86 PVE::VZDump::stop_running_backups();
87 return 'OK' if !scalar(@{$local_vmids});
88 }
89
90 # silent exit if specified VMs run on other nodes
91 return "OK" if !scalar(@{$local_vmids}) && !$param->{all};
92
93 PVE::VZDump::parse_mailto_exclude_path($param);
94
95 die "you can only backup a single VM with option --stdout\n"
96 if $param->{stdout} && scalar(@{$local_vmids}) != 1;
97
98 $rpcenv->check($user, "/storage/$param->{storage}", [ 'Datastore.AllocateSpace' ])
99 if $param->{storage};
100
101 my $worker = sub {
102 my $upid = shift;
103
104 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
105 die "interrupted by signal\n";
106 };
107
108 $param->{vmids} = $local_vmids;
109 my $vzdump = PVE::VZDump->new($cmdline, $param, $skiplist);
110
111 my $LOCK_FH = eval {
112 $vzdump->getlock($upid); # only one process allowed
113 };
114 if (my $err = $@) {
115 $vzdump->sendmail([], 0, $err);
116 exit(-1);
117 }
118
119 if (defined($param->{ionice})) {
120 if ($param->{ionice} > 7) {
121 PVE::VZDump::run_command(undef, "ionice -c3 -p $$");
122 } else {
123 PVE::VZDump::run_command(undef, "ionice -c2 -n$param->{ionice} -p $$");
124 }
125 }
126 $vzdump->exec_backup($rpcenv, $user);
127
128 close($LOCK_FH);
129 };
130
131 open STDOUT, '>/dev/null' if $param->{quiet} && !$param->{stdout};
132 open STDERR, '>/dev/null' if $param->{quiet};
133
134 if ($rpcenv->{type} eq 'cli') {
135 if ($param->{stdout}) {
136
137 open my $saved_stdout, ">&STDOUT"
138 || die "can't dup STDOUT: $!\n";
139
140 open STDOUT, '>&STDERR' ||
141 die "unable to redirect STDOUT: $!\n";
142
143 $param->{stdout} = $saved_stdout;
144 }
145 }
146
147 my $taskid;
148 $taskid = $local_vmids->[0] if scalar(@{$local_vmids}) == 1;
149
150 return $rpcenv->fork_worker('vzdump', $taskid, $user, $worker);
151 }});
152
153 __PACKAGE__->register_method ({
154 name => 'defaults',
155 path => 'defaults',
156 method => 'GET',
157 description => "Get the currently configured vzdump defaults.",
158 permissions => {
159 description => "The user needs 'Datastore.Audit' or 'Datastore.AllocateSpace' " .
160 "permissions for the specified storage (or default storage if none specified). Some " .
161 "properties are only returned when the user has 'Sys.Audit' permissions for the node.",
162 user => 'all',
163 },
164 proxyto => 'node',
165 parameters => {
166 additionalProperties => 0,
167 properties => {
168 node => get_standard_option('pve-node'),
169 storage => get_standard_option('pve-storage-id', { optional => 1 }),
170 },
171 },
172 returns => {
173 type => 'object',
174 additionalProperties => 0,
175 properties => PVE::VZDump::Common::json_config_properties(),
176 },
177 code => sub {
178 my ($param) = @_;
179
180 my $node = extract_param($param, 'node');
181 my $storage = extract_param($param, 'storage');
182
183 my $rpcenv = PVE::RPCEnvironment::get();
184 my $authuser = $rpcenv->get_user();
185
186 my $res = PVE::VZDump::read_vzdump_defaults();
187
188 $res->{storage} = $storage if defined($storage);
189
190 if (!defined($res->{dumpdir}) && !defined($res->{storage})) {
191 $res->{storage} = 'local';
192 }
193
194 if (defined($res->{storage})) {
195 $rpcenv->check_any(
196 $authuser,
197 "/storage/$res->{storage}",
198 ['Datastore.Audit', 'Datastore.AllocateSpace'],
199 );
200
201 my $info = PVE::VZDump::storage_info($res->{storage});
202 for my $key (qw(dumpdir prune-backups)) {
203 $res->{$key} = $info->{$key} if defined($info->{$key});
204 }
205 }
206
207 if (defined($res->{'prune-backups'})) {
208 $res->{'prune-backups'} = PVE::JSONSchema::print_property_string(
209 $res->{'prune-backups'},
210 'prune-backups',
211 );
212 }
213
214 $res->{mailto} = join(",", @{$res->{mailto}})
215 if defined($res->{mailto});
216
217 $res->{'exclude-path'} = join(",", @{$res->{'exclude-path'}})
218 if defined($res->{'exclude-path'});
219
220 # normal backup users don't need to know these
221 if (!$rpcenv->check($authuser, "/nodes/$node", ['Sys.Audit'], 1)) {
222 delete $res->{mailto};
223 delete $res->{tmpdir};
224 delete $res->{dumpdir};
225 delete $res->{script};
226 delete $res->{ionice};
227 }
228
229 my $pool = $res->{pool};
230 if (defined($pool) &&
231 !$rpcenv->check($authuser, "/pool/$pool", ['Pool.Audit'], 1)) {
232 delete $res->{pool};
233 }
234
235 return $res;
236 }});
237
238 __PACKAGE__->register_method ({
239 name => 'extractconfig',
240 path => 'extractconfig',
241 method => 'GET',
242 description => "Extract configuration from vzdump backup archive.",
243 permissions => {
244 description => "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
245 user => 'all',
246 },
247 protected => 1,
248 proxyto => 'node',
249 parameters => {
250 additionalProperties => 0,
251 properties => {
252 node => get_standard_option('pve-node'),
253 volume => {
254 description => "Volume identifier",
255 type => 'string',
256 completion => \&PVE::Storage::complete_volume,
257 },
258 },
259 },
260 returns => { type => 'string' },
261 code => sub {
262 my ($param) = @_;
263
264 my $volume = extract_param($param, 'volume');
265
266 my $rpcenv = PVE::RPCEnvironment::get();
267 my $authuser = $rpcenv->get_user();
268
269 my $storage_cfg = PVE::Storage::config();
270 PVE::Storage::check_volume_access(
271 $rpcenv,
272 $authuser,
273 $storage_cfg,
274 undef,
275 $volume,
276 'backup',
277 );
278
279 if (PVE::Storage::parse_volume_id($volume, 1)) {
280 my (undef, undef, $ownervm) = PVE::Storage::parse_volname($storage_cfg, $volume);
281 $rpcenv->check($authuser, "/vms/$ownervm", ['VM.Backup']);
282 }
283
284 return PVE::Storage::extract_vzdump_config($storage_cfg, $volume);
285 }});
286
287 1;