[pmg-devel] [PATCH pmg-api v2 1/3] backup: pbs: prevent race in concurrent backups

Stoiko Ivanov s.ivanov at proxmox.com
Mon Mar 1 15:12:18 CET 2021


If two pbs backup-creation calls happen simultaneously, it is possible
that the first removes the backup dir before the other is done
creating or sending it to the pbs remote.

This patch takes the same route as non-PBS backups - creating a unique
tempdir indexed by remote, PID and current time.

the tmp-dir now also needs to be removed in case of error while
backing up. (before the next invocation would have wiped it).

Noticed while having 2 schedules to different PBS instances with the
same interval and w/o random delay.

Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
 src/PMG/API2/PBS/Job.pm | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/src/PMG/API2/PBS/Job.pm b/src/PMG/API2/PBS/Job.pm
index 2387422..c095d25 100644
--- a/src/PMG/API2/PBS/Job.pm
+++ b/src/PMG/API2/PBS/Job.pm
@@ -294,20 +294,28 @@ __PACKAGE__->register_method ({
 	$param->{statistic} //= $remote_config->{'include-statistics'} // 1;
 
 	my $pbs = PVE::PBSClient->new($remote_config, $remote, $conf->{secret_dir});
-	my $backup_dir = "/var/lib/pmg/backup/current";
+
+	my $time = time;
+	my $backup_dir = "/tmp/pbsbackup_${remote}_$$.$time";
 
 	my $worker = sub {
 	    my $upid = shift;
 
 	    print "starting update of current backup state\n";
 
-	    -d $backup_dir || mkdir $backup_dir;
-	    PMG::Backup::pmg_backup($backup_dir, $param->{statistic});
+	    eval {
+		-d $backup_dir || mkdir $backup_dir;
+		PMG::Backup::pmg_backup($backup_dir, $param->{statistic});
 
-	    $pbs->backup_fs_tree($backup_dir, $node, 'pmgbackup');
+		$pbs->backup_fs_tree($backup_dir, $node, 'pmgbackup');
 
-	    rmtree $backup_dir;
+		rmtree $backup_dir;
+	    };
+	    if (my $err = $@) {
+		rmtree $backup_dir;
+		die "backup failed: $err\n";
 
+	    }
 	    print "backup finished\n";
 
 	    my $group = "host/$node";
-- 
2.20.1





More information about the pmg-devel mailing list