public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api v2 1/3] backup: pbs: prevent race in concurrent backups
Date: Mon,  1 Mar 2021 15:12:18 +0100	[thread overview]
Message-ID: <20210301141225.18394-2-s.ivanov@proxmox.com> (raw)
In-Reply-To: <20210301141225.18394-1-s.ivanov@proxmox.com>

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@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





  reply	other threads:[~2021-03-01 14:13 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-01 14:12 [pmg-devel] [PATCH pmg-api/gui/docs v2] small improvments for PBS integration Stoiko Ivanov
2021-03-01 14:12 ` Stoiko Ivanov [this message]
2021-03-05 21:42   ` [pmg-devel] applied: [PATCH pmg-api v2 1/3] backup: pbs: prevent race in concurrent backups Thomas Lamprecht
2021-03-01 14:12 ` [pmg-devel] [PATCH pmg-api v2 2/3] backup: fix #3146 add email notifications Stoiko Ivanov
2021-03-05 21:43   ` [pmg-devel] applied: " Thomas Lamprecht
2021-03-01 14:12 ` [pmg-devel] [PATCH pmg-api v2 3/3] backup: add notify parameter to 'classic' backup Stoiko Ivanov
2021-03-05 21:43   ` [pmg-devel] applied: " Thomas Lamprecht
2021-03-01 14:12 ` [pmg-devel] [PATCH pmg-gui v2 1/4] backup: pbs: add onlineHelp anchors Stoiko Ivanov
2021-03-01 14:12 ` [pmg-devel] [PATCH pmg-gui v2 2/4] backup: fix #3154: make statistic backup optional Stoiko Ivanov
2021-03-01 14:12 ` [pmg-devel] [PATCH pmg-gui v2 3/4] backup: pbs: fix #3154: add statistic setting to remote Stoiko Ivanov
2021-03-01 14:12 ` [pmg-devel] [PATCH pmg-gui v2 4/4] backup: pbs: fix #3146 add notify " Stoiko Ivanov
2021-03-01 14:12 ` [pmg-devel] [PATCH pmg-docs v2 1/1] backup: shortly document #3146 and #3154 Stoiko Ivanov
2021-03-05 21:45   ` [pmg-devel] applied: " Thomas Lamprecht
2021-03-05 22:07 ` [pmg-devel] applied: [PATCH pmg-api/gui/docs v2] small improvments for PBS integration Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210301141225.18394-2-s.ivanov@proxmox.com \
    --to=s.ivanov@proxmox.com \
    --cc=pmg-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal