From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 3D77660E41 for ; Mon, 19 Oct 2020 21:02:58 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 362862F1BE for ; Mon, 19 Oct 2020 21:02:28 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 845002F10D for ; Mon, 19 Oct 2020 21:02:21 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 55DD645E25 for ; Mon, 19 Oct 2020 21:02:21 +0200 (CEST) From: Stoiko Ivanov To: pmg-devel@lists.proxmox.com Date: Mon, 19 Oct 2020 21:01:59 +0200 Message-Id: <20201019190209.11495-3-s.ivanov@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201019190209.11495-1-s.ivanov@proxmox.com> References: <20201019190209.11495-1-s.ivanov@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.315 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_SBL 0.644 Contains an URL's NS IP listed in the Spamhaus SBL blocklist [backup.pm] URIBL_SBL_A 0.1 Contains URL's A record listed in the Spamhaus SBL blocklist [backup.pm] Subject: [pmg-devel] [RFC pmg-api 02/12] Backup: split backup creation and creating tar X-BeenThere: pmg-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Mail Gateway development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 Oct 2020 19:02:58 -0000 In preparation for integrating PMG with PBS split the current creation of a PMG backup into 2 methods: * create all files in a backup in a target directory * create a tarball from a backup in a temporary directory use the changed method in the backup API call. Signed-off-by: Stoiko Ivanov --- src/PMG/API2/Backup.pm | 2 +- src/PMG/Backup.pm | 41 ++++++++++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/PMG/API2/Backup.pm b/src/PMG/API2/Backup.pm index 0bfcfc9..08c06b5 100644 --- a/src/PMG/API2/Backup.pm +++ b/src/PMG/API2/Backup.pm @@ -131,7 +131,7 @@ __PACKAGE__->register_method ({ print "starting backup to: $filename\n"; - PMG::Backup::pmg_backup($filename, $param->{statistic}); + PMG::Backup::pmg_backup_pack($filename, $param->{statistic}); print "backup finished\n"; diff --git a/src/PMG/Backup.pm b/src/PMG/Backup.pm index 025bac2..3854a5d 100644 --- a/src/PMG/Backup.pm +++ b/src/PMG/Backup.pm @@ -133,10 +133,11 @@ sub dumpstatdb { } sub pmg_backup { - my ($filename, $include_statistics) = @_; + my ($backupdir, $include_statistics) = @_; + my $time = time; - my $dirname = "/tmp/proxbackup_$$.$time"; + my $dirname = $backupdir // '/var/lib/pmg/backup/current'; my $dbfn = "Proxmox_ruledb.sql"; my $statfn = "Proxmox_statdb.sql"; my $tarfn = "config_backup.tar"; @@ -145,12 +146,7 @@ sub pmg_backup { eval { - my $targetdir = dirname($filename); - mkdir $targetdir; # try to create target dir - -d $targetdir || - die "unable to access target directory '$targetdir'\n"; - - # create a temporary directory + # create backup directory mkdir $dirname; # dump the database first @@ -197,7 +193,34 @@ sub pmg_backup { system("cd $dirname; md5sum $tarfn $dbfn $extradb $verfn> $sigfn") == 0 || die "unable to create backup signature: ERROR"; - system("rm -f $filename; tar czf $filename -C $dirname $verfn $sigfn $dbfn $extradb $tarfn") == 0 || + }; + my $err = $@; + + if ($err) { + die $err; + } +} + +sub pmg_backup_pack { + my ($filename, $include_statistics) = @_; + + my $time = time; + my $dirname = "/tmp/proxbackup_$$.$time"; + + eval { + + my $targetdir = dirname($filename); + mkdir $targetdir; # try to create target dir + -d $targetdir || + die "unable to access target directory '$targetdir'\n"; + + rmtree $dirname; + # create backup directory + mkdir $dirname; + + pmg_backup($dirname, $include_statistics); + + system("rm -f $filename; tar czf $filename --strip-components=1 -C $dirname .") == 0 || die "unable to create backup archive: ERROR"; }; my $err = $@; -- 2.20.1