]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/VZDump.pm
update shipped appliance info index
[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' on the backup storage. The 'maxfiles', 'tmpdir', 'dumpdir', 'script', 'bwlimit' and 'ionice' parameters are restricted to the 'root\@pam' user.",
29 user => 'all',
30 },
31 protected => 1,
32 proxyto => 'node',
33 parameters => {
34 additionalProperties => 0,
35 properties => PVE::VZDump::Common::json_config_properties({
36 stdout => {
37 type => 'boolean',
38 description => "Write tar to stdout, not to a file.",
39 optional => 1,
40 },
41 }),
42 },
43 returns => { type => 'string' },
44 code => sub {
45 my ($param) = @_;
46
47 my $rpcenv = PVE::RPCEnvironment::get();
48
49 my $user = $rpcenv->get_user();
50
51 my $nodename = PVE::INotify::nodename();
52
53 if ($rpcenv->{type} ne 'cli') {
54 raise_param_exc({ node => "option is only allowed on the command line interface."})
55 if $param->{node} && $param->{node} ne $nodename;
56
57 raise_param_exc({ stdout => "option is only allowed on the command line interface."})
58 if $param->{stdout};
59 }
60
61 foreach my $key (qw(maxfiles tmpdir dumpdir script bwlimit ionice)) {
62 raise_param_exc({ $key => "Only root may set this option."})
63 if defined($param->{$key}) && ($user ne 'root@pam');
64 }
65
66 PVE::VZDump::verify_vzdump_parameters($param, 1);
67
68 # silent exit if we run on wrong node
69 return 'OK' if $param->{node} && $param->{node} ne $nodename;
70
71 my $cmdline = PVE::VZDump::Common::command_line($param);
72 my @vmids;
73 # convert string lists to arrays
74 if ($param->{pool}) {
75 @vmids = @{PVE::API2Tools::get_resource_pool_guest_members($param->{pool})};
76 } else {
77 @vmids = PVE::Tools::split_list(extract_param($param, 'vmid'));
78 }
79
80 if($param->{stop}){
81 PVE::VZDump::stop_running_backups();
82 return 'OK' if !scalar(@vmids);
83 }
84
85 my $skiplist = [];
86 if (!$param->{all}) {
87 if (!$param->{node} || $param->{node} eq $nodename) {
88 my $vmlist = PVE::Cluster::get_vmlist();
89 my @localvmids = ();
90 foreach my $vmid (@vmids) {
91 my $d = $vmlist->{ids}->{$vmid};
92 if ($d && ($d->{node} ne $nodename)) {
93 push @$skiplist, $vmid;
94 } else {
95 push @localvmids, $vmid;
96 }
97 }
98 @vmids = @localvmids;
99 # silent exit if specified VMs run on other nodes
100 return "OK" if !scalar(@vmids);
101 }
102
103 $param->{vmids} = PVE::VZDump::check_vmids(@vmids)
104 }
105
106 my @exclude = PVE::Tools::split_list(extract_param($param, 'exclude'));
107 $param->{exclude} = PVE::VZDump::check_vmids(@exclude);
108
109 # exclude-path list need to be 0 separated
110 if (defined($param->{'exclude-path'})) {
111 my @expaths = split(/\0/, $param->{'exclude-path'} || '');
112 $param->{'exclude-path'} = [ @expaths ];
113 }
114
115 if (defined($param->{mailto})) {
116 my @mailto = PVE::Tools::split_list(extract_param($param, 'mailto'));
117 $param->{mailto} = [ @mailto ];
118 }
119
120 die "you can only backup a single VM with option --stdout\n"
121 if $param->{stdout} && scalar(@vmids) != 1;
122
123 $rpcenv->check($user, "/storage/$param->{storage}", [ 'Datastore.AllocateSpace' ])
124 if $param->{storage};
125
126 my $worker = sub {
127 my $upid = shift;
128
129 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
130 die "interrupted by signal\n";
131 };
132
133 my $vzdump = PVE::VZDump->new($cmdline, $param, $skiplist);
134
135 eval {
136 $vzdump->getlock($upid); # only one process allowed
137 };
138 if (my $err = $@) {
139 $vzdump->sendmail([], 0, $err);
140 exit(-1);
141 }
142
143 if (defined($param->{ionice})) {
144 if ($param->{ionice} > 7) {
145 PVE::VZDump::run_command(undef, "ionice -c3 -p $$");
146 } else {
147 PVE::VZDump::run_command(undef, "ionice -c2 -n$param->{ionice} -p $$");
148 }
149 }
150 $vzdump->exec_backup($rpcenv, $user);
151 };
152
153 open STDOUT, '>/dev/null' if $param->{quiet} && !$param->{stdout};
154 open STDERR, '>/dev/null' if $param->{quiet};
155
156 if ($rpcenv->{type} eq 'cli') {
157 if ($param->{stdout}) {
158
159 open my $saved_stdout, ">&STDOUT"
160 || die "can't dup STDOUT: $!\n";
161
162 open STDOUT, '>&STDERR' ||
163 die "unable to redirect STDOUT: $!\n";
164
165 $param->{stdout} = $saved_stdout;
166 }
167 }
168
169 my $taskid;
170 $taskid = $vmids[0] if scalar(@vmids) == 1;
171
172 return $rpcenv->fork_worker('vzdump', $taskid, $user, $worker);
173 }});
174
175 __PACKAGE__->register_method ({
176 name => 'extractconfig',
177 path => 'extractconfig',
178 method => 'GET',
179 description => "Extract configuration from vzdump backup archive.",
180 permissions => {
181 description => "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
182 user => 'all',
183 },
184 protected => 1,
185 proxyto => 'node',
186 parameters => {
187 additionalProperties => 0,
188 properties => {
189 node => get_standard_option('pve-node'),
190 volume => {
191 description => "Volume identifier",
192 type => 'string',
193 completion => \&PVE::Storage::complete_volume,
194 },
195 },
196 },
197 returns => { type => 'string' },
198 code => sub {
199 my ($param) = @_;
200
201 my $volume = extract_param($param, 'volume');
202
203 my $rpcenv = PVE::RPCEnvironment::get();
204 my $authuser = $rpcenv->get_user();
205
206 my $storage_cfg = PVE::Storage::config();
207 PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, undef, $volume);
208
209 return PVE::Storage::extract_vzdump_config($storage_cfg, $volume);
210 }});
211
212 1;