Skip to content

Commit 8cbbc00

Browse files
enhance restore logic: timestamp existing .save backups to avoid overwrites and improve file naming conventions
1 parent c1ca553 commit 8cbbc00

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

src/backupmgr/restore.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path/filepath"
77
"strings"
8+
"time"
89

910
"github.com/JacksonTheMaster/StationeersServerUI/v5/src/logger"
1011
)
@@ -38,7 +39,7 @@ func (m *BackupManager) RestoreBackup(index int) error {
3839
if targetGroup.BinFile != "" && strings.HasSuffix(targetGroup.BinFile, ".save") {
3940
// .save file case
4041
backupFile := targetGroup.BinFile
41-
destFile := filepath.Join("./saves/"+m.config.WorldName, filepath.Base(backupFile))
42+
destFile := filepath.Join("./saves/"+m.config.WorldName, m.config.WorldName+".save")
4243

4344
// Before restore, check if we have existing .save files in the root saves/WorldName dir
4445
saveDir := filepath.Join("./saves/", m.config.WorldName)
@@ -54,7 +55,8 @@ func (m *BackupManager) RestoreBackup(index int) error {
5455
if strings.HasSuffix(file.Name(), ".save") {
5556
existingFile := filepath.Join(saveDir, file.Name())
5657
// Move existing .save file to SafeBackupDir with timestamp to avoid overwrites
57-
savedPreviousHeadSaveFilePath := filepath.Join(m.config.SafeBackupDir, fmt.Sprintf("%s_%s", "_oldHeadSaveBackup", file.Name()))
58+
timestamp := time.Now().Format("2006-01-02_15-04-05")
59+
savedPreviousHeadSaveFilePath := filepath.Join(m.config.SafeBackupDir, fmt.Sprintf("%s_%s_%s", "oldHeadSaveBackup", timestamp, file.Name()))
5860
if err := os.Rename(existingFile, savedPreviousHeadSaveFilePath); err != nil {
5961
return fmt.Errorf("failed to move existing HEAD .save file %s to %s: %w", existingFile, savedPreviousHeadSaveFilePath, err)
6062
}

0 commit comments

Comments
 (0)