Skip to content

Commit

Permalink
Merge pull request #12395 from MusicDin/fix/export-instance-only
Browse files Browse the repository at this point in the history
Respect instance-only flag when generating backup.yaml
  • Loading branch information
tomponline authored Oct 17, 2023
2 parents 5185258 + 371b55f commit 0eed2a3
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lxd/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8283,7 +8283,7 @@ func (d *lxc) UpdateBackupFile() error {
return err
}

return pool.UpdateInstanceBackupFile(d, nil)
return pool.UpdateInstanceBackupFile(d, true, nil)
}

// Info returns "lxc" and the currently loaded version of LXC.
Expand Down
2 changes: 1 addition & 1 deletion lxd/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7736,7 +7736,7 @@ func (d *qemu) UpdateBackupFile() error {
return err
}

return pool.UpdateInstanceBackupFile(d, nil)
return pool.UpdateInstanceBackupFile(d, true, nil)
}

type cpuTopology struct {
Expand Down
8 changes: 4 additions & 4 deletions lxd/storage/backend_lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2516,7 +2516,7 @@ func (b *lxdBackend) BackupInstance(inst instance.Instance, tarWriter *instancew
}

// Ensure the backup file reflects current config.
err = b.UpdateInstanceBackupFile(inst, op)
err = b.UpdateInstanceBackupFile(inst, snapshots, op)
if err != nil {
return err
}
Expand Down Expand Up @@ -2943,7 +2943,7 @@ func (b *lxdBackend) RenameInstanceSnapshot(inst instance.Instance, newName stri
})

// Ensure the backup file reflects current config.
err = b.UpdateInstanceBackupFile(inst, op)
err = b.UpdateInstanceBackupFile(inst, true, op)
if err != nil {
return err
}
Expand Down Expand Up @@ -5934,7 +5934,7 @@ func (b *lxdBackend) GenerateInstanceBackupConfig(inst instance.Instance, snapsh
}

// UpdateInstanceBackupFile writes the instance's config to the backup.yaml file on the storage device.
func (b *lxdBackend) UpdateInstanceBackupFile(inst instance.Instance, op *operations.Operation) error {
func (b *lxdBackend) UpdateInstanceBackupFile(inst instance.Instance, snapshots bool, op *operations.Operation) error {
l := b.logger.AddContext(logger.Ctx{"project": inst.Project().Name, "instance": inst.Name()})
l.Debug("UpdateInstanceBackupFile started")
defer l.Debug("UpdateInstanceBackupFile finished")
Expand All @@ -5944,7 +5944,7 @@ func (b *lxdBackend) UpdateInstanceBackupFile(inst instance.Instance, op *operat
return nil
}

config, err := b.GenerateInstanceBackupConfig(inst, true, op)
config, err := b.GenerateInstanceBackupConfig(inst, snapshots, op)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion lxd/storage/backend_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (b *mockBackend) GenerateInstanceBackupConfig(inst instance.Instance, snaps
return nil, nil
}

func (b *mockBackend) UpdateInstanceBackupFile(inst instance.Instance, op *operations.Operation) error {
func (b *mockBackend) UpdateInstanceBackupFile(inst instance.Instance, snapshot bool, op *operations.Operation) error {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion lxd/storage/pool_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type Pool interface {
RenameInstance(inst instance.Instance, newName string, op *operations.Operation) error
DeleteInstance(inst instance.Instance, op *operations.Operation) error
UpdateInstance(inst instance.Instance, newDesc string, newConfig map[string]string, op *operations.Operation) error
UpdateInstanceBackupFile(inst instance.Instance, op *operations.Operation) error
UpdateInstanceBackupFile(inst instance.Instance, snapshots bool, op *operations.Operation) error
GenerateInstanceBackupConfig(inst instance.Instance, snapshots bool, op *operations.Operation) (*backupConfig.Config, error)
CheckInstanceBackupFileSnapshots(backupConf *backupConfig.Config, projectName string, deleteMissing bool, op *operations.Operation) ([]*api.InstanceSnapshot, error)
ImportInstance(inst instance.Instance, poolVol *backupConfig.Config, op *operations.Operation) (revert.Hook, error)
Expand Down
1 change: 1 addition & 0 deletions test/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ if [ "${1:-"all"}" != "cluster" ]; then
run_test test_backup_export "backup export"
run_test test_backup_rename "backup rename"
run_test test_backup_volume_export "backup volume export"
run_test test_backup_export_import_instance_only "backup export and import instance only"
run_test test_backup_volume_rename_delete "backup volume rename and delete"
run_test test_backup_different_instance_uuid "backup instance and check instance UUIDs"
run_test test_backup_volume_expiry "backup volume expiry"
Expand Down
26 changes: 25 additions & 1 deletion test/suites/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ test_backup_export_import_recover() {
# Create and export an instance.
lxc launch testimage c1
lxc export c1 "${LXD_DIR}/c1.tar.gz"
lxc rm -f c1
lxc delete -f c1

# Import instance and remove no longer required tarball.
lxc import "${LXD_DIR}/c1.tar.gz" c2
Expand All @@ -990,3 +990,27 @@ EOF
lxc rm -f c2
)
}

test_backup_export_import_instance_only() {
poolName=$(lxc profile device get default root pool)

ensure_import_testimage
ensure_has_localhost_remote "${LXD_ADDR}"

# Create an instance with snapshot.
lxc init testimage c1
lxc snapshot c1

# Export the instance and remove it.
lxc export c1 "${LXD_DIR}/c1.tar.gz" --instance-only
lxc delete -f c1

# Import the instance from tarball.
lxc import "${LXD_DIR}/c1.tar.gz"

# Verify imported instance has no snapshots.
[ "$(lxc query "/1.0/storage-pools/${poolName}/volumes/container/c1/snapshots" | jq "length == 0")" = "true" ]

rm "${LXD_DIR}/c1.tar.gz"
lxc delete -f c1
}

0 comments on commit 0eed2a3

Please sign in to comment.