]> git.proxmox.com Git - pve-manager.git/blob - PVE/API2/VZDump.pm
cleanup vzdump backup/cron code
[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 protected => 1,
26 parameters => {
27 additionalProperties => 0,
28 properties => PVE::VZDump::json_config_properties({
29 stdout => {
30 type => 'boolean',
31 description => "Write tar to stdout, not to a file.",
32 optional => 1,
33 },
34 }),
35 },
36 returns => { type => 'string' },
37 code => sub {
38 my ($param) = @_;
39
40 my $rpcenv = PVE::RPCEnvironment::get();
41
42 my $user = $rpcenv->get_user();
43
44 my $nodename = PVE::INotify::nodename();
45
46 if ($rpcenv->{type} ne 'cli') {
47 raise_param_exc({ node => "option is only allowed on the command line interface."})
48 if $param->{node} && $param->{node} ne $nodename;
49
50 raise_param_exc({ stdout => "option is only allowed on the command line interface."})
51 if $param->{stdout};
52 }
53
54 # by default we set --rsyncable for gzip
55 local $ENV{GZIP} = "--rsyncable" if !$ENV{GZIP};
56
57 PVE::VZDump::verify_vzdump_parameters($param, 1);
58
59 # silent exit if we run on wrong node
60 exit(0) if $param->{node} && $param->{node} ne $nodename;
61
62 my $cmdline = PVE::VZDump::command_line($param);
63
64 # convert string lists to arrays
65 my @vmids = PVE::Tools::split_list(extract_param($param, 'vmid'));
66
67 $param->{vmids} = PVE::VZDump::check_vmids(@vmids) if !$param->{all};
68 my @exclude = PVE::Tools::split_list(extract_param($param, 'exclude'));
69 $param->{exclude} = PVE::VZDump::check_vmids(@exclude);
70
71 # exclude-path list need to be 0 separated
72 my @expaths = split(/\0/, $param->{'exclude-path'} || '');
73 $param->{'exclude-path'} = @expaths;
74
75 my @mailto = PVE::Tools::split_list(extract_param($param, 'mailto'));
76 $param->{mailto} = [ @mailto ];
77
78 die "you can only backup a single VM with option --stdout\n"
79 if $param->{stdout} && scalar(@vmids) != 1;
80
81 my $vzdump = PVE::VZDump->new($cmdline, $param);
82
83 my $worker = sub {
84 $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub {
85 die "interrupted by signal\n";
86 };
87
88 $vzdump->getlock (); # only one process allowed
89
90 if (defined($param->{ionice})) {
91 if ($param->{ionice} > 7) {
92 PVE::VZDump::run_command(undef, "ionice -c3 -p $$");
93 } else {
94 PVE::VZDump::run_command(undef, "ionice -c2 -n$param->{ionice} -p $$");
95 }
96 }
97 $vzdump->exec_backup();
98 };
99
100 open STDOUT, '>/dev/null' if $param->{quiet} && !$param->{stdout};
101 open STDERR, '>/dev/null' if $param->{quiet};
102
103 if ($rpcenv->{type} eq 'cli') {
104 if ($param->{stdout}) {
105
106 open my $saved_stdout, ">&STDOUT"
107 || die "can't dup STDOUT: $!\n";
108
109 open STDOUT, '>&STDERR' ||
110 die "unable to redirect STDOUT: $!\n";
111
112 $param->{stdout} = $saved_stdout;
113 }
114 }
115
116 return $rpcenv->fork_worker('vzdump', undef, $user, $worker);
117 }});