[lxc-devel] [lxd/master] Instance: Moves backups from shared.VarPath("backups") to shared.VarPath("backups", "instances")

tomponline on Github lxc-bot at linuxcontainers.org
Fri Sep 18 15:08:57 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 431 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200918/513828f1/attachment.bin>
-------------- next part --------------
From 13cc075b6270508f205779751273641cf4f7157c Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 18 Sep 2020 16:05:50 +0100
Subject: [PATCH 1/9] lxd/backup: Adds WorkingDirPrefix constant

To link all variations that use temporary working directories in the codebase.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/backup/backup.go | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lxd/backup/backup.go b/lxd/backup/backup.go
index 5d20ac00f9..b4dd5736fa 100644
--- a/lxd/backup/backup.go
+++ b/lxd/backup/backup.go
@@ -17,6 +17,9 @@ import (
 	"github.com/lxc/lxd/shared/api"
 )
 
+// WorkingDirPrefix is used when temporary working directories are needed.
+const WorkingDirPrefix = "lxd_backup_"
+
 // Instance represents the backup relevant subset of a LXD instance.
 // This is used rather than instance.Instance to avoid import loops.
 type Instance interface {

From fb9935e94507258e39680b6c78906da86c672781 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 18 Sep 2020 16:06:47 +0100
Subject: [PATCH 2/9] lxd: backup.WorkingDirPrefix usage

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instances_post.go                       | 4 ++--
 lxd/storage/drivers/driver_btrfs_volumes.go | 2 +-
 lxd/storage/drivers/driver_zfs_volumes.go   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lxd/instances_post.go b/lxd/instances_post.go
index b3a83e89b8..cbe58a1bba 100644
--- a/lxd/instances_post.go
+++ b/lxd/instances_post.go
@@ -550,7 +550,7 @@ func createFromBackup(d *Daemon, project string, data io.Reader, pool string) re
 	defer revert.Fail()
 
 	// Create temporary file to store uploaded backup data.
-	backupFile, err := ioutil.TempFile(shared.VarPath("backups"), "lxd_backup_")
+	backupFile, err := ioutil.TempFile(shared.VarPath("backups"), fmt.Sprintf("%s_", backup.WorkingDirPrefix))
 	if err != nil {
 		return response.InternalError(err)
 	}
@@ -575,7 +575,7 @@ func createFromBackup(d *Daemon, project string, data io.Reader, pool string) re
 		decomArgs := append(decomArgs, backupFile.Name())
 
 		// Create temporary file to store the decompressed tarball in.
-		tarFile, err := ioutil.TempFile(shared.VarPath("backups"), "lxd_backup_decompress_")
+		tarFile, err := ioutil.TempFile(shared.VarPath("backups"), fmt.Sprintf("%s_decompress_", backup.WorkingDirPrefix))
 		if err != nil {
 			return response.InternalError(err)
 		}
diff --git a/lxd/storage/drivers/driver_btrfs_volumes.go b/lxd/storage/drivers/driver_btrfs_volumes.go
index 222efcd031..ebe7dc5334 100644
--- a/lxd/storage/drivers/driver_btrfs_volumes.go
+++ b/lxd/storage/drivers/driver_btrfs_volumes.go
@@ -1029,7 +1029,7 @@ func (d *btrfs) BackupVolume(vol Volume, tarWriter *instancewriter.InstanceTarWr
 
 		// Create temporary file to store output of btrfs send.
 		backupsPath := shared.VarPath("backups")
-		tmpFile, err := ioutil.TempFile(backupsPath, "lxd_backup_btrfs")
+		tmpFile, err := ioutil.TempFile(backupsPath, fmt.Sprintf("%s_btrfs", backup.WorkingDirPrefix))
 		if err != nil {
 			return errors.Wrapf(err, "Failed to open temporary file for BTRFS backup")
 		}
diff --git a/lxd/storage/drivers/driver_zfs_volumes.go b/lxd/storage/drivers/driver_zfs_volumes.go
index 5c548061e0..5cae1d92d1 100644
--- a/lxd/storage/drivers/driver_zfs_volumes.go
+++ b/lxd/storage/drivers/driver_zfs_volumes.go
@@ -1390,7 +1390,7 @@ func (d *zfs) BackupVolume(vol Volume, tarWriter *instancewriter.InstanceTarWrit
 
 		// Create temporary file to store output of ZFS send.
 		backupsPath := shared.VarPath("backups")
-		tmpFile, err := ioutil.TempFile(backupsPath, "lxd_backup_zfs")
+		tmpFile, err := ioutil.TempFile(backupsPath, fmt.Sprintf("%s_zfs", backup.WorkingDirPrefix))
 		if err != nil {
 			return errors.Wrapf(err, "Failed to open temporary file for ZFS backup")
 		}

From c461d50b923bc6151fa926afc665aa5158eef9b0 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 18 Sep 2020 15:25:42 +0100
Subject: [PATCH 3/9] lxd/backup: Updates backupCreate to store backups in
 backups/instances

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/backup.go | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lxd/backup.go b/lxd/backup.go
index a938311e14..3014912073 100644
--- a/lxd/backup.go
+++ b/lxd/backup.go
@@ -83,7 +83,7 @@ func backupCreate(s *state.State, args db.InstanceBackup, sourceInst instance.In
 	}
 
 	// Create the target path if needed.
-	backupsPath := shared.VarPath("backups", project.Instance(sourceInst.Project(), sourceInst.Name()))
+	backupsPath := shared.VarPath("backups", "instances", project.Instance(sourceInst.Project(), sourceInst.Name()))
 	if !shared.PathExists(backupsPath) {
 		err := os.MkdirAll(backupsPath, 0700)
 		if err != nil {
@@ -93,7 +93,7 @@ func backupCreate(s *state.State, args db.InstanceBackup, sourceInst instance.In
 		revert.Add(func() { os.Remove(backupsPath) })
 	}
 
-	target := shared.VarPath("backups", project.Instance(sourceInst.Project(), b.Name()))
+	target := shared.VarPath("backups", "instances", project.Instance(sourceInst.Project(), b.Name()))
 
 	// Setup the tarball writer.
 	logger.Debug("Opening backup tarball for writing", log.Ctx{"path": target})

From 61ea832674555527b831fee5986d4cd7e2f7a34c Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 18 Sep 2020 15:26:34 +0100
Subject: [PATCH 4/9] lxd/backup: Updates Rename to support new backup location

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/backup/backup.go | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lxd/backup/backup.go b/lxd/backup/backup.go
index b4dd5736fa..5f924d3a6e 100644
--- a/lxd/backup/backup.go
+++ b/lxd/backup/backup.go
@@ -179,13 +179,13 @@ func (b *Backup) OptimizedStorage() bool {
 	return b.optimizedStorage
 }
 
-// Rename renames a container backup
+// Rename renames an instance backup.
 func (b *Backup) Rename(newName string) error {
-	oldBackupPath := shared.VarPath("backups", project.Instance(b.instance.Project(), b.name))
-	newBackupPath := shared.VarPath("backups", project.Instance(b.instance.Project(), newName))
+	oldBackupPath := shared.VarPath("backups", "instances", project.Instance(b.instance.Project(), b.name))
+	newBackupPath := shared.VarPath("backups", "instances", project.Instance(b.instance.Project(), newName))
 
-	// Create the new backup path
-	backupsPath := shared.VarPath("backups", project.Instance(b.instance.Project(), b.instance.Name()))
+	// Create the new backup path.
+	backupsPath := shared.VarPath("backups", "instances", project.Instance(b.instance.Project(), b.instance.Name()))
 	if !shared.PathExists(backupsPath) {
 		err := os.MkdirAll(backupsPath, 0700)
 		if err != nil {

From 620630dc7d346357cd84cff922636a615668e359 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 18 Sep 2020 15:26:52 +0100
Subject: [PATCH 5/9] lxd/backup: Rename comment ending

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/backup/backup.go | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lxd/backup/backup.go b/lxd/backup/backup.go
index 5f924d3a6e..ef1c0d18d4 100644
--- a/lxd/backup/backup.go
+++ b/lxd/backup/backup.go
@@ -193,13 +193,13 @@ func (b *Backup) Rename(newName string) error {
 		}
 	}
 
-	// Rename the backup directory
+	// Rename the backup directory.
 	err := os.Rename(oldBackupPath, newBackupPath)
 	if err != nil {
 		return err
 	}
 
-	// Check if we can remove the container directory
+	// Check if we can remove the instance directory.
 	empty, _ := shared.PathIsEmpty(backupsPath)
 	if empty {
 		err := os.Remove(backupsPath)
@@ -208,7 +208,7 @@ func (b *Backup) Rename(newName string) error {
 		}
 	}
 
-	// Rename the database record
+	// Rename the database record.
 	err = b.state.Cluster.RenameInstanceBackup(b.name, newName)
 	if err != nil {
 		return err

From 5d740dcd5ecc254d92742d2245586f9e6f9619be Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 18 Sep 2020 15:27:14 +0100
Subject: [PATCH 6/9] lxd/backup: Updates DoBackupDelete to handle new backup
 location

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/backup/backup.go | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/lxd/backup/backup.go b/lxd/backup/backup.go
index ef1c0d18d4..2147d717c8 100644
--- a/lxd/backup/backup.go
+++ b/lxd/backup/backup.go
@@ -235,10 +235,10 @@ func (b *Backup) Render() *api.InstanceBackup {
 }
 
 // DoBackupDelete deletes a backup.
-func DoBackupDelete(s *state.State, projectName, backupName, containerName string) error {
-	backupPath := shared.VarPath("backups", project.Instance(projectName, backupName))
+func DoBackupDelete(s *state.State, projectName, backupName, instanceName string) error {
+	backupPath := shared.VarPath("backups", "instances", project.Instance(projectName, backupName))
 
-	// Delete the on-disk data
+	// Delete the on-disk data.
 	if shared.PathExists(backupPath) {
 		err := os.RemoveAll(backupPath)
 		if err != nil {
@@ -246,8 +246,8 @@ func DoBackupDelete(s *state.State, projectName, backupName, containerName strin
 		}
 	}
 
-	// Check if we can remove the container directory
-	backupsPath := shared.VarPath("backups", project.Instance(projectName, containerName))
+	// Check if we can remove the instance directory.
+	backupsPath := shared.VarPath("backups", "instances", project.Instance(projectName, instanceName))
 	empty, _ := shared.PathIsEmpty(backupsPath)
 	if empty {
 		err := os.Remove(backupsPath)

From 5b483afe3786f6abc0a8dd4c71283341c8cac7f9 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 18 Sep 2020 15:27:28 +0100
Subject: [PATCH 7/9] lxd/backup: DoBackupDelete comment ending

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/backup/backup.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lxd/backup/backup.go b/lxd/backup/backup.go
index 2147d717c8..d20b2a7b3d 100644
--- a/lxd/backup/backup.go
+++ b/lxd/backup/backup.go
@@ -256,7 +256,7 @@ func DoBackupDelete(s *state.State, projectName, backupName, instanceName string
 		}
 	}
 
-	// Remove the database record
+	// Remove the database record.
 	err := s.Cluster.DeleteInstanceBackup(backupName)
 	if err != nil {
 		return err

From f7610f06f044e0c090010f534974ba266f90bfea Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 18 Sep 2020 15:27:43 +0100
Subject: [PATCH 8/9] lxd/instance/backup: Updates containerBackupExportGet to
 support new backup location

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instance_backup.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lxd/instance_backup.go b/lxd/instance_backup.go
index f4fd540316..4f9dfd8011 100644
--- a/lxd/instance_backup.go
+++ b/lxd/instance_backup.go
@@ -351,7 +351,7 @@ func containerBackupExportGet(d *Daemon, r *http.Request) response.Response {
 	}
 
 	ent := response.FileResponseEntry{
-		Path: shared.VarPath("backups", project.Instance(proj, backup.Name())),
+		Path: shared.VarPath("backups", "instances", project.Instance(proj, backup.Name())),
 	}
 
 	return response.FileResponse(r, []response.FileResponseEntry{ent}, nil, false)

From 68ca95c7cb50a3267286a4e02b7c7893791e2b0d Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 18 Sep 2020 15:55:39 +0100
Subject: [PATCH 9/9] lxd/patches: Adds patchMoveBackupsInstances to move
 backups into backups/instances dir

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/patches.go | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/lxd/patches.go b/lxd/patches.go
index a5d849721f..2ac752323e 100644
--- a/lxd/patches.go
+++ b/lxd/patches.go
@@ -99,6 +99,7 @@ var patches = []patch{
 	{name: "storage_lvm_skipactivation", stage: patchPostDaemonStorage, run: patchGenericStorage},
 	{name: "clustering_drop_database_role", stage: patchPostDaemonStorage, run: patchClusteringDropDatabaseRole},
 	{name: "network_clear_bridge_volatile_hwaddr", stage: patchPostDaemonStorage, run: patchNetworkCearBridgeVolatileHwaddr},
+	{name: "move_backups_instances", stage: patchPostDaemonStorage, run: patchMoveBackupsInstances},
 }
 
 type patch struct {
@@ -162,6 +163,42 @@ func patchesApply(d *Daemon, stage patchStage) error {
 }
 
 // Patches begin here
+
+// Moves backups from shared.VarPath("backups") to shared.VarPath("backups", "instances").
+func patchMoveBackupsInstances(name string, d *Daemon) error {
+	if !shared.PathExists(shared.VarPath("backups")) {
+		return nil // Nothing to do, no backups directory.
+	}
+
+	backupsPath := shared.VarPath("backups", "instances")
+
+	err := os.MkdirAll(backupsPath, 0700)
+	if err != nil {
+		return errors.Wrapf(err, "Failed creating instances backup directory %q", backupsPath)
+	}
+
+	backups, err := ioutil.ReadDir(shared.VarPath("backups"))
+	if err != nil {
+		return errors.Wrapf(err, "Failed listing existing backup directory %q", shared.VarPath("backups"))
+	}
+
+	for _, backup := range backups {
+		if backup.Name() == "instances" || strings.HasPrefix(backup.Name(), "lxd_backup_") {
+			continue // Don't try and move our new instances directory or temporary directories.
+		}
+
+		oldPath := shared.VarPath("backups", backup.Name())
+		newPath := filepath.Join(backupsPath, backup.Name())
+		logger.Debugf("Moving backup from %q to %q", oldPath, newPath)
+		err = os.Rename(oldPath, newPath)
+		if err != nil {
+			return errors.Wrapf(err, "Failed moving backup from %q to %q", oldPath, newPath)
+		}
+	}
+
+	return nil
+}
+
 func patchNetworkPIDFiles(name string, d *Daemon) error {
 	networks, err := ioutil.ReadDir(shared.VarPath("networks"))
 	if err != nil {


More information about the lxc-devel mailing list