Skip to content

Commit 9e46423

Browse files
authored
Fix removing backup and tempdir after backup and restore (#394)
* Fix removing backup and tempdir after backup and restore * Fix * Fix dir removal
1 parent 223c563 commit 9e46423

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

configurer/linux.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,7 @@ func (l Linux) UpsertFile(h os.Host, path, content string) error {
242242

243243
return nil
244244
}
245+
246+
func (l Linux) DeleteDir(h os.Host, path string, opts ...exec.Option) error {
247+
return h.Exec(fmt.Sprintf(`rmdir %s`, shellescape.Quote(path)), opts...)
248+
}

phase/backup.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package phase
33
import (
44
"fmt"
55
"os"
6+
"path"
67
"path/filepath"
78
"time"
89

@@ -63,15 +64,19 @@ func (p *Backup) Run() error {
6364
}
6465

6566
// get the name of the backup file
66-
remoteFile, err := h.ExecOutput(fmt.Sprintf("ls %s", backupDir))
67+
remoteFile, err := h.ExecOutputf(`ls "%s"`, backupDir)
6768
if err != nil {
6869
return err
6970
}
71+
remotePath := path.Join(backupDir, remoteFile)
7072

7173
defer func() {
72-
log.Debugf("%s: cleaning up %s", h, remoteFile)
73-
if err := h.Configurer.DeleteFile(h, remoteFile); err != nil {
74-
log.Warnf("%s: failed to clean up backup temp file %s: %s", h, remoteFile, err)
74+
log.Debugf("%s: cleaning up %s", h, remotePath)
75+
if err := h.Configurer.DeleteFile(h, remotePath); err != nil {
76+
log.Warnf("%s: failed to clean up backup temp file %s: %s", h, remotePath, err)
77+
}
78+
if err := h.Configurer.DeleteDir(h, backupDir, exec.Sudo(h)); err != nil {
79+
log.Warnf("%s: failed to clean up backup temp directory %s: %s", h, backupDir, err)
7580
}
7681
}()
7782

@@ -87,8 +92,7 @@ func (p *Backup) Run() error {
8792
}
8893
defer f.Close()
8994

90-
cmd := fmt.Sprintf("cat %s/%s", backupDir, remoteFile)
91-
if err := h.Exec(cmd, exec.Writer(f)); err != nil {
95+
if err := h.Execf(`cat "%s"`, remotePath, exec.Writer(f)); err != nil {
9296
return err
9397
}
9498

phase/prepare_hosts.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
// PrepareHosts installs required packages and so on on the hosts.
1212
type PrepareHosts struct {
1313
GenericPhase
14-
cancel func()
1514
}
1615

1716
// Title for the phase
@@ -28,10 +27,6 @@ type prepare interface {
2827
Prepare(os.Host) error
2928
}
3029

31-
func (p *PrepareHosts) CleanUp() {
32-
p.cancel()
33-
}
34-
3530
func (p *PrepareHosts) prepareHost(h *cluster.Host) error {
3631
if c, ok := h.Configurer.(prepare); ok {
3732
if err := c.Prepare(h); err != nil {

phase/restore.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package phase
22

33
import (
44
"fmt"
5+
"path"
56

67
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
78
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
@@ -49,12 +50,20 @@ func (p *Restore) Run() error {
4950
if err != nil {
5051
return err
5152
}
52-
dstFile := fmt.Sprintf("%s/k0s_backup.tar.gz", tmpDir)
53+
dstFile := path.Join(tmpDir, "k0s_backup.tar.gz")
5354
if err := h.Upload(p.RestoreFrom, dstFile); err != nil {
5455
return err
5556
}
5657

57-
defer func() { _ = h.Configurer.DeleteFile(h, dstFile) }()
58+
defer func() {
59+
if err := h.Configurer.DeleteFile(h, dstFile); err != nil {
60+
log.Warnf("%s: failed to remove backup file %s: %s", h, dstFile, err)
61+
}
62+
63+
if err := h.Configurer.DeleteDir(h, tmpDir, exec.Sudo(h)); err != nil {
64+
log.Warnf("%s: failed to remove backup temp dir %s: %s", h, tmpDir, err)
65+
}
66+
}()
5867

5968
// Run restore
6069
log.Infof("%s: restoring cluster state", h)

pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ type configurer interface {
123123
CleanupServiceEnvironment(os.Host, string) error
124124
Stat(os.Host, string, ...exec.Option) (*os.FileInfo, error)
125125
Touch(os.Host, string, time.Time, ...exec.Option) error
126+
DeleteDir(os.Host, string, ...exec.Option) error
126127
K0sctlLockFilePath(os.Host) string
127128
UpsertFile(os.Host, string, string) error
128129
}

0 commit comments

Comments
 (0)