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

Stoiko Ivanov s.ivanov at proxmox.com
Wed Feb 24 19:31:02 CET 2021


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

non-PBS backups are not affected, since they create the files for
tar in a tempdir (indexed by PID and current time).

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 | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/PMG/API2/PBS/Job.pm b/src/PMG/API2/PBS/Job.pm
index 279afbc..e5dcb9c 100644
--- a/src/PMG/API2/PBS/Job.pm
+++ b/src/PMG/API2/PBS/Job.pm
@@ -303,13 +303,14 @@ __PACKAGE__->register_method ({
 
 	my $pbs = PVE::PBSClient->new($remote_config, $remote, $conf->{secret_dir});
 	my $backup_dir = "/var/lib/pmg/backup/current";
+	my $lockfile = "/var/lock/pmg-pbs-backup.lck";
 
 	my $worker = sub {
 	    my $upid = shift;
 
 	    my $log = "starting update of current backup state\n";
 
-	    eval {
+	    my $create_backup = sub {
 		-d $backup_dir || mkdir $backup_dir;
 		PMG::Backup::pmg_backup($backup_dir, $param->{statistic});
 
@@ -317,6 +318,10 @@ __PACKAGE__->register_method ({
 
 		rmtree $backup_dir;
 	    };
+
+	    eval {
+		PVE::Tools::lock_file($lockfile, undef, $create_backup);
+	    };
 	    my $err = $@;
 	    $log .= $err if $err;
 
-- 
2.20.1





More information about the pmg-devel mailing list