]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/VZDump.pm
2eda973e59ce9c65bcae7efab5de53d4e93abedb
[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
73 my $vmids_per_node = PVE::VZDump::get_included_guests($param);
74
75 my $local_vmids = delete $vmids_per_node->{$nodename} // [];
76
77 my $skiplist = [ map { @$_ } values $vmids_per_node->%* ];
78
79 if($param->{stop}){
80 PVE::VZDump::stop_running_backups();
81 return 'OK' if !scalar(@{$local_vmids});
82 }
83
84 # silent exit if specified VMs run on other nodes
85 return "OK" if !scalar(@{$local_vmids}) && !$param->{all};
86
87 # exclude-path list need to be 0 separated
88 if (defined($param->{'exclude-path'})) {
89 my @expaths = split(/\0/, $param->{'exclude-path'} || '');
90 $param->{'exclude-path'} = [ @expaths ];
91 }
92
93 if (defined($param->{mailto})) {
94 my @mailto = PVE::Tools::split_list(extract_param($param, 'mailto'));
95 $param->{mailto} = [ @mailto ];
96 }
97
98 die "you can only backup a single VM with option --stdout\n"
99 if $param->{stdout} && scalar(@{$local_vmids}) != 1;
100
101 $rpcenv->check($user, "/storage/$param->{storage}", [ 'Datastore.AllocateSpace' ])
102 if $param->{storage};
103
104 my $worker = sub {
105 my $upid = shift;
106
107 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
108 die "interrupted by signal\n";
109 };
110
111 $param->{vmids} = $local_vmids;
112 my $vzdump = PVE::VZDump->new($cmdline, $param, $skiplist);
113
114 eval {
115 $vzdump->getlock($upid); # only one process allowed
116 };
117 if (my $err = $@) {
118 $vzdump->sendmail([], 0, $err);
119 exit(-1);
120 }
121
122 if (defined($param->{ionice})) {
123 if ($param->{ionice} > 7) {
124 PVE::VZDump::run_command(undef, "ionice -c3 -p $$");
125 } else {
126 PVE::VZDump::run_command(undef, "ionice -c2 -n$param->{ionice} -p $$");
127 }
128 }
129 $vzdump->exec_backup($rpcenv, $user);
130 };
131
132 open STDOUT, '>/dev/null' if $param->{quiet} && !$param->{stdout};
133 open STDERR, '>/dev/null' if $param->{quiet};
134
135 if ($rpcenv->{type} eq 'cli') {
136 if ($param->{stdout}) {
137
138 open my $saved_stdout, ">&STDOUT"
139 || die "can't dup STDOUT: $!\n";
140
141 open STDOUT, '>&STDERR' ||
142 die "unable to redirect STDOUT: $!\n";
143
144 $param->{stdout} = $saved_stdout;
145 }
146 }
147
148 my $taskid;
149 $taskid = $local_vmids->[0] if scalar(@{$local_vmids}) == 1;
150
151 return $rpcenv->fork_worker('vzdump', $taskid, $user, $worker);
152 }});
153
154 __PACKAGE__->register_method ({
155 name => 'extractconfig',
156 path => 'extractconfig',
157 method => 'GET',
158 description => "Extract configuration from vzdump backup archive.",
159 permissions => {
160 description => "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
161 user => 'all',
162 },
163 protected => 1,
164 proxyto => 'node',
165 parameters => {
166 additionalProperties => 0,
167 properties => {
168 node => get_standard_option('pve-node'),
169 volume => {
170 description => "Volume identifier",
171 type => 'string',
172 completion => \&PVE::Storage::complete_volume,
173 },
174 },
175 },
176 returns => { type => 'string' },
177 code => sub {
178 my ($param) = @_;
179
180 my $volume = extract_param($param, 'volume');
181
182 my $rpcenv = PVE::RPCEnvironment::get();
183 my $authuser = $rpcenv->get_user();
184
185 my $storage_cfg = PVE::Storage::config();
186 PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, undef, $volume);
187
188 return PVE::Storage::extract_vzdump_config($storage_cfg, $volume);
189 }});
190
191 1;