Skip to content

Commit

Permalink
Fix removing backup and tempdir after backup and restore (#394)
Browse files Browse the repository at this point in the history
* Fix removing backup and tempdir after backup and restore

* Fix

* Fix dir removal
  • Loading branch information
kke committed May 27, 2022
1 parent 223c563 commit 9e46423
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
4 changes: 4 additions & 0 deletions configurer/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,7 @@ func (l Linux) UpsertFile(h os.Host, path, content string) error {

return nil
}

func (l Linux) DeleteDir(h os.Host, path string, opts ...exec.Option) error {
return h.Exec(fmt.Sprintf(`rmdir %s`, shellescape.Quote(path)), opts...)
}
16 changes: 10 additions & 6 deletions phase/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package phase
import (
"fmt"
"os"
"path"
"path/filepath"
"time"

Expand Down Expand Up @@ -63,15 +64,19 @@ func (p *Backup) Run() error {
}

// get the name of the backup file
remoteFile, err := h.ExecOutput(fmt.Sprintf("ls %s", backupDir))
remoteFile, err := h.ExecOutputf(`ls "%s"`, backupDir)
if err != nil {
return err
}
remotePath := path.Join(backupDir, remoteFile)

defer func() {
log.Debugf("%s: cleaning up %s", h, remoteFile)
if err := h.Configurer.DeleteFile(h, remoteFile); err != nil {
log.Warnf("%s: failed to clean up backup temp file %s: %s", h, remoteFile, err)
log.Debugf("%s: cleaning up %s", h, remotePath)
if err := h.Configurer.DeleteFile(h, remotePath); err != nil {
log.Warnf("%s: failed to clean up backup temp file %s: %s", h, remotePath, err)
}
if err := h.Configurer.DeleteDir(h, backupDir, exec.Sudo(h)); err != nil {
log.Warnf("%s: failed to clean up backup temp directory %s: %s", h, backupDir, err)
}
}()

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

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

Expand Down
5 changes: 0 additions & 5 deletions phase/prepare_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
// PrepareHosts installs required packages and so on on the hosts.
type PrepareHosts struct {
GenericPhase
cancel func()
}

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

func (p *PrepareHosts) CleanUp() {
p.cancel()
}

func (p *PrepareHosts) prepareHost(h *cluster.Host) error {
if c, ok := h.Configurer.(prepare); ok {
if err := c.Prepare(h); err != nil {
Expand Down
13 changes: 11 additions & 2 deletions phase/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package phase

import (
"fmt"
"path"

"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
Expand Down Expand Up @@ -49,12 +50,20 @@ func (p *Restore) Run() error {
if err != nil {
return err
}
dstFile := fmt.Sprintf("%s/k0s_backup.tar.gz", tmpDir)
dstFile := path.Join(tmpDir, "k0s_backup.tar.gz")
if err := h.Upload(p.RestoreFrom, dstFile); err != nil {
return err
}

defer func() { _ = h.Configurer.DeleteFile(h, dstFile) }()
defer func() {
if err := h.Configurer.DeleteFile(h, dstFile); err != nil {
log.Warnf("%s: failed to remove backup file %s: %s", h, dstFile, err)
}

if err := h.Configurer.DeleteDir(h, tmpDir, exec.Sudo(h)); err != nil {
log.Warnf("%s: failed to remove backup temp dir %s: %s", h, tmpDir, err)
}
}()

// Run restore
log.Infof("%s: restoring cluster state", h)
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type configurer interface {
CleanupServiceEnvironment(os.Host, string) error
Stat(os.Host, string, ...exec.Option) (*os.FileInfo, error)
Touch(os.Host, string, time.Time, ...exec.Option) error
DeleteDir(os.Host, string, ...exec.Option) error
K0sctlLockFilePath(os.Host) string
UpsertFile(os.Host, string, string) error
}
Expand Down

0 comments on commit 9e46423

Please sign in to comment.