1 package PVE
::API2
::Backup
;
9 use PVE
::Tools
qw(extract_param);
10 use PVE
::Cluster
qw(cfs_lock_file cfs_read_file cfs_write_file);
12 use PVE
::RPCEnvironment
;
15 use PVE
::Exception
qw(raise_param_exc);
17 use PVE
::VZDump
::Common
;
18 use PVE
::VZDump
::JobBase
;
19 use PVE
::Jobs
; # for VZDump Jobs
20 use Proxmox
::RS
::CalendarEvent
;
22 use base
qw(PVE::RESTHandler);
24 use constant ALL_DAYS
=> 'mon,tue,wed,thu,fri,sat,sun';
26 PVE
::JSONSchema
::register_format
('pve-day-of-week', \
&verify_day_of_week
);
27 sub verify_day_of_week
{
28 my ($value, $noerr) = @_;
30 return $value if $value =~ m/^(mon|tue|wed|thu|fri|sat|sun)$/;
32 return undef if $noerr;
34 die "invalid day '$value'\n";
37 my $vzdump_job_id_prop = {
39 description
=> "The job ID.",
43 my $assert_param_permission = sub {
44 my ($param, $user) = @_;
45 return if $user eq 'root@pam'; # always OK
47 for my $key (qw(tmpdir dumpdir script)) {
48 raise_param_exc
({ $key => "Only root may set this option."}) if exists $param->{$key};
52 my $convert_to_schedule = sub {
55 my $starttime = $job->{starttime
};
57 return "$starttime" if !$job->{dow
}; # dow is restrictive, so none means all days
59 # normalize as it could be a null-separated list previously
60 my $dow = join(',', PVE
::Tools
::split_list
($job->{dow
}));
62 return $dow eq ALL_DAYS ?
"$starttime" : "$dow $starttime";
65 my $schedule_param_check = sub {
66 my ($param, $required) = @_;
67 if (defined($param->{schedule
})) {
68 if (defined($param->{starttime
})) {
69 raise_param_exc
({ starttime
=> "'starttime' and 'schedule' cannot both be set" });
71 } elsif (!defined($param->{starttime
})) {
72 raise_param_exc
({ schedule
=> "neither 'starttime' nor 'schedule' were set" })
75 $param->{schedule
} = $convert_to_schedule->($param);
78 delete $param->{starttime
};
82 __PACKAGE__-
>register_method({
86 description
=> "List vzdump backup schedule.",
88 check
=> ['perm', '/', ['Sys.Audit']],
91 additionalProperties
=> 0,
99 id
=> $vzdump_job_id_prop
102 links
=> [ { rel
=> 'child', href
=> "{id}" } ],
107 my $rpcenv = PVE
::RPCEnvironment
::get
();
108 my $user = $rpcenv->get_user();
110 my $data = cfs_read_file
('vzdump.cron');
111 my $jobs_data = cfs_read_file
('jobs.cfg');
112 my $order = $jobs_data->{order
};
113 my $jobs = $jobs_data->{ids
};
115 my $res = $data->{jobs
} || [];
116 foreach my $job (@$res) {
117 $job->{schedule
} = $convert_to_schedule->($job);
120 foreach my $jobid (sort { $order->{$a} <=> $order->{$b} } keys %$jobs) {
121 my $job = $jobs->{$jobid};
122 next if $job->{type
} ne 'vzdump';
124 if (my $schedule = $job->{schedule
}) {
125 # vzdump jobs are cluster wide, there maybe was no local run
126 # so simply calculate from now
127 my $last_run = time();
128 my $calspec = Proxmox
::RS
::CalendarEvent-
>new($schedule);
129 my $next_run = $calspec->compute_next_event($last_run);
130 $job->{'next-run'} = $next_run if defined($next_run);
139 __PACKAGE__-
>register_method({
140 name
=> 'create_job',
144 description
=> "Create new vzdump backup job.",
146 check
=> ['perm', '/', ['Sys.Modify']],
147 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
150 additionalProperties
=> 0,
151 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
154 description
=> "Job ID (will be autogenerated).",
155 format
=> 'pve-configid',
156 optional
=> 1, # FIXME: make required on 8.0
159 description
=> "Backup schedule. The format is a subset of `systemd` calendar events.",
160 type
=> 'string', format
=> 'pve-calendar-event',
166 description
=> "Job Start time.",
167 pattern
=> '\d{1,2}:\d{1,2}',
172 type
=> 'string', format
=> 'pve-day-of-week-list',
174 description
=> "Day of week selection.",
175 requires
=> 'starttime',
181 description
=> "Enable or disable the job.",
187 description
=> "If true, the job will be run as soon as possible if it was missed".
188 " while the scheduler was not running.",
194 description
=> "Description for the Job.",
199 returns
=> { type
=> 'null' },
203 my $rpcenv = PVE
::RPCEnvironment
::get
();
204 my $user = $rpcenv->get_user();
206 $assert_param_permission->($param, $user);
208 if (my $pool = $param->{pool
}) {
209 $rpcenv->check_pool_exist($pool);
210 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
213 $schedule_param_check->($param, 1);
215 $param->{enabled
} = 1 if !defined($param->{enabled
});
217 # autogenerate id for api compatibility FIXME remove with 8.0
218 my $id = extract_param
($param, 'id') // UUID
::uuid
();
220 cfs_lock_file
('jobs.cfg', undef, sub {
221 my $data = cfs_read_file
('jobs.cfg');
223 die "Job '$id' already exists\n"
224 if $data->{ids
}->{$id};
226 PVE
::VZDump
::verify_vzdump_parameters
($param, 1);
227 my $opts = PVE
::VZDump
::JobBase-
>check_config($id, $param, 1, 1);
229 $data->{ids
}->{$id} = $opts;
231 PVE
::Jobs
::create_job
($id, 'vzdump', $opts);
233 cfs_write_file
('jobs.cfg', $data);
240 __PACKAGE__-
>register_method({
244 description
=> "Read vzdump backup job definition.",
246 check
=> ['perm', '/', ['Sys.Audit']],
249 additionalProperties
=> 0,
251 id
=> $vzdump_job_id_prop
260 my $rpcenv = PVE
::RPCEnvironment
::get
();
261 my $user = $rpcenv->get_user();
263 my $data = cfs_read_file
('vzdump.cron');
265 my $jobs = $data->{jobs
} || [];
267 foreach my $job (@$jobs) {
268 if ($job->{id
} eq $param->{id
}) {
269 $job->{schedule
} = $convert_to_schedule->($job);
274 my $jobs_data = cfs_read_file
('jobs.cfg');
275 my $job = $jobs_data->{ids
}->{$param->{id
}};
276 return $job if $job && $job->{type
} eq 'vzdump';
278 raise_param_exc
({ id
=> "No such job '$param->{id}'" });
282 __PACKAGE__-
>register_method({
283 name
=> 'delete_job',
286 description
=> "Delete vzdump backup job definition.",
288 check
=> ['perm', '/', ['Sys.Modify']],
292 additionalProperties
=> 0,
294 id
=> $vzdump_job_id_prop
297 returns
=> { type
=> 'null' },
301 my $rpcenv = PVE
::RPCEnvironment
::get
();
302 my $user = $rpcenv->get_user();
304 my $id = $param->{id
};
306 my $delete_job = sub {
307 my $data = cfs_read_file
('vzdump.cron');
309 my $jobs = $data->{jobs
} || [];
313 foreach my $job (@$jobs) {
314 if ($job->{id
} eq $id) {
317 push @$newjobs, $job;
322 cfs_lock_file
('jobs.cfg', undef, sub {
323 my $jobs_data = cfs_read_file
('jobs.cfg');
325 if (!defined($jobs_data->{ids
}->{$id})) {
326 raise_param_exc
({ id
=> "No such job '$id'" });
328 delete $jobs_data->{ids
}->{$id};
330 PVE
::Jobs
::remove_job
($id, 'vzdump');
332 cfs_write_file
('jobs.cfg', $jobs_data);
336 $data->{jobs
} = $newjobs;
338 cfs_write_file
('vzdump.cron', $data);
341 cfs_lock_file
('vzdump.cron', undef, $delete_job);
347 __PACKAGE__-
>register_method({
348 name
=> 'update_job',
352 description
=> "Update vzdump backup job definition.",
354 check
=> ['perm', '/', ['Sys.Modify']],
355 description
=> "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
358 additionalProperties
=> 0,
359 properties
=> PVE
::VZDump
::Common
::json_config_properties
({
360 id
=> $vzdump_job_id_prop,
362 description
=> "Backup schedule. The format is a subset of `systemd` calendar events.",
363 type
=> 'string', format
=> 'pve-calendar-event',
369 description
=> "Job Start time.",
370 pattern
=> '\d{1,2}:\d{1,2}',
375 type
=> 'string', format
=> 'pve-day-of-week-list',
377 requires
=> 'starttime',
378 description
=> "Day of week selection.",
381 type
=> 'string', format
=> 'pve-configid-list',
382 description
=> "A list of settings you want to delete.",
388 description
=> "Enable or disable the job.",
394 description
=> "If true, the job will be run as soon as possible if it was missed".
395 " while the scheduler was not running.",
401 description
=> "Description for the Job.",
406 returns
=> { type
=> 'null' },
410 my $rpcenv = PVE
::RPCEnvironment
::get
();
411 my $user = $rpcenv->get_user();
413 $assert_param_permission->($param, $user);
415 if (my $pool = $param->{pool
}) {
416 $rpcenv->check_pool_exist($pool);
417 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
420 $schedule_param_check->($param);
422 my $id = extract_param
($param, 'id');
423 my $delete = extract_param
($param, 'delete');
425 $delete = [PVE
::Tools
::split_list
($delete)];
428 my $update_job = sub {
429 my $data = cfs_read_file
('vzdump.cron');
430 my $jobs_data = cfs_read_file
('jobs.cfg');
432 my $jobs = $data->{jobs
} || [];
434 die "no options specified\n" if !scalar(keys %$param);
436 PVE
::VZDump
::verify_vzdump_parameters
($param);
437 my $opts = PVE
::VZDump
::JobBase-
>check_config($id, $param, 0, 1);
439 # try to find it in old vzdump.cron and convert it to a job
440 my ($idx) = grep { $jobs->[$_]->{id
} eq $id } (0 .. scalar(@$jobs) - 1);
444 $job = splice @$jobs, $idx, 1;
445 $job->{schedule
} = $convert_to_schedule->($job);
446 delete $job->{starttime
};
449 $job->{type
} = 'vzdump';
450 $jobs_data->{ids
}->{$id} = $job;
452 $job = $jobs_data->{ids
}->{$id};
453 die "no such vzdump job\n" if !$job || $job->{type
} ne 'vzdump';
458 'repeat-missed' => 1,
461 foreach my $k (@$delete) {
462 if (!PVE
::VZDump
::option_exists
($k) && !$deletable->{$k}) {
463 raise_param_exc
({ delete => "unknown option '$k'" });
469 foreach my $k (keys %$param) {
470 $job->{$k} = $param->{$k};
473 $job->{all
} = 1 if (defined($job->{exclude
}) && !defined($job->{pool
}));
475 if (defined($param->{vmid
})) {
477 delete $job->{exclude
};
479 } elsif ($param->{all
}) {
482 } elsif ($job->{pool
}) {
485 delete $job->{exclude
};
488 PVE
::VZDump
::verify_vzdump_parameters
($job, 1);
491 cfs_write_file
('vzdump.cron', $data);
493 cfs_write_file
('jobs.cfg', $jobs_data);
495 PVE
::Jobs
::detect_changed_runtime_props
($id, 'vzdump', $job);
499 cfs_lock_file
('vzdump.cron', undef, sub {
500 cfs_lock_file
('jobs.cfg', undef, $update_job);
506 __PACKAGE__-
>register_method({
507 name
=> 'get_volume_backup_included',
508 path
=> '{id}/included_volumes',
511 description
=> "Returns included guests and the backup status of their disks. Optimized to be used in ExtJS tree views.",
513 check
=> ['perm', '/', ['Sys.Audit']],
516 additionalProperties
=> 0,
518 id
=> $vzdump_job_id_prop
523 description
=> 'Root node of the tree object. Children represent guests, grandchildren represent volumes of that guest.',
532 description
=> 'VMID of the guest.',
536 description
=> 'Name of the guest',
541 description
=> 'Type of the guest, VM, CT or unknown for removed but not purged guests.',
542 enum
=> ['qemu', 'lxc', 'unknown'],
547 description
=> 'The volumes of the guest with the information if they will be included in backups.',
553 description
=> 'Configuration key of the volume.',
557 description
=> 'Name of the volume.',
561 description
=> 'Whether the volume is included in the backup or not.',
565 description
=> 'The reason why the volume is included (or excluded).',
578 my $rpcenv = PVE
::RPCEnvironment
::get
();
580 my $user = $rpcenv->get_user();
582 my $vzconf = cfs_read_file
('vzdump.cron');
583 my $all_jobs = $vzconf->{jobs
} || [];
585 my $rrd = PVE
::Cluster
::rrd_dump
();
587 for my $j (@$all_jobs) {
588 if ($j->{id
} eq $param->{id
}) {
594 my $jobs_data = cfs_read_file
('jobs.cfg');
595 my $j = $jobs_data->{ids
}->{$param->{id
}};
596 if ($j && $j->{type
} eq 'vzdump') {
600 raise_param_exc
({ id
=> "No such job '$param->{id}'" }) if !$job;
602 my $vmlist = PVE
::Cluster
::get_vmlist
();
606 my $included_guests = PVE
::VZDump
::get_included_guests
($job);
608 for my $node (keys %{$included_guests}) {
609 my $node_vmids = $included_guests->{$node};
610 push(@job_vmids, @{$node_vmids});
613 # remove VMIDs to which the user has no permission to not leak infos
614 # like the guest name
615 my @allowed_vmids = grep {
616 $rpcenv->check($user, "/vms/$_", [ 'VM.Audit' ], 1);
623 for my $vmid (@allowed_vmids) {
627 # It's possible that a job has VMIDs configured that are not in
628 # vmlist. This could be because a guest was removed but not purged.
629 # Since there is no more data available we can only deliver the VMID
631 if (!defined $vmlist->{ids
}->{$vmid}) {
632 push(@{$result->{children
}}, {
640 my $type = $vmlist->{ids
}->{$vmid}->{type
};
641 my $node = $vmlist->{ids
}->{$vmid}->{node
};
647 if ($type eq 'qemu') {
648 $conf = PVE
::QemuConfig-
>load_config($vmid, $node);
649 $volumes = PVE
::QemuConfig-
>get_backup_volumes($conf);
650 $name = $conf->{name
};
651 } elsif ($type eq 'lxc') {
652 $conf = PVE
::LXC
::Config-
>load_config($vmid, $node);
653 $volumes = PVE
::LXC
::Config-
>get_backup_volumes($conf);
654 $name = $conf->{hostname
};
656 die "VMID $vmid is neither Qemu nor LXC guest\n";
659 foreach my $volume (@$volumes) {
661 # id field must be unique for ExtJS tree view
662 id
=> "$vmid:$volume->{key}",
663 name
=> $volume->{volume_config
}->{file
} // $volume->{volume_config
}->{volume
},
664 included
=> $volume->{included
},
665 reason
=> $volume->{reason
},
668 push(@{$children}, $disk);
672 # it's possible for a guest to have no volumes configured
673 $leaf = 1 if !@{$children};
675 push(@{$result->{children
}}, {
679 children
=> $children,