Skip to content

Commit 03c3c8b

Browse files
author
Karen Almog
committed
restore k0s.yaml from backup to current directory
Signed-off-by: Karen Almog <kalmog@mirantis.com>
1 parent 68e8c53 commit 03c3c8b

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

pkg/backup/config.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ import (
1111
)
1212

1313
type configurationStep struct {
14-
path string
14+
path string
15+
backupTime string
1516
}
1617

17-
func newConfigurationStep(path string) *configurationStep {
18-
return &configurationStep{path: path}
18+
func newConfigurationStep(path string, archivePath string) *configurationStep {
19+
return &configurationStep{
20+
path: path,
21+
backupTime: getArchiveTimestamp(archivePath)}
1922
}
2023

2124
func (c configurationStep) Name() string {
@@ -42,7 +45,13 @@ func (c configurationStep) Restore(restoreFrom, restoreTo string) error {
4245
return nil
4346
}
4447
logrus.Infof("Previously used k0s.yaml saved under the data directory `%s`", restoreTo)
45-
objectPathInRestored := path.Join(restoreTo, "k0s.yaml")
48+
cwd, err := os.Getwd()
49+
if err != nil {
50+
return err
51+
}
52+
restoredFileName := fmt.Sprintf("k0s_%s.yaml", c.backupTime)
53+
objectPathInRestored := path.Join(cwd, restoredFileName)
54+
4655
logrus.Infof("restoring from `%s` to `%s`", objectPathInArchive, objectPathInRestored)
4756
return util.FileCopy(objectPathInArchive, objectPathInRestored)
4857
}

pkg/backup/manager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type Manager struct {
4141

4242
// RunBackup backups cluster
4343
func (bm *Manager) RunBackup(cfgPath string, clusterSpec *v1beta1.ClusterSpec, vars constant.CfgVars, savePathDir string) error {
44-
bm.discoverSteps(cfgPath, clusterSpec, vars, "backup")
44+
bm.discoverSteps(cfgPath, clusterSpec, vars, "backup", "")
4545
defer os.RemoveAll(bm.tmpDir)
4646
assets := make([]string, 0, len(bm.steps))
4747

@@ -68,7 +68,7 @@ func (bm *Manager) RunBackup(cfgPath string, clusterSpec *v1beta1.ClusterSpec, v
6868

6969
}
7070

71-
func (bm *Manager) discoverSteps(cfgPath string, clusterSpec *v1beta1.ClusterSpec, vars constant.CfgVars, action string) {
71+
func (bm *Manager) discoverSteps(cfgPath string, clusterSpec *v1beta1.ClusterSpec, vars constant.CfgVars, action string, archivePath string) {
7272
if clusterSpec.Storage.Type == v1beta1.EtcdStorageType {
7373
bm.Add(newEtcdStep(bm.tmpDir, vars.CertRootDir, vars.EtcdCertDir, clusterSpec.Storage.Etcd.PeerAddress, vars.EtcdDataDir))
7474
}
@@ -90,7 +90,7 @@ func (bm *Manager) discoverSteps(cfgPath string, clusterSpec *v1beta1.ClusterSpe
9090
}
9191
bm.Add(NewFilesystemStep(path))
9292
}
93-
bm.Add(newConfigurationStep(cfgPath))
93+
bm.Add(newConfigurationStep(cfgPath, archivePath))
9494
}
9595

9696
// Add adds backup step
@@ -132,9 +132,9 @@ func (bm *Manager) RunRestore(archivePath string, k0sVars constant.CfgVars) erro
132132
defer os.RemoveAll(bm.tmpDir)
133133
cfg, err := bm.getConfigForRestore(k0sVars)
134134
if err != nil {
135-
return fmt.Errorf("failed to parse backuped configuration file, check the backup archive: %v", err)
135+
return fmt.Errorf("failed to parse backed-up configuration file.s check the backup archive: %v", err)
136136
}
137-
bm.discoverSteps(fmt.Sprintf("%s/k0s.yaml", bm.tmpDir), cfg.Spec, k0sVars, "restore")
137+
bm.discoverSteps(fmt.Sprintf("%s/k0s.yaml", bm.tmpDir), cfg.Spec, k0sVars, "restore", archivePath)
138138
logrus.Info("Starting restore")
139139

140140
for _, step := range bm.steps {

pkg/backup/util.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,9 @@ func addToArchive(tw *tar.Writer, filename string, baseDir string) error {
9494
func timeStamp() string {
9595
return time.Now().Format(timeStampLayout)
9696
}
97+
98+
func getArchiveTimestamp(archivePath string) string {
99+
fileName := filepath.Base(archivePath)
100+
nameWithoutExt := strings.Split(fileName, ".")[0]
101+
return strings.Trim(nameWithoutExt, "k0s_backup")
102+
}

0 commit comments

Comments
 (0)