1 package PVE
::API2
::VZDump
;
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);
9 use PVE
::RPCEnvironment
;
10 use PVE
::AccessControl
;
11 use PVE
::JSONSchema
qw(get_standard_option);
14 use PVE
::VZDump
::Common
;
17 use Data
::Dumper
; # fixme: remove
20 use base
qw(PVE::RESTHandler);
22 __PACKAGE__-
>register_method ({
26 description
=> "Create backup.",
28 description
=> "The user needs 'VM.Backup' permissions on any VM, and "
29 ."'Datastore.AllocateSpace' on the backup storage. The 'maxfiles', 'prune-backups', "
30 ."'tmpdir', 'dumpdir', 'script', 'bwlimit', 'performance' and 'ionice' parameters are "
31 ."restricted to the 'root\@pam' user.",
37 additionalProperties
=> 0,
38 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
41 description
=> "Write tar to stdout, not to a file.",
46 returns
=> { type
=> 'string' },
50 my $rpcenv = PVE
::RPCEnvironment
::get
();
52 my $user = $rpcenv->get_user();
54 my $nodename = PVE
::INotify
::nodename
();
56 if ($rpcenv->{type
} ne 'cli') {
57 raise_param_exc
({ node
=> "option is only allowed on the command line interface."})
58 if $param->{node
} && $param->{node
} ne $nodename;
60 raise_param_exc
({ stdout
=> "option is only allowed on the command line interface."})
64 for my $key (qw(maxfiles prune-backups tmpdir dumpdir script bwlimit performance ionice)) {
65 raise_param_exc
({ $key => "Only root may set this option."})
66 if defined($param->{$key}) && ($user ne 'root@pam');
69 PVE
::VZDump
::verify_vzdump_parameters
($param, 1);
71 # silent exit if we run on wrong node
72 return 'OK' if $param->{node
} && $param->{node
} ne $nodename;
74 my $cmdline = PVE
::VZDump
::Common
::command_line
($param);
76 my $vmids_per_node = PVE
::VZDump
::get_included_guests
($param);
78 my $local_vmids = delete $vmids_per_node->{$nodename} // [];
80 # include IDs for deleted guests, and visibly fail later
81 my $orphaned_vmids = delete $vmids_per_node->{''} // [];
82 push @{$local_vmids}, @{$orphaned_vmids};
84 my $skiplist = [ map { @$_ } values $vmids_per_node->%* ];
87 PVE
::VZDump
::stop_running_backups
();
88 return 'OK' if !scalar(@{$local_vmids});
91 # silent exit if specified VMs run on other nodes
92 return "OK" if !scalar(@{$local_vmids}) && !$param->{all
};
94 PVE
::VZDump
::parse_mailto_exclude_path
($param);
96 die "you can only backup a single VM with option --stdout\n"
97 if $param->{stdout
} && scalar(@{$local_vmids}) != 1;
99 # If the root-only dumpdir is used rather than a storage, the check will succeed anyways.
100 my $storeid = $param->{storage
} || 'local';
101 $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.AllocateSpace' ]);
106 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
107 die "interrupted by signal\n";
110 $param->{vmids
} = $local_vmids;
111 my $vzdump = PVE
::VZDump-
>new($cmdline, $param, $skiplist);
114 $vzdump->getlock($upid); # only one process allowed
117 $vzdump->sendmail([], 0, $err);
121 if (defined($param->{ionice
})) {
122 if ($param->{ionice
} > 7) {
123 PVE
::VZDump
::run_command
(undef, "ionice -c3 -p $$");
125 PVE
::VZDump
::run_command
(undef, "ionice -c2 -n$param->{ionice} -p $$");
128 $vzdump->exec_backup($rpcenv, $user);
133 open STDOUT
, '>/dev/null' if $param->{quiet
} && !$param->{stdout
};
134 open STDERR
, '>/dev/null' if $param->{quiet
};
136 if ($rpcenv->{type
} eq 'cli') {
137 if ($param->{stdout
}) {
139 open my $saved_stdout, ">&STDOUT"
140 || die "can't dup STDOUT: $!\n";
142 open STDOUT
, '>&STDERR' ||
143 die "unable to redirect STDOUT: $!\n";
145 $param->{stdout
} = $saved_stdout;
150 $taskid = $local_vmids->[0] if scalar(@{$local_vmids}) == 1;
152 return $rpcenv->fork_worker('vzdump', $taskid, $user, $worker);
155 __PACKAGE__-
>register_method ({
159 description
=> "Get the currently configured vzdump defaults.",
161 description
=> "The user needs 'Datastore.Audit' or 'Datastore.AllocateSpace' " .
162 "permissions for the specified storage (or default storage if none specified). Some " .
163 "properties are only returned when the user has 'Sys.Audit' permissions for the node.",
168 additionalProperties
=> 0,
170 node
=> get_standard_option
('pve-node'),
171 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1 }),
176 additionalProperties
=> 0,
177 properties
=> PVE
::VZDump
::Common
::json_config_properties
(),
182 my $node = extract_param
($param, 'node');
183 my $storage = extract_param
($param, 'storage');
185 my $rpcenv = PVE
::RPCEnvironment
::get
();
186 my $authuser = $rpcenv->get_user();
188 my $res = PVE
::VZDump
::read_vzdump_defaults
();
190 $res->{storage
} = $storage if defined($storage);
192 if (!defined($res->{dumpdir
}) && !defined($res->{storage
})) {
193 $res->{storage
} = 'local';
196 if (defined($res->{storage
})) {
199 "/storage/$res->{storage}",
200 ['Datastore.Audit', 'Datastore.AllocateSpace'],
203 my $info = PVE
::VZDump
::storage_info
($res->{storage
});
204 for my $key (qw(dumpdir prune-backups)) {
205 $res->{$key} = $info->{$key} if defined($info->{$key});
209 if (defined($res->{'prune-backups'})) {
210 $res->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
211 $res->{'prune-backups'},
216 $res->{mailto
} = join(",", @{$res->{mailto
}})
217 if defined($res->{mailto
});
219 $res->{'exclude-path'} = join(",", @{$res->{'exclude-path'}})
220 if defined($res->{'exclude-path'});
222 # normal backup users don't need to know these
223 if (!$rpcenv->check($authuser, "/nodes/$node", ['Sys.Audit'], 1)) {
224 delete $res->{mailto
};
225 delete $res->{tmpdir
};
226 delete $res->{dumpdir
};
227 delete $res->{script
};
228 delete $res->{ionice
};
231 my $pool = $res->{pool
};
232 if (defined($pool) &&
233 !$rpcenv->check($authuser, "/pool/$pool", ['Pool.Audit'], 1)) {
240 __PACKAGE__-
>register_method ({
241 name
=> 'extractconfig',
242 path
=> 'extractconfig',
244 description
=> "Extract configuration from vzdump backup archive.",
246 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
252 additionalProperties
=> 0,
254 node
=> get_standard_option
('pve-node'),
256 description
=> "Volume identifier",
258 completion
=> \
&PVE
::Storage
::complete_volume
,
262 returns
=> { type
=> 'string' },
266 my $volume = extract_param
($param, 'volume');
268 my $rpcenv = PVE
::RPCEnvironment
::get
();
269 my $authuser = $rpcenv->get_user();
271 my $storage_cfg = PVE
::Storage
::config
();
272 PVE
::Storage
::check_volume_access
(
281 if (PVE
::Storage
::parse_volume_id
($volume, 1)) {
282 my (undef, undef, $ownervm) = PVE
::Storage
::parse_volname
($storage_cfg, $volume);
283 $rpcenv->check($authuser, "/vms/$ownervm", ['VM.Backup']);
286 return PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);