From: Stoiko Ivanov Date: Mon, 1 Mar 2021 14:12:18 +0000 (+0100) Subject: backup: pbs: prevent race in concurrent backups X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=a528c44128c72e79613d750c6b4b9559ab10af28;p=pmg-api.git backup: pbs: prevent race in concurrent backups 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 Signed-off-by: Thomas Lamprecht --- 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";