From a528c44128c72e79613d750c6b4b9559ab10af28 Mon Sep 17 00:00:00 2001 From: Stoiko Ivanov Date: Mon, 1 Mar 2021 15:12:18 +0100 Subject: [PATCH] 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 --- 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.39.5