[pve-devel] [PATCH container 2/2] backup: refactor mp included in backup to use from other modules

Aaron Lauterer a.lauterer at proxmox.com
Thu Jan 16 14:00:32 CET 2020


This refactor will allow us to use the same code that decides which
mountpoint will be backed up in multiple places and provide a reason for
the decision.

Signed-off-by: Aaron Lauterer <a.lauterer at proxmox.com>
---
 src/PVE/LXC/Config.pm | 38 ++++++++++++++++++++++++++++++++++++++
 src/PVE/VZDump/LXC.pm | 42 +++++++++++++++++++++++++-----------------
 2 files changed, 63 insertions(+), 17 deletions(-)

diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm
index 760ec23..1dcfc40 100644
--- a/src/PVE/LXC/Config.pm
+++ b/src/PVE/LXC/Config.pm
@@ -1558,4 +1558,42 @@ sub get_replicatable_volumes {
     return $volhash;
 }
 
+sub get_volumes_backup_status {
+    my ($class, $conf) = @_;
+
+    my $volhash = {};
+
+    my $test_volid = sub {
+	my ($volid, $attr) = @_;
+
+	return if !$volid;
+
+	my $status = $class->mountpoint_backup_enabled($volid, $attr);
+	my $reason = 'not able to determine reason';
+
+	if ($status == 0) {
+	    if ($attr->{type} ne 'volume') {
+		$reason = 'not a volume';
+	    } else {
+		$reason = 'default exclude';
+	    }
+	} elsif ($status == 1) {
+	    if ($volid eq 'rootfs') {
+		$reason = 'rootfs';
+	    } elsif (exists($attr->{backup}) && $attr->{backup}) {
+		$reason = 'manual';
+	    }
+	}
+
+	$volhash->{$volid}->{included} = $status;
+	$volhash->{$volid}->{reason} = $reason;
+	$volhash->{$volid}->{volume} = $attr->{volume};
+	$volhash->{$volid}->{data} = $attr;
+    };
+
+    PVE::LXC::Config->foreach_mountpoint($conf, $test_volid);
+
+    return $volhash;
+}
+
 1;
diff --git a/src/PVE/VZDump/LXC.pm b/src/PVE/VZDump/LXC.pm
index 0260184..df60d08 100644
--- a/src/PVE/VZDump/LXC.pm
+++ b/src/PVE/VZDump/LXC.pm
@@ -118,24 +118,32 @@ sub prepare {
     $task->{rootgid} = $rootgid;
 
     my $volids = $task->{volids} = [];
-    PVE::LXC::Config->foreach_mountpoint($conf, sub {
-	my ($name, $data) = @_;
-	my $volid = $data->{volume};
-	my $mount = $data->{mp};
-	my $type = $data->{type};
-
-	return if !$volid || !$mount;
-
-	if (!PVE::LXC::Config->mountpoint_backup_enabled($name, $data)) {
-	    push @$exclude_dirs, $mount;
-	    $self->loginfo("excluding $type mount point $name ('$mount') from backup");
-	    return;
-	}
 
-	push @$disks, $data;
-	push @$volids, $volid
-	    if $type eq 'volume';
-    });
+    my $backup_status = PVE::LXC::Config->get_volumes_backup_status($conf);
+
+    # mp order is important. rootfs needs to be processed before mpX
+    my @mp_keys = sort keys %{$backup_status};
+    unshift(@mp_keys, pop @mp_keys);
+
+    foreach my $name (@mp_keys) {
+        my $disk = $backup_status->{$name};
+
+        my $volid = $disk->{data}->{volume};
+        my $mount = $disk->{data}->{mp};
+        my $type = $disk->{data}->{type};
+
+        return if !$volid || !$mount;
+
+        if (exists $disk->{included} && !$disk->{included}) {
+             push @$exclude_dirs, $mount;
+             $self->loginfo("excluding $type mount point $name ('$mount') from backup");
+             next;
+         }
+
+         push @$disks, $disk->{data};
+         push @$volids, $volid
+            if $disk->{included};
+    }
 
     if ($mode eq 'snapshot') {
 	if (!PVE::LXC::Config->has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
-- 
2.20.1





More information about the pve-devel mailing list