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 '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.",
36 additionalProperties
=> 0,
37 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
40 description
=> "Write tar to stdout, not to a file.",
45 returns
=> { type
=> 'string' },
49 my $rpcenv = PVE
::RPCEnvironment
::get
();
51 my $user = $rpcenv->get_user();
53 my $nodename = PVE
::INotify
::nodename
();
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;
59 raise_param_exc
({ stdout
=> "option is only allowed on the command line interface."})
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');
68 PVE
::VZDump
::verify_vzdump_parameters
($param, 1);
70 # silent exit if we run on wrong node
71 return 'OK' if $param->{node
} && $param->{node
} ne $nodename;
73 my $cmdline = PVE
::VZDump
::Common
::command_line
($param);
75 my $vmids_per_node = PVE
::VZDump
::get_included_guests
($param);
77 my $local_vmids = delete $vmids_per_node->{$nodename} // [];
79 # include IDs for deleted guests, and visibly fail later
80 my $orphaned_vmids = delete $vmids_per_node->{''} // [];
81 push @{$local_vmids}, @{$orphaned_vmids};
83 my $skiplist = [ map { @$_ } values $vmids_per_node->%* ];
86 PVE
::VZDump
::stop_running_backups
();
87 return 'OK' if !scalar(@{$local_vmids});
90 # silent exit if specified VMs run on other nodes
91 return "OK" if !scalar(@{$local_vmids}) && !$param->{all
};
93 PVE
::VZDump
::parse_mailto_exclude_path
($param);
95 die "you can only backup a single VM with option --stdout\n"
96 if $param->{stdout
} && scalar(@{$local_vmids}) != 1;
98 $rpcenv->check($user, "/storage/$param->{storage}", [ 'Datastore.AllocateSpace' ])
104 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
105 die "interrupted by signal\n";
108 $param->{vmids
} = $local_vmids;
109 my $vzdump = PVE
::VZDump-
>new($cmdline, $param, $skiplist);
112 $vzdump->getlock($upid); # only one process allowed
115 $vzdump->sendmail([], 0, $err);
119 if (defined($param->{ionice
})) {
120 if ($param->{ionice
} > 7) {
121 PVE
::VZDump
::run_command
(undef, "ionice -c3 -p $$");
123 PVE
::VZDump
::run_command
(undef, "ionice -c2 -n$param->{ionice} -p $$");
126 $vzdump->exec_backup($rpcenv, $user);
131 open STDOUT
, '>/dev/null' if $param->{quiet
} && !$param->{stdout
};
132 open STDERR
, '>/dev/null' if $param->{quiet
};
134 if ($rpcenv->{type
} eq 'cli') {
135 if ($param->{stdout
}) {
137 open my $saved_stdout, ">&STDOUT"
138 || die "can't dup STDOUT: $!\n";
140 open STDOUT
, '>&STDERR' ||
141 die "unable to redirect STDOUT: $!\n";
143 $param->{stdout
} = $saved_stdout;
148 $taskid = $local_vmids->[0] if scalar(@{$local_vmids}) == 1;
150 return $rpcenv->fork_worker('vzdump', $taskid, $user, $worker);
153 __PACKAGE__-
>register_method ({
157 description
=> "Get the currently configured vzdump defaults.",
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.",
166 additionalProperties
=> 0,
168 node
=> get_standard_option
('pve-node'),
169 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1 }),
174 additionalProperties
=> 0,
175 properties
=> PVE
::VZDump
::Common
::json_config_properties
(),
180 my $node = extract_param
($param, 'node');
181 my $storage = extract_param
($param, 'storage');
183 my $rpcenv = PVE
::RPCEnvironment
::get
();
184 my $authuser = $rpcenv->get_user();
186 my $res = PVE
::VZDump
::read_vzdump_defaults
();
188 $res->{storage
} = $storage if defined($storage);
190 if (!defined($res->{dumpdir
}) && !defined($res->{storage
})) {
191 $res->{storage
} = 'local';
194 if (defined($res->{storage
})) {
197 "/storage/$res->{storage}",
198 ['Datastore.Audit', 'Datastore.AllocateSpace'],
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});
207 if (defined($res->{'prune-backups'})) {
208 $res->{'prune-backups'} = PVE
::JSONSchema
::print_property_string
(
209 $res->{'prune-backups'},
214 $res->{mailto
} = join(",", @{$res->{mailto
}})
215 if defined($res->{mailto
});
217 $res->{'exclude-path'} = join(",", @{$res->{'exclude-path'}})
218 if defined($res->{'exclude-path'});
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
};
229 my $pool = $res->{pool
};
230 if (defined($pool) &&
231 !$rpcenv->check($authuser, "/pool/$pool", ['Pool.Audit'], 1)) {
238 __PACKAGE__-
>register_method ({
239 name
=> 'extractconfig',
240 path
=> 'extractconfig',
242 description
=> "Extract configuration from vzdump backup archive.",
244 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
250 additionalProperties
=> 0,
252 node
=> get_standard_option
('pve-node'),
254 description
=> "Volume identifier",
256 completion
=> \
&PVE
::Storage
::complete_volume
,
260 returns
=> { type
=> 'string' },
264 my $volume = extract_param
($param, 'volume');
266 my $rpcenv = PVE
::RPCEnvironment
::get
();
267 my $authuser = $rpcenv->get_user();
269 my $storage_cfg = PVE
::Storage
::config
();
270 PVE
::Storage
::check_volume_access
(
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']);
284 return PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);