1 package PVE
::API2
::VZDump
;
6 use PVE
::AccessControl
;
8 use PVE
::Exception
qw(raise_param_exc);
10 use PVE
::JSONSchema
qw(get_standard_option);
11 use PVE
::RPCEnvironment
;
13 use PVE
::Tools
qw(extract_param);
14 use PVE
::VZDump
::Common
;
19 use Data
::Dumper
; # fixme: remove
21 use base
qw(PVE::RESTHandler);
23 __PACKAGE__-
>register_method ({
27 description
=> "Create backup.",
29 description
=> "The user needs 'VM.Backup' permissions on any VM, and "
30 ."'Datastore.AllocateSpace' on the backup storage. The 'maxfiles', 'prune-backups', "
31 ."'tmpdir', 'dumpdir', 'script', 'bwlimit', 'performance' and 'ionice' parameters are "
32 ."restricted to the 'root\@pam' user.",
38 additionalProperties
=> 0,
39 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
42 description
=> "Write tar to stdout, not to a file.",
47 returns
=> { type
=> 'string' },
51 my $rpcenv = PVE
::RPCEnvironment
::get
();
53 my $user = $rpcenv->get_user();
55 my $nodename = PVE
::INotify
::nodename
();
57 if ($rpcenv->{type
} ne 'cli') {
58 raise_param_exc
({ node
=> "option is only allowed on the command line interface."})
59 if $param->{node
} && $param->{node
} ne $nodename;
61 raise_param_exc
({ stdout
=> "option is only allowed on the command line interface."})
65 for my $key (qw(maxfiles prune-backups tmpdir dumpdir script bwlimit performance ionice)) {
66 raise_param_exc
({ $key => "Only root may set this option."})
67 if defined($param->{$key}) && ($user ne 'root@pam');
70 PVE
::VZDump
::verify_vzdump_parameters
($param, 1);
72 # silent exit if we run on wrong node
73 return 'OK' if $param->{node
} && $param->{node
} ne $nodename;
75 my $cmdline = PVE
::VZDump
::Common
::command_line
($param);
77 my $vmids_per_node = PVE
::VZDump
::get_included_guests
($param);
79 my $local_vmids = delete $vmids_per_node->{$nodename} // [];
81 # include IDs for deleted guests, and visibly fail later
82 my $orphaned_vmids = delete $vmids_per_node->{''} // [];
83 push @{$local_vmids}, @{$orphaned_vmids};
85 my $skiplist = [ map { @$_ } values $vmids_per_node->%* ];
88 PVE
::VZDump
::stop_running_backups
();
89 return 'OK' if !scalar(@{$local_vmids});
92 # silent exit if specified VMs run on other nodes
93 return "OK" if !scalar(@{$local_vmids}) && !$param->{all
};
95 PVE
::VZDump
::parse_mailto_exclude_path
($param);
97 die "you can only backup a single VM with option --stdout\n"
98 if $param->{stdout
} && scalar(@{$local_vmids}) != 1;
100 # If the root-only dumpdir is used rather than a storage, the check will succeed anyways.
101 my $storeid = $param->{storage
} || 'local';
102 $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.AllocateSpace' ]);
107 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
108 die "interrupted by signal\n";
111 $param->{vmids
} = $local_vmids;
112 my $vzdump = PVE
::VZDump-
>new($cmdline, $param, $skiplist);
115 $vzdump->getlock($upid); # only one process allowed
118 $vzdump->sendmail([], 0, $err);
122 if (defined($param->{ionice
})) {
123 if ($param->{ionice
} > 7) {
124 PVE
::VZDump
::run_command
(undef, "ionice -c3 -p $$");
126 PVE
::VZDump
::run_command
(undef, "ionice -c2 -n$param->{ionice} -p $$");
129 $vzdump->exec_backup($rpcenv, $user);
134 open STDOUT
, '>/dev/null' if $param->{quiet
} && !$param->{stdout
};
135 open STDERR
, '>/dev/null' if $param->{quiet
};
137 if ($rpcenv->{type
} eq 'cli') {
138 if ($param->{stdout
}) {
140 open my $saved_stdout, ">&STDOUT"
141 || die "can't dup STDOUT: $!\n";
143 open STDOUT
, '>&STDERR' ||
144 die "unable to redirect STDOUT: $!\n";
146 $param->{stdout
} = $saved_stdout;
151 $taskid = $local_vmids->[0] if scalar(@{$local_vmids}) == 1;
153 return $rpcenv->fork_worker('vzdump', $taskid, $user, $worker);
156 __PACKAGE__-
>register_method ({
160 description
=> "Get the currently configured vzdump defaults.",
162 description
=> "The user needs 'Datastore.Audit' or 'Datastore.AllocateSpace' " .
163 "permissions for the specified storage (or default storage if none specified). Some " .
164 "properties are only returned when the user has 'Sys.Audit' permissions for the node.",
169 additionalProperties
=> 0,
171 node
=> get_standard_option
('pve-node'),
172 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1 }),
177 additionalProperties
=> 0,
178 properties
=> PVE
::VZDump
::Common
::json_config_properties
(),
183 my $node = extract_param
($param, 'node');
184 my $storage = extract_param
($param, 'storage');
186 my $rpcenv = PVE
::RPCEnvironment
::get
();
187 my $authuser = $rpcenv->get_user();
189 my $res = PVE
::VZDump
::read_vzdump_defaults
();
191 $res->{storage
} = $storage if defined($storage);
193 if (!defined($res->{dumpdir
}) && !defined($res->{storage
})) {
194 $res->{storage
} = 'local';
197 if (defined($res->{storage
})) {
200 "/storage/$res->{storage}",
201 ['Datastore.Audit', 'Datastore.AllocateSpace'],
204 my $info = PVE
::VZDump
::storage_info
($res->{storage
});
205 for my $key (qw(dumpdir prune-backups)) {
206 $res->{$key} = $info->{$key} if defined($info->{$key});
210 if (defined($res->{'prune-backups'})) {
211 $res->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
212 $res->{'prune-backups'},
217 $res->{mailto
} = join(",", @{$res->{mailto
}})
218 if defined($res->{mailto
});
220 $res->{'exclude-path'} = join(",", @{$res->{'exclude-path'}})
221 if defined($res->{'exclude-path'});
223 # normal backup users don't need to know these
224 if (!$rpcenv->check($authuser, "/nodes/$node", ['Sys.Audit'], 1)) {
225 delete $res->{mailto
};
226 delete $res->{tmpdir
};
227 delete $res->{dumpdir
};
228 delete $res->{script
};
229 delete $res->{ionice
};
232 my $pool = $res->{pool
};
233 if (defined($pool) &&
234 !$rpcenv->check($authuser, "/pool/$pool", ['Pool.Audit'], 1)) {
241 __PACKAGE__-
>register_method ({
242 name
=> 'extractconfig',
243 path
=> 'extractconfig',
245 description
=> "Extract configuration from vzdump backup archive.",
247 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
253 additionalProperties
=> 0,
255 node
=> get_standard_option
('pve-node'),
257 description
=> "Volume identifier",
259 completion
=> \
&PVE
::Storage
::complete_volume
,
263 returns
=> { type
=> 'string' },
267 my $volume = extract_param
($param, 'volume');
269 my $rpcenv = PVE
::RPCEnvironment
::get
();
270 my $authuser = $rpcenv->get_user();
272 my $storage_cfg = PVE
::Storage
::config
();
273 PVE
::Storage
::check_volume_access
(
282 if (PVE
::Storage
::parse_volume_id
($volume, 1)) {
283 my (undef, undef, $ownervm) = PVE
::Storage
::parse_volname
($storage_cfg, $volume);
284 $rpcenv->check($authuser, "/vms/$ownervm", ['VM.Backup']);
287 return PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);