]>
git.proxmox.com Git - pve-manager.git/blob - PVE/API2/VZDump.pm
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) ;
15 use Data
:: Dumper
; # fixme: remove
18 use base
qw(PVE::RESTHandler) ;
20 __PACKAGE__-
> register_method ({
24 description
=> "Create backup." ,
26 description
=> "The user needs 'VM.Backup' permissions on any VM, and 'Datastore.AllocateSpace' on the backup storage. The 'maxfiles', 'tmpdir', 'dumpdir', 'script', 'bwlimit' and 'ionice' parameters are restricted to the 'root\ @pam ' user." ,
32 additionalProperties
=> 0 ,
33 properties
=> PVE
:: VZDump
:: json_config_properties
({
36 description
=> "Write tar to stdout, not to a file." ,
41 returns
=> { type
=> 'string' },
45 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
47 my $user = $rpcenv -> get_user ();
49 my $nodename = PVE
:: INotify
:: nodename
();
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 ;
55 raise_param_exc
({ stdout
=> "option is only allowed on the command line interface." })
59 foreach my $key ( qw(maxfiles tmpdir dumpdir script bwlimit ionice) ) {
60 raise_param_exc
({ $key => "Only root may set this option." })
61 if defined ( $param ->{ $key }) && ( $user ne 'root @pam ' );
64 # by default we set --rsyncable for gzip
65 local $ENV { GZIP
} = "--rsyncable" if ! $ENV { GZIP
};
67 PVE
:: VZDump
:: verify_vzdump_parameters
( $param, 1 );
69 # silent exit if we run on wrong node
70 return 'OK' if $param ->{ node
} && $param ->{ node
} ne $nodename ;
72 my $cmdline = PVE
:: VZDump
:: command_line
( $param );
74 # convert string lists to arrays
75 my @vmids = PVE
:: Tools
:: split_list
( extract_param
( $param, 'vmid' ));
78 PVE
:: VZDump
:: stop_running_backups
();
79 return 'OK' if ! scalar ( @vmids );
84 if (! $param ->{ node
}) {
85 my $vmlist = PVE
:: Cluster
:: get_vmlist
();
87 foreach my $vmid ( @vmids ) {
88 my $d = $vmlist ->{ ids
}->{ $vmid };
89 if ( $d && ( $d ->{ node
} ne $nodename )) {
90 push @$skiplist, $vmid ;
92 push @localvmids, $vmid ;
96 # silent exit if specified VMs run on other nodes
97 return "OK" if ! scalar ( @vmids );
100 $param ->{ vmids
} = PVE
:: VZDump
:: check_vmids
( @vmids )
103 my @exclude = PVE
:: Tools
:: split_list
( extract_param
( $param, 'exclude' ));
104 $param ->{ exclude
} = PVE
:: VZDump
:: check_vmids
( @exclude );
106 # exclude-path list need to be 0 separated
107 if ( defined ( $param ->{ 'exclude-path' })) {
108 my @expaths = split ( /\0/ , $param ->{ 'exclude-path' } || '' );
109 $param ->{ 'exclude-path' } = [ @expaths ];
112 if ( defined ( $param ->{ mailto
})) {
113 my @mailto = PVE
:: Tools
:: split_list
( extract_param
( $param, 'mailto' ));
114 $param ->{ mailto
} = [ @mailto ];
117 die "you can only backup a single VM with option --stdout \n "
118 if $param ->{ stdout
} && scalar ( @vmids ) != 1 ;
120 $rpcenv -> check ( $user, "/storage/ $param ->{storage}" , [ 'Datastore.AllocateSpace' ])
121 if $param ->{ storage
};
126 $SIG { INT
} = $SIG { TERM
} = $SIG { QUIT
} = $SIG { HUP
} = $SIG { PIPE
} = sub {
127 die "interrupted by signal \n " ;
130 my $vzdump = PVE
:: VZDump-
> new ( $cmdline, $param, $skiplist );
133 $vzdump -> getlock ( $upid ); # only one process allowed
136 $vzdump -> sendmail ([], 0 , $err );
140 if ( defined ( $param ->{ ionice
})) {
141 if ( $param ->{ ionice
} > 7 ) {
142 PVE
:: VZDump
:: run_command
( undef , "ionice -c3 -p $$ " );
144 PVE
:: VZDump
:: run_command
( undef , "ionice -c2 -n $param ->{ionice} -p $$ " );
147 $vzdump -> exec_backup ( $rpcenv, $user );
150 open STDOUT
, '>/dev/null' if $param ->{ quiet
} && ! $param ->{ stdout
};
151 open STDERR
, '>/dev/null' if $param ->{ quiet
};
153 if ( $rpcenv ->{ type
} eq 'cli' ) {
154 if ( $param ->{ stdout
}) {
156 open my $saved_stdout, ">&STDOUT"
157 || die "can't dup STDOUT: $!\n " ;
159 open STDOUT
, '>&STDERR' ||
160 die "unable to redirect STDOUT: $!\n " ;
162 $param ->{ stdout
} = $saved_stdout ;
166 return $rpcenv -> fork_worker ( 'vzdump' , undef , $user, $worker );
169 __PACKAGE__-
> register_method ({
170 name
=> 'extractconfig' ,
171 path
=> 'extractconfig' ,
173 description
=> "Extract configuration from vzdump backup archive." ,
175 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage." ,
181 additionalProperties
=> 0 ,
183 node
=> get_standard_option
( 'pve-node' ),
185 description
=> "Volume identifier" ,
187 completion
=> \
& PVE
:: Storage
:: complete_volume
,
191 returns
=> { type
=> 'string' },
195 my $volume = extract_param
( $param, 'volume' );
197 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
198 my $authuser = $rpcenv -> get_user ();
200 my $storage_cfg = PVE
:: Storage
:: config
();
201 PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $storage_cfg, undef , $volume );
203 return PVE
:: Storage
:: extract_vzdump_config
( $storage_cfg, $volume );