[lxc-devel] [lxd/master] lxd/backups: Cleanup on failure

stgraber on Github lxc-bot at linuxcontainers.org
Sat Mar 23 06:24:33 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 413 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190322/f277e371/attachment.bin>
-------------- next part --------------
From cda193ac5b3e814718f7bb61e2cf4730d05f5da0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Fri, 22 Mar 2019 19:16:11 -0700
Subject: [PATCH] lxd/backups: Cleanup on failure
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reported at: https://discuss.linuxcontainers.org/t/4380

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/backup.go        | 15 ++++++++++++++-
 lxd/storage_btrfs.go |  1 +
 lxd/storage_ceph.go  |  1 +
 lxd/storage_dir.go   |  1 +
 lxd/storage_lvm.go   |  1 +
 lxd/storage_zfs.go   |  1 +
 6 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/lxd/backup.go b/lxd/backup.go
index 60b5da0871..4bd0aeb515 100644
--- a/lxd/backup.go
+++ b/lxd/backup.go
@@ -376,6 +376,15 @@ func backupCreateTarball(s *state.State, path string, backup backup) error {
 
 	// Create the tarball
 	backupPath := shared.VarPath("backups", backup.name)
+	success := false
+	defer func() {
+		if success {
+			return
+		}
+
+		os.RemoveAll(backupPath)
+	}()
+
 	args := []string{"-cf", backupPath, "--xattrs", "-C", path, "--transform", "s,^./,backup/,", "."}
 	_, err = shared.RunCommand("tar", args...)
 	if err != nil {
@@ -404,7 +413,10 @@ func backupCreateTarball(s *state.State, path string, backup backup) error {
 		if err != nil {
 			return err
 		}
+		compressedName := compressed.Name()
+
 		defer compressed.Close()
+		defer os.Remove(compressedName)
 
 		err = compressFile(compress, infile, compressed)
 		if err != nil {
@@ -416,7 +428,7 @@ func backupCreateTarball(s *state.State, path string, backup backup) error {
 			return err
 		}
 
-		err = os.Rename(compressed.Name(), backupPath)
+		err = os.Rename(compressedName, backupPath)
 		if err != nil {
 			return err
 		}
@@ -428,6 +440,7 @@ func backupCreateTarball(s *state.State, path string, backup backup) error {
 		return err
 	}
 
+	success = true
 	return nil
 }
 
diff --git a/lxd/storage_btrfs.go b/lxd/storage_btrfs.go
index f3e7534cca..e371848e12 100644
--- a/lxd/storage_btrfs.go
+++ b/lxd/storage_btrfs.go
@@ -1819,6 +1819,7 @@ func (s *storageBtrfs) ContainerBackupCreate(backup backup, source container) er
 	if err != nil {
 		return err
 	}
+	defer os.RemoveAll(tmpPath)
 
 	// Generate the actual backup
 	if backup.optimizedStorage {
diff --git a/lxd/storage_ceph.go b/lxd/storage_ceph.go
index c7c9e2571d..0e379a6bdb 100644
--- a/lxd/storage_ceph.go
+++ b/lxd/storage_ceph.go
@@ -1987,6 +1987,7 @@ func (s *storageCeph) ContainerBackupCreate(backup backup, source container) err
 	if err != nil {
 		return err
 	}
+	defer os.RemoveAll(tmpPath)
 
 	// Generate the actual backup
 	if !backup.containerOnly {
diff --git a/lxd/storage_dir.go b/lxd/storage_dir.go
index 0aa31ee9bd..6fe7b3e5b1 100644
--- a/lxd/storage_dir.go
+++ b/lxd/storage_dir.go
@@ -1106,6 +1106,7 @@ func (s *storageDir) ContainerBackupCreate(backup backup, source container) erro
 	if err != nil {
 		return err
 	}
+	defer os.RemoveAll(tmpPath)
 
 	// Prepare for rsync
 	rsync := func(oldPath string, newPath string, bwlimit string) error {
diff --git a/lxd/storage_lvm.go b/lxd/storage_lvm.go
index 33f631db7d..0a9c3adb72 100644
--- a/lxd/storage_lvm.go
+++ b/lxd/storage_lvm.go
@@ -1697,6 +1697,7 @@ func (s *storageLvm) ContainerBackupCreate(backup backup, source container) erro
 	if err != nil {
 		return err
 	}
+	defer os.RemoveAll(tmpPath)
 
 	// Prepare for rsync
 	rsync := func(oldPath string, newPath string, bwlimit string) error {
diff --git a/lxd/storage_zfs.go b/lxd/storage_zfs.go
index 318c607550..0c0f169f4b 100644
--- a/lxd/storage_zfs.go
+++ b/lxd/storage_zfs.go
@@ -2136,6 +2136,7 @@ func (s *storageZfs) ContainerBackupCreate(backup backup, source container) erro
 	if err != nil {
 		return err
 	}
+	defer os.RemoveAll(tmpPath)
 
 	// Generate the actual backup
 	if backup.optimizedStorage {


More information about the lxc-devel mailing list