]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/VZDump.pm
1c0743320862e1a88a8707694722bb7ee959e96a
[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
15 use Data::Dumper; # fixme: remove
16
17
18 use base qw(PVE::RESTHandler);
19
20 __PACKAGE__->register_method ({
21 name => 'vzdump',
22 path => '',
23 method => 'POST',
24 description => "Create backup.",
25 permissions => {
26 description => "The user needs VM.Backup permissions on any VM.",
27 user => 'all',
28 },
29 protected => 1,
30 proxyto => 'node',
31 parameters => {
32 additionalProperties => 0,
33 properties => PVE::VZDump::json_config_properties({
34 stdout => {
35 type => 'boolean',
36 description => "Write tar to stdout, not to a file.",
37 optional => 1,
38 },
39 }),
40 },
41 returns => { type => 'string' },
42 code => sub {
43 my ($param) = @_;
44
45 my $rpcenv = PVE::RPCEnvironment::get();
46
47 my $user = $rpcenv->get_user();
48
49 my $nodename = PVE::INotify::nodename();
50
51 if ($rpcenv->{type} ne 'cli') {
52 raise_param_exc({ node => "option is only allowed on the command line interface."})
53 if $param->{node} && $param->{node} ne $nodename;
54
55 raise_param_exc({ stdout => "option is only allowed on the command line interface."})
56 if $param->{stdout};
57 }
58
59 # by default we set --rsyncable for gzip
60 local $ENV{GZIP} = "--rsyncable" if !$ENV{GZIP};
61
62 PVE::VZDump::verify_vzdump_parameters($param, 1);
63
64 # silent exit if we run on wrong node
65 exit(0) if $param->{node} && $param->{node} ne $nodename;
66
67 my $cmdline = PVE::VZDump::command_line($param);
68
69 # convert string lists to arrays
70 my @vmids = PVE::Tools::split_list(extract_param($param, 'vmid'));
71
72 my $skiplist = [];
73 if (!$param->{all}) {
74 if (!$param->{node}) {
75 my $vmlist = PVE::Cluster::get_vmlist();
76 my @localvmids = ();
77 foreach my $vmid (@vmids) {
78 my $d = $vmlist->{ids}->{$vmid};
79 if ($d && ($d->{node} ne $nodename)) {
80 push @$skiplist, $vmid;
81 } else {
82 push @localvmids, $vmid;
83 }
84 }
85 @vmids = @localvmids;
86 # silent exit if specified VMs run on other nodes
87 exit(0) if !scalar(@vmids);
88 }
89
90 $param->{vmids} = PVE::VZDump::check_vmids(@vmids)
91 }
92
93 my @exclude = PVE::Tools::split_list(extract_param($param, 'exclude'));
94 $param->{exclude} = PVE::VZDump::check_vmids(@exclude);
95
96 # exclude-path list need to be 0 separated
97 my @expaths = split(/\0/, $param->{'exclude-path'} || '');
98 $param->{'exclude-path'} = [ @expaths ];
99
100 my @mailto = PVE::Tools::split_list(extract_param($param, 'mailto'));
101 $param->{mailto} = [ @mailto ];
102
103 die "you can only backup a single VM with option --stdout\n"
104 if $param->{stdout} && scalar(@vmids) != 1;
105
106 foreach my $key (qw(maxfiles tmpdir dumpdir script size bwlimit ionice)) {
107 raise_param_exc({ $key => "Only root may set this option."})
108 if defined($param->{$key}) && ($user ne 'root@pam');
109 }
110
111 my $vzdump = PVE::VZDump->new($cmdline, $param, $skiplist);
112
113 my $worker = sub {
114 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
115 die "interrupted by signal\n";
116 };
117
118 $vzdump->getlock (); # only one process allowed
119
120 if (defined($param->{ionice})) {
121 if ($param->{ionice} > 7) {
122 PVE::VZDump::run_command(undef, "ionice -c3 -p $$");
123 } else {
124 PVE::VZDump::run_command(undef, "ionice -c2 -n$param->{ionice} -p $$");
125 }
126 }
127 $vzdump->exec_backup($rpcenv, $user);
128 };
129
130 open STDOUT, '>/dev/null' if $param->{quiet} && !$param->{stdout};
131 open STDERR, '>/dev/null' if $param->{quiet};
132
133 if ($rpcenv->{type} eq 'cli') {
134 if ($param->{stdout}) {
135
136 open my $saved_stdout, ">&STDOUT"
137 || die "can't dup STDOUT: $!\n";
138
139 open STDOUT, '>&STDERR' ||
140 die "unable to redirect STDOUT: $!\n";
141
142 $param->{stdout} = $saved_stdout;
143 }
144 }
145
146 return $rpcenv->fork_worker('vzdump', undef, $user, $worker);
147 }});