Add prune_backups to storage API

Implement it for generic storages supporting backups
(i.e. directory-based storages) and add a wrapper for PBS.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
This commit is contained in:
Fabian Ebner
2020-07-09 14:45:42 +02:00
committed by Thomas Lamprecht
parent 3353698f45
commit 8f26b3910d
5 changed files with 565 additions and 2 deletions

View File

@ -40,11 +40,11 @@ use PVE::Storage::DRBDPlugin;
use PVE::Storage::PBSPlugin;
# Storage API version. Icrement it on changes in storage API interface.
use constant APIVER => 5;
use constant APIVER => 6;
# Age is the number of versions we're backward compatible with.
# This is like having 'current=APIVER' and age='APIAGE' in libtool,
# see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
use constant APIAGE => 4;
use constant APIAGE => 5;
# load standard plugins
PVE::Storage::DirPlugin->register();
@ -1542,6 +1542,93 @@ sub extract_vzdump_config {
}
}
sub prune_backups {
my ($cfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
my $scfg = storage_config($cfg, $storeid);
die "storage '$storeid' does not support backups\n" if !$scfg->{content}->{backup};
if (!defined($keep)) {
die "no prune-backups options configured for storage '$storeid'\n"
if !defined($scfg->{'prune-backups'});
$keep = PVE::JSONSchema::parse_property_string('prune-backups', $scfg->{'prune-backups'});
}
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
return $plugin->prune_backups($scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc);
}
my $prune_mark = sub {
my ($prune_entries, $keep_count, $id_func) = @_;
return if !$keep_count;
my $already_included = {};
my $newly_included = {};
foreach my $prune_entry (@{$prune_entries}) {
my $mark = $prune_entry->{mark};
my $id = $id_func->($prune_entry->{ctime});
next if $already_included->{$id};
if (defined($mark)) {
$already_included->{$id} = 1 if $mark eq 'keep';
next;
}
if (!$newly_included->{$id}) {
last if scalar(keys %{$newly_included}) >= $keep_count;
$newly_included->{$id} = 1;
$prune_entry->{mark} = 'keep';
} else {
$prune_entry->{mark} = 'remove';
}
}
};
sub prune_mark_backup_group {
my ($backup_group, $keep) = @_;
my $prune_list = [ sort { $b->{ctime} <=> $a->{ctime} } @{$backup_group} ];
$prune_mark->($prune_list, $keep->{'keep-last'}, sub {
my ($ctime) = @_;
return $ctime;
});
$prune_mark->($prune_list, $keep->{'keep-hourly'}, sub {
my ($ctime) = @_;
my (undef, undef, $hour, $day, $month, $year) = localtime($ctime);
return "$hour/$day/$month/$year";
});
$prune_mark->($prune_list, $keep->{'keep-daily'}, sub {
my ($ctime) = @_;
my (undef, undef, undef, $day, $month, $year) = localtime($ctime);
return "$day/$month/$year";
});
$prune_mark->($prune_list, $keep->{'keep-weekly'}, sub {
my ($ctime) = @_;
my ($sec, $min, $hour, $day, $month, $year) = localtime($ctime);
my $iso_week = int(strftime("%V", $sec, $min, $hour, $day, $month - 1, $year - 1900));
my $iso_week_year = int(strftime("%G", $sec, $min, $hour, $day, $month - 1, $year - 1900));
return "$iso_week/$iso_week_year";
});
$prune_mark->($prune_list, $keep->{'keep-monthly'}, sub {
my ($ctime) = @_;
my (undef, undef, undef, undef, $month, $year) = localtime($ctime);
return "$month/$year";
});
$prune_mark->($prune_list, $keep->{'keep-yearly'}, sub {
my ($ctime) = @_;
my $year = (localtime($ctime))[5];
return "$year";
});
foreach my $prune_entry (@{$prune_list}) {
$prune_entry->{mark} //= 'remove';
}
}
sub volume_export {
my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;

View File

@ -266,6 +266,74 @@ sub extract_vzdump_config {
return $config;
}
sub prune_backups {
my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
$logfunc //= sub { print "$_[1]\n" };
my $backups = $class->list_volumes($storeid, $scfg, $vmid, ['backup']);
$type = 'vm' if defined($type) && $type eq 'qemu';
$type = 'ct' if defined($type) && $type eq 'lxc';
my $backup_groups = {};
foreach my $backup (@{$backups}) {
(my $backup_type = $backup->{format}) =~ s/^pbs-//;
next if defined($type) && $backup_type ne $type;
my $backup_group = "$backup_type/$backup->{vmid}";
$backup_groups->{$backup_group} = 1;
}
my @param;
foreach my $opt (keys %{$keep}) {
push @param, "--$opt";
push @param, "$keep->{$opt}";
}
push @param, '--dry-run' if $dryrun;
my $prune_list = [];
my $failed;
foreach my $backup_group (keys %{$backup_groups}) {
$logfunc->('info', "running 'proxmox-backup-client prune' for '$backup_group'")
if !$dryrun;
eval {
my $res = run_client_cmd($scfg, $storeid, 'prune', [ $backup_group, @param ]);
foreach my $backup (@{$res}) {
die "result from proxmox-backup-client is not as expected\n"
if !defined($backup->{'backup-time'})
|| !defined($backup->{'backup-type'})
|| !defined($backup->{'backup-id'})
|| !defined($backup->{'keep'});
my $ctime = $backup->{'backup-time'};
my $type = $backup->{'backup-type'};
my $vmid = $backup->{'backup-id'};
my $volid = print_volid($storeid, $type, $vmid, $ctime);
push @{$prune_list}, {
ctime => $ctime,
mark => $backup->{keep} ? 'keep' : 'remove',
type => $type eq 'vm' ? 'qemu' : 'lxc',
vmid => $vmid,
volid => $volid,
};
}
};
if (my $err = $@) {
$logfunc->('err', "prune '$backup_group': $err\n");
$failed = 1;
}
}
die "error pruning backups - check log\n" if $failed;
return $prune_list;
}
my $autogen_encryption_key = sub {
my ($scfg, $storeid) = @_;
my $encfile = pbs_encryption_key_file_name($scfg, $storeid);

View File

@ -1174,6 +1174,71 @@ sub check_connection {
return 1;
}
sub prune_backups {
my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
$logfunc //= sub { print "$_[1]\n" };
my $backups = $class->list_volumes($storeid, $scfg, $vmid, ['backup']);
my $backup_groups = {};
my $prune_list = [];
foreach my $backup (@{$backups}) {
my $volid = $backup->{volid};
my $backup_vmid = $backup->{vmid};
my $archive_info = eval { PVE::Storage::archive_info($volid) } // {};
my $backup_type = $archive_info->{type} // 'unknown';
next if defined($type) && $type ne $backup_type;
my $prune_entry = {
ctime => $backup->{ctime},
type => $backup_type,
volid => $volid,
};
$prune_entry->{vmid} = $backup_vmid if defined($backup_vmid);
if ($archive_info->{is_std_name}) {
$prune_entry->{ctime} = $archive_info->{ctime};
my $group = "$backup_type/$backup_vmid";
push @{$backup_groups->{$group}}, $prune_entry;
} else {
# ignore backups that don't use the standard naming scheme
$prune_entry->{mark} = 'protected';
}
push @{$prune_list}, $prune_entry;
}
foreach my $backup_group (values %{$backup_groups}) {
PVE::Storage::prune_mark_backup_group($backup_group, $keep);
}
my $failed;
if (!$dryrun) {
foreach my $prune_entry (@{$prune_list}) {
next if $prune_entry->{mark} ne 'remove';
my $volid = $prune_entry->{volid};
$logfunc->('info', "removing backup '$volid'");
eval {
my (undef, $volname) = parse_volume_id($volid);
my $archive_path = $class->filesystem_path($scfg, $volname);
PVE::Storage::archive_remove($archive_path);
};
if (my $err = $@) {
$logfunc->('err', "error when removing backup '$volid' - $err\n");
$failed = 1;
}
}
}
die "error pruning backups - check log\n" if $failed;
return $prune_list;
}
# Import/Export interface:
# Any path based storage is assumed to support 'raw' and 'tar' streams, so
# the default implementations will return this if $scfg->{path} is set,